Win32 Thread Theory - Syncronisation

Hello,

I have a for loop similar to the one below that executes several functions inside a multi threaded function.

UINT MultiThreadedFunc(LPVOID pParam)
{
ThreadData *pp = (ThreadData*)pParam;
CSingleLock singleLock(&(pp->pcs));
singleLock.Lock(INFINITE);
if(singleLock.IsLocked())
{
StartNum = pp->StartNum;
pp->StartNum++;
}
singleLock.Unlock();

for(int i = StartNum; i<EndNum; i+=4)
{

CALC(pParam);

//Need to wait for threads to finish before going to next function!

STATS(pParam);

CALC(pParam);

//Need to wait for threads to finish before going to next function!

STATS(pParam);


}

return 0;

}

I have created two threads using AfxBeginThread that execute the above function.

What I want to do is wait for both threads to finish with CALC before moving onto STATS. I have tried using a counter in the pp variable and using a while loop to force each thread to wait until the counter reaches a specified valie. This ends in a deadlock situation though... is there a better way to wait for the threads to reach the same point between the function calls? In OpenMP I can use a barrier - what is the equiavalent in Win32 threads?

One thing I have tried is making CALC and STATS multithreaded functions themselves and creating threads for them inside the for loop and using WaitForMultipleObjects to wait for the them to finish. This works great but I have to use AfxBeginThread inside the for loop. I noticed that AfxBeginThread takes a lot of CPU time and this did not make the program faster at all!

I have to do this using MFC threads as it is for an assignment. Please can someone say which approach above is better (if any!) and steer me in the right direction?

Thanks,

Tom
You need them to synchronise. Have each thread create keep a set of non-signalled events for each checkpoint. At the end of each checkpoint, each thread should signal that it's done and wait for the others to do the same (with WaitForMultipleObjects).

You should note that you have a great big lock at the top of your thread function, so all the threads will block there and the function will be executed serially (not in parallel).
Thank you very much. I have implemented events for each thread and WaitForMultipleObjects and this is working very well.

As for the lock at the top I used this as I was advised that incrementing StartNum using ++ is not thread safe. The for loop below does around 1000 iterations and I profiled the code and the biggest bottleneck is the stats function which hopefully will run a lot faster using two threads. I am using the StartNum variable so each thread executes seperate parts of the for loop.

When you say this will execute serially will it just be the part before the start of the for loop? As you can tell I am new to this subject area, and any advice is really appreciated.

Thank you!
You're right, it unlocks. That's a horrible lock class!.

If you just want to increment a number, WIN32 provide IncrementInterlocked which will do it properly, and not require you to do locking.
Topic archived. No new replies allowed.