Solved

Help for synchronization

Posted on 2004-09-02
2
151 Views
Last Modified: 2013-11-20
hi
I need some help for threading concept.
Suppose we have three thread. And I want to execute all this thread before executing any code.After executing this three thread, I need to execute a block of code.How can I do this.What threading method will be usefull.please give some example and also give some link for threading concept.
Regards
0
Comment
Question by:nclgaurav
2 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 11962172
What is the reason for three threads if nothing is executed in the main thread before all of them have finished. My logic for using a thread is to keep the main thread reactive/responsive. When you want to block it until all threads have finished I would say one thread can be ommited and its tasks should be executed (as a function?) in the main one.

For general worker threads:
http://www.flounder.com/mvp_tips.htm#Using%20Worker%20Threads

Waiting for threads to finish:
generate an event which is set when the thread is finished. In the main thread you do
WaitForMultipleObject() with all thread events until all of them are set.
0
 
LVL 16

Accepted Solution

by:
nonubik earned 50 total points
ID: 11962758
You can do a while loop to check whether the threads are finished with
GetExitCodeThread(
  HANDLE hThread,
  LPDWORD lpExitCode
);
If the exit code is STILL_ACTIVE continue the loop.

while(1)
{
  DWORD dwExitCode;
  int i=0;
  if(GetExitCodeThread(hThread1, &dwExitCode))
    if(dwExitCode == STILL_ACTIVE)
      i++;
   //same for the other threads

  if(!i)
     break; //no thread active
  Sleep(1);
}

Or just the WaitForMultipleObject way, but you can pass as handles the threads handles, not some thread-event. When one of the threads finishes, it wil signal.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
List out all word 7 224
method notes when mouse over in eclipse 5 68
sum13 challenge 24 77
Problem to open Excel file 15 43
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now