Solved

Help for synchronization

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand LInux Boot partition remotly 3 90
sumNumber challenge 16 122
matchUp  challenge 6 60
Unix Command -- Challenging  question 7 88
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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 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