This project is read-only.

Thread Initialization Events - Race Condition?

Sep 12, 2008 at 9:59 PM
As threads are created, I am using the Thread Init / Deinit events to allocate resources for each thread and map unquely to each thread from System.Threading.Thread.CurrentThread.ManagedThreadId

There seems to be a race condition between creating the thread pool and attaching the thread init event.  Threads can get created that never had their OnThreadInitialization event handled.  Then when the thread tries to do work, it is missing the resources that were supposed to be allocated.

I am inheriting the threadpool class , but there is no vritual function to override so I have to attach an event.

Is there any thread safe way to do this? 


Sep 14, 2008 at 7:09 AM
Hi,

Create the SmartThreadPool suspended, register to the event and then start the SmartThreadPool.

STPStartInfo stpStartInfo = new STPStartInfo();
stpStartInfo.StartSuspended = true;
SmartThreadPool smartThreadPool = new SmartThreadPool(stpStartInfo);
smartThreadPool.OnThreadInitialization += MyThreadInitializationMethod;
smartThreadPool.Start();

This way MyThreadInitializationMethod will be called on all threads.

Ami