Tag Archives: threading

How to synchronize multiple threads?

In certain load tests you want to make sure a bunch of threads reach a particular state before they proceed with the rest of the work. You cannot make sure that all threads execute a point simultaneously since the CPU scheduling would determine this. However you can move these threads to Ready. A ready-thread is a thread can be scheduled for execution on a particular core – http://msdn.microsoft.com/en-us/library/dd627187%28VS.85%29.aspx

WaitForMultipleObjects helps synchronize multiple user mode threads.

“The WaitForMultipleObjects function determines whether the wait criteria have been met. If the criteria have not been met, the calling thread enters the wait state until the conditions of the wait criteria have been met or the time-out interval elapses.”

Here is a small example of how to start multiple threads and then let them proceed after all of them have reached a particular point in execution.

using System;using System.Threading;using System.IO;namespace TestThreading{    class Program    {        const int ThreadCount = 10;        static ManualResetEvent[] events = new ManualResetEvent[ThreadCount];        static ThreadStart onStart = new ThreadStart(Start);        static int locked = -1;        static void Main(string[] args)        {            Thread[] threads = new Thread[ThreadCount];            for (int i = 0; i < ThreadCount; i++)            {                threads[i] = new Thread(onStart);                events[i] = new ManualResetEvent(false);            }            for (int i = 0; i < ThreadCount; i++)            {                threads[i].Start();            }            Console.ReadLine();        }        private static void Start()        {            int threadCount = Interlocked.Increment(ref locked);            Console.WriteLine("Thread {0} started & waiting", threadCount);            Thread.Sleep(3000); //Simulate some work befor setting.            events[threadCount].Set();            ManualResetEvent.WaitAll(events);            Console.WriteLine("Thread {0} proceeded", threadCount);        }    }}

How to throttle callbacks or completions?

WCF enables throttling execution of operations but not their completions. This becomes and issue when a large number of outstanding operations complete almost simultaneously causing the callback on the client to be overwhelmed with completions.  Generally we don’t expect the client to issue of infinite number of pending operations but if you do end up with very high CPU usage and all suspect all your operations are stuck in the callback method which takes a lock then you need to throttle the callbacks yourself.You could try Setting the minThreads but this affects the whole app domain. The issue is due to the large number of callbacks that come in concurrently. The sample attached throttles the callbacks to have only one thread execute completions while there are 20 threads starting the operations and all completing almost simultaneously. The idea is to wrap the AsyncResult  of your operation and complete only the required number of results in parallel and this would throttle the service operation Ends automatically.Sample Source: AsyncEndThrottling