Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help for synchronization

Posted on 2004-09-02
2
Medium Priority
?
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 100 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

660 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