High Performance by Design
Can a Parallel Application for Windows
Have More Than 64 Threads?
[Posted by Jenn-Ching Luo on Apr. 27, 2009 ]
The answer is definitely YES. Some people may wonder why this question is raised? The confusion is from the Windows API WaitForMultipleObjects.
In parallel processing, cooperating tasks (or threads) need to synchronize themselves. The WINAPI WaitForMultipleObjects provides a simple and effective way to wait for multiple cooperating threads. I personally believe many programmers, including myself, use the API for parallel processing.
The calling syntax to WaitForMultipleObjects is as:
DWORD WINAPI WaitForMultipleObjects( __in DWORD nCount, __in const HANDLE *lpHandles, __in BOOL bWaitAll, __in DWORD dwMilliseconds );
A parallel application on Windows certainly can have more than 64 threads. If a parallel application has more than 64 threads, the underlying question is how to apply WaitForMultipleObjects to wait for all cooperating threads? It is very easy to resolve the question. We can apply a loop.
The array pointed to by lpHandles can be viewed as a collection of segments, each of which has MAXIMUM_WAIT_OBJECTS handles. Then, call WaitForMultipleObjects to wait for handles (in this example, threads) in each segment. For example, I use the following "non-standard" fortran statements to illustrate it
do i = 1,handles, MAXIMUM_WAIT_OBJECTS
j = min0(handles-i+1,MAXIMUM_WAIT_OBJECTS)
dword_var = WaitForMultipleObjects(j,handle(i),
where handles is the object handles in the array handle, and each segment of object handles has a length of MAXIMUM_WAIT_OBEJCTS. In the above example, variable j is the number of object handles in a segment. j equals to MAXIMUM_WAIT_OBJECTS for each segment other than the last segment. handle(i) is the reference (or pointer) of the segment. [Note: the above fortran example is for illustration only. An interface is necessary to set forth argument is passed by either reference or value]. The entire object handles can be waited for in a loop.
It is definitely OK to have more than 64 threads in an application for Windows. However, creating more threads cannot guarantee an improvement of parallel performance.