Skip to main content

Timeline for Code for a simple thread pool in C#

Current License: CC BY-SA 3.0

10 events
when toggle format what by license comment
Mar 30, 2016 at 7:16 comment added Matteo Umili A solution could be to have another list( that will never be "touched" by the methods) containing all the threads. Then in the Dispose method we'll foreach on that list
Mar 30, 2016 at 7:12 comment added Matteo Umili To me the problem could be the fact that if a Worker is running an Action it isn't in the List this._workers, so, the Dispose method (in foreach (var worker in this._workers)) will not wait it to end
Mar 30, 2016 at 7:11 comment added Matteo Umili Your code is awesome, but I noticed that it seems that dispose does not wait the threads to end correctly. Try add the following lines in the main after the pool is disposed: Console.WriteLine("Thread pool disposed!"); Thread.Sleep(2000);. Then place a breakpoint after that line (just to avoid the console to close). You'll notice that after Thread pool disposed! the workers will continue to write their operations (like Worker 0: Ending 39)
May 27, 2013 at 20:58 history edited Legend CC BY-SA 3.0
Fixed a small concurrency bug - Added a lock around `this._workers.AddLast`
May 27, 2013 at 20:55 comment added Legend +1 Thank you. You are right. I asked a question here: stackoverflow.com/questions/16763626/… It seems that the problem was indeed due to the missing lock. Thanks for your time. I'm currently using .NET 3.5 and this works like a charm.
May 27, 2013 at 17:04 comment added Milan Gardian @Legend not sure what the problem might be, but if I had to guess I'd say it is related to the fact that _workers linked list is being accessed outside of lock. If using .NET 4, you could try using ConcurrentQueue<Action> instead.
May 26, 2013 at 20:50 comment added Legend +1 Thank you. I was using this snippet but after an extremely long period of time, I encountered an error: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at System.Collections.Generic.LinkedList'1.InternalInsertNodeBefore(LinkedListNode>' node, LinkedListNode'1 newNode) at System.Collections.Generic.LinkedList'1.AddLast(T value) at Prog.Pool.Worker()`. Any idea what is causing this?
Apr 10, 2013 at 2:54 review Suggested edits
Apr 10, 2013 at 3:11
Dec 9, 2010 at 20:24 history edited Milan Gardian CC BY-SA 2.5
Simplified worker implementation, added comments
Jan 12, 2009 at 18:55 history answered Milan Gardian CC BY-SA 2.5