Giter Site home page Giter Site logo

amibar / smartthreadpool Goto Github PK

View Code? Open in Web Editor NEW
507.0 65.0 182.0 1.44 MB

A .NET Thread Pool fully implemented in C# with many features

Home Page: http://www.codeproject.com/Articles/7933/Smart-Thread-Pool

License: Microsoft Public License

C# 100.00%

smartthreadpool's Introduction

smartthreadpool's People

Contributors

acgessler avatar amibar avatar paszczi avatar robhruska avatar tiwariarvin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

smartthreadpool's Issues

is it must to ShutDown/Dispose STP?

Hi,
We recently explored STP, and started using it in one of our asp.net core application.
and our implementation is like, during particular object's constructor we configure/instantiate the smart thread pool with work item groups and min, max threads etc.

There are two questions:

  1. We often face outofmemory exceptions, though we have 8 core, 16GB RAM with 1 TB HDD VM. any guidelines or best practices to adept ?
  2. when to use ShutDown and Dispose methods. Since this is a web application, we as said above, configured, in one of our singleton class. so when to dispose? or shutdown?
  3. I tried with destructor and it seems it didn't get called. Then I tried with application stopping event in asp.net core where I retrieve the singleton instance and called custom dispose method to shutdown and dispose stp. is this right approach?

Thanks,
Matheswaran S

Dual-license under MIT or BSD if possible

Awesome work on SmartThreadPool - it solves a lot of problems for me! However, my employer has a list of pre-approved licenses that I'm allowed to use, and MS-PL isn't on the list.

It appears to me that both BSD 2.0 and MIT licenses are basically the equivalent of MS-PL. Do you have any objections to dual-licensing SmartThreadPool under one of those licenses?

Threads with context

Hi,

I'm using the STP but wrapped within the following code:

using (new Impersonator( "myUsername", "myDomainname", "myPassword" ))
{
    [code that executes under the new context]
}

see code here:
http://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User?msg=4023519#xx4023519xx

But when the threads execute I get an error thrown stating:
Invalid token for impersonation - it cannot be duplicated.

Do you know what is going on here?
I've tried to set the UseCurrentContext but to no avail. Do you know how I can kick off a thread so it carried using the Impersonatoer context it was kick off within?

I'm assuming that I need to pass through the UserToken for the user I'm logged in as I but can't find how to do this anywhere

New dotnet libraries and mechanisms

Hello Ami,
A chance to thank you for your great job!
I'm using STP a lot and this is defiantly my favorite library!

Do you consider for future releases some of the new dotnet libraries and mechanisms?

  • async/await?
  • ValueTasks?
  • System.Threading.Channels as a Base queue?

Thanks!

Race condition in SmartThreadPool causes not processing tasks

Hi,

I found an issue in using groups and I would like to check if it is known issue or not.

steps to reproduce:

  1. Start a ThreadPool with a WorkGroup - set MinThreads = 0, MaxThreads =1, IdleTimeout = 60 seconds
  2. Send a task to WorkItemGroup every 60 seconds
  3. after some time, tasks stop to process

Here is what happens:
Thread#1 [STP Worker thread] timeouts, because it has empty queue for 60 seconds and starts terminating (there's still some work to do after the timeout check and before the thread is freed)
In meantime
Thread#2 [new thread] enqueues a task to the WorkItemGroup,
Thread#2 WorkItemGroup enqueues a task to SmartThreadPool expecting that STP withdraw the task,
Thread#2 STP enqueues a task and doesn't start a thread, because there's one already running (it doesn't know that it's terminating)
Thread#1 [STP Worker thread] finishes termination and removes itself from STP
Thread#3 [new thread] [Receiver] enqueues a task to Work Items Group
Thread#3 WorkItemGroup doesn't do anything, because it thinks that STP has already enough tasks in queue

since then the STP has no running thread and all new tasks are enqueued to the WorkItemGroup and the queue is growing and never processed

use SmartThreadPool app crashed

