Solved

Help for synchronization

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ADO Memory leak with DELPHI 2007 37 171
Named range not carried over 10 62
wait notify demo infinite loop 3 96
NotAlone Challenge 20 80
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

17 Experts available now in Live!

Get 1:1 Help Now