Solved

Help for synchronization

Posted on 2004-09-02
2
154 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
set up trigger (or something) to rename files (starting with ATL) in a linux directory 6 55
matchUp  challenge 9 117
mapBully challenge 6 156
Problem to App source 6 75
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. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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