problem:
问题签名:
问题事件名称: BEX64
应用程序名: Run.exe
应用程序版本: 2.0.0.0
应用程序时间戳: 5d6dfe32
故障模块名称: StackHash_b4ee
故障模块版本: 0.0.0.0
故障模块时间戳: 00000000
异常偏移: 000007fe00000000
异常代码: c0000005
异常数据: 0000000000000008
OS 版本: 6.1.7601.2.1.0.274.10
区域设置 ID: 2052
其他信息 1: b4ee
其他信息 2: b4ee5de6a2322745523997a782b35692
其他信息 3: 277e
其他信息 4: 277e19c30fbd5f6bb531ec9e027c37c3

program info:
// 全局类型:
// 入口点: run.Main
// 架构: x64
// 运行库: .NET Framework 4.5.1
// 时间戳: 5DAD0D73 (2019/10/21 1:44:19)

SmartThreadPool.
// SmartThreadPool.dll

// 全局类型:
// 架构: AnyCPU (64位优先)
// 运行库: .NET Framework 4.5
// 时间戳: <未知> (83155524)

what can i do to sovle this problem
thankyou

Add support for .net framework 4.7.x and .net framework 4.8

Hi @amibar
Would it be possible to add support for dotnet 4.7.x (4.7.2 in particular) and dotnet 4.8 ?
dotnet 4.7.2 is the default .net version that comes with Windows 10, 11, and server 2019 and dotnet 4.8 is the LTS version of the dotnet framework. It will help a lot for maintaining old code and upgrading it to dotnet versions that are still supported (moving the code to dotnet core and dotnet 5.0+ will take more time).

Thanks

System.PlatformNotSupportedException: Thread abort is not supported on this platform.

Hello,
I'm using NetCore version: 3.1.302, but when i running, have error:
System.PlatformNotSupportedException: Thread abort is not supported on this platform. at System.Threading.Thread.Abort() at Amib.Threading.SmartThreadPool.Shutdown(Boolean forceAbort, Int32 millisecondsTimeout) at Amib.Threading.SmartThreadPool.Shutdown()

I saw admin talk it support in versionn 3.1, but why when i use, it's can't work?

Any can help me?

System.InvalidOperationException when Cancel ThreadPool

Hello,

I'm using this library for long-times but i recently had a problem :

Here the call stack (french sorry) :

System.InvalidOperationException: La collection a été modifiée; l'opération d'énumération peut ne pas s'exécuter.
à System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
à System.Collections.Generic.Dictionary`2.ValueCollection.Enumerator.MoveNext()
à Amib.Threading.SmartThreadPool.Cancel(Boolean abortExecution)

When watching the code of the method i think, it must create a copy of the values before looping on it. Replace :

foreach (ThreadEntry threadEntry in _workerThreads.Values)

by

var threadEntrys = _workerThreads.Values.ToList()
foreach (ThreadEntry threadEntry in threadEntrys )

New Release with Fixes

Would you mind releasing the latest version (with InUseThreads for IWorkItemsGroup) as a new NuGet Version? Would be useful for me without having to fork it / Create another NuGet package

Cancel(true) + WaitForIdle()

Hi Ami,

I'm having an issue and need your advice.
Code is very simple :

var pool = new SmartThreadPool(true);
items.ForEach(item => pool.QueueWorkItem(...));
try
{
	pool.Start();
	pool.WaitForIdle();
}
catch (ThreadAbortException)
{
	Thread.ResetAbort();
	pool.Cancel(true);
	pool.WaitForIdle();
}

In each working items, the Thread.Abort is handled to perform cleaning stuff.
What I want is, in case of cancellation, to wait for all threads in the pool to have this "cleaning stuff" done.
But the pool.WaitForIdle() in the catch doesn't seams to block, as there is some thread still alive doing their cleansing.

Is there a better/correct way to do this with your library ?
Thanks Ami !

Workitem with high priority within workgroup is being not picked up by STP

Hi, I have 20-30 workgroups. Each workgroup will have arroud 3 to 4k workitems with different priorities. Sometime i face a problem. Few workgroup items are not being picked up even though those workitems are having high priority. Lower priority items are being picked from other workgroup. I tried to simulate the issue, but I couldn't. This issue doesn't happen always.

Have you come across similar issue?

Null reference exception on IsWorkItemCanceled call

I've ugraded from using an older version of STP to the newest one available on NuGet.

Now I get an error:

System.NullReferenceException

With the following stack trace (excerpt):

at Amib.Threading.SmartThreadPool.get_IsWorkItemCanceled() in e:\Dev\STP\STP.git\SmartThreadPool\SmartThreadPool.cs:Zeile 1420.
at ZetaTest.Runtime.Code.ThreadPooling.ThreadPoolManager.CheckWantCancel() in C:\P\Zeta Test\3.5\Zeta Test Main\Source\Runtime\Code\ThreadPooling\ThreadPoolManager.cs:Zeile 260.
at ...

It seems that the line number 140 is different from the code in this GitHub repository, since the function IsWorkItemCanceled seems to be located here.

My question:

Am I doing anything wrong to get the null reference exception?

Two threads get the same WorkItem

I'm using the STP 2.2.1 in a server application.
Today I had a problem that a work item has been processed by two threads simultaneously.
In this a known issues?

When looking into the sourcecode I've notices the method

PriorityQueue.Dequeue ()

In this method there is a sequence

workItem = _queues[queueIndex].First.Value;
_queues[queueIndex].RemoveFirst();

For me it seems that these two actions are not atomic, and it might be that two threads got the same WorkItem.
Is that possible?

Follow-up support plan

This repository has not been updated for a long time. Is there any further update in the next step, such as .net7 .net8...

STP SmartThreadPool Thread# ThreadCounter > MaxThreads Settings

Our MaxThreads settings is set to 20. However I see that the Thread Name [STP SmartThreadPool Thread#] increase over time and is currently at STP SmartThreadPool Thread #62 We've noticed this only in our test environments.

Can you please explain under what circumstances can the ThreadCounter number be greater than the MaxThreads settings?

why WorkItemResultTWrapper<T>.Exception cast to T

when I try to use IWorkItemResult.Exception, I am get a System.InvalidCastException

exception detail:

InvalidCastException: Cannot cast from source type to destination type.
Amib.Threading.Internal.WorkItemResultTWrapper`1[System.String].get_Exception ()

