More tips than no answer :
(Note that I do not see any real use for what you are trying to achieve, so I see your problem as a thought experiment / proof of concept that is not explained in detail.)
If you want your threads to be βracesβ for control, don't let the main topic start the game! Creating a thread has some overhead, and the main thread has already been created (since it creates another thread). If you are looking for basically equal chances for both the main and the workflow, you should wait until your workflow is created in the main thread and wait until the main thread starts the race in the background thread. This can be achieved using synchronization objects .
In practice, it will look like this:
You must declare two ManualResetEvents elements that are visible to both the main and background threads as follows:
private static ManualResetEvent backgroundThreadReady = new ManualResetEvent(false); private static ManualResetEvent startThreadRace = new ManualResetEvent(false);
Then in the main thread, you should wait for the thread to initialize:
static void Main(string[] args) { Thread t = new Thread(WriteX); t.Start(); backgroundThreadReady.WaitOne();
And in your subject:
private static void WriteX() { backgroundThreadReady.Set();
Please note that I could use other pending synchronization objects (mutex, autosave event, even locking a critical section with some hacks, I just choose the simplest and fastest solution that can be easily expanded).
mg30rg Jun 01 '15 at 7:55 2015-06-01 07:55
source share