and i looking the source code:

public object Exception
{
    get { return (TResult)_workItemResult.Exception; }
}

why the exception will be cast to TResult? is it a bug or feature

STP is incompatible with .NET 2.0 sp0

In .NET 2.0 sp0 we didn't have WaitOne/WaitAll/WaitAny that took waithandle(s) and an Int32. It was added in .NET 2.0 sp2. The overloads with exitContext = false provides identical functionality, but it's compatible with .NET 2.0 (yes I have machines that have 0 patches to them, nor can I affect them).

stp.WaitForIdle() does not work (rare)

SmartThreadPool v2.2.3

This finding is very rare, I've encountered this problem only a couple of times so far. Therefore, It's very hard to reproduce.

On some occasions the WaitForIdle doesn't wait with callbacks on the queue. It seems like the ManualResetEvent was Set() or the underlying state changed somehow when it shouldn't have been.

To simulate this problem uncomment the call to StpChangeState().

The behavior with StpChangeState() uncommented is consistent with what I'm occasionally encountering.

Any idea what may be causing this? Has this problem been reported before?


         using (var stp = new SmartThreadPool(new STPStartInfo
                       { MinWorkerThreads = 0, MaxWorkerThreads = 2, StartSuspended = false }))
            {
                do
                {
                    stp.QueueWorkItem(JobOne, stp);

                    stp.QueueWorkItem(JobTwo, stp);

                    Thread.Sleep(20);
                    
                    // Uncomment this line to simulate the problem
                    // StpChangeState(stp); 

                    stp.WaitForIdle();

                } while (stp.InUseThreads > 0);
            }

            Console.WriteLine("Enter any key to close");
            Console.ReadKey();
        }

        private static void JobOne(SmartThreadPool obj)
        {
            Thread.Sleep(TimeSpan.FromSeconds(10));
            Console.WriteLine(MethodBase.GetCurrentMethod());
        }

        private static void JobTwo(SmartThreadPool obj)
        {
            Thread.Sleep(TimeSpan.FromSeconds(10));
            Console.WriteLine(MethodBase.GetCurrentMethod());
        }
        
        private static void StpChangeState(SmartThreadPool stp)
        {
            FieldInfo field = stp.GetType().GetField("_isIdleWaitHandle", BindingFlags.NonPublic | BindingFlags.Instance);
            var value = (ManualResetEvent)field?.GetValue(stp);
            value?.Set();
        }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.