Second worker thread should start after first has finished it task.
-
Hi all, I have two worker thread. one thread is populating the list and second thread is doing operation on that list. my problem is i want that after i have populated the list then only second thread should start. How can i do it? Thanks in advance
-
Hi all, I have two worker thread. one thread is populating the list and second thread is doing operation on that list. my problem is i want that after i have populated the list then only second thread should start. How can i do it? Thanks in advance
In the second thread you can use WaitForSingleObject() to wait for the first thread's handle to complete.
-
Hi all, I have two worker thread. one thread is populating the list and second thread is doing operation on that list. my problem is i want that after i have populated the list then only second thread should start. How can i do it? Thanks in advance
-
Hi all, I have two worker thread. one thread is populating the list and second thread is doing operation on that list. my problem is i want that after i have populated the list then only second thread should start. How can i do it? Thanks in advance
In addition to the previous answer: is there a particular reason why you want to use a second thread ? You have to wait until the first thread is finished, so why don't you simply execute that task in the same thread as the first one. I don't really see an added value of starting a second thread when the first one terminates.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Hi all, I have two worker thread. one thread is populating the list and second thread is doing operation on that list. my problem is i want that after i have populated the list then only second thread should start. How can i do it? Thanks in advance
//
#include "stdafx.h"#include "iostream"
using namespace std;HANDLE hThread1 = NULL; //worker thread 1
HANDLE hThread2 = NULL; //worker therad 2HANDLE hEvt1 = NULL;
DWORD __stdcall ThreadProc_1( LPVOID lpParameter )
{
// populating the list ...
for(int i = 0; i<10; i++)
{
cout<<"populating the list -- "<<i<<endl;
}// after populate the list , set event, then the worker thread 2 will stop waiting and do operation on the list... ::SetEvent(hEvt1); return 0;
}
DWORD __stdcall ThreadProc_2( LPVOID lpParameter )
{
// wait
while(::WaitForSingleObject(hEvt1, 5) == WAIT_TIMEOUT)
{
//keep waiting ...
Sleep(5);
continue;
}// To do operation on that list... for(int i = 0; i<10; i++) { cout<<"doing operation on that list -- "<<i<<endl; } return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
hEvt1 = CreateEvent(NULL, TRUE, FALSE, NULL);//Create the two worker threads hThread1 = CreateThread(NULL, 0, ThreadProc\_1, NULL, 0, NULL); hThread2 = CreateThread(NULL, 0, ThreadProc\_2, NULL, 0, NULL); // waiting , while(::WaitForSingleObject(hThread2, 5) == WAIT\_TIMEOUT) { Sleep(5); continue; } if(hThread1) ::CloseHandle(hThread1); if(hThread2) ::CloseHandle(hThread2); if(hEvt1) ::CloseHandle(hEvt1); return 0;
}
-
In addition to the previous answer: is there a particular reason why you want to use a second thread ? You have to wait until the first thread is finished, so why don't you simply execute that task in the same thread as the first one. I don't really see an added value of starting a second thread when the first one terminates.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++quite right
-
//
#include "stdafx.h"#include "iostream"
using namespace std;HANDLE hThread1 = NULL; //worker thread 1
HANDLE hThread2 = NULL; //worker therad 2HANDLE hEvt1 = NULL;
DWORD __stdcall ThreadProc_1( LPVOID lpParameter )
{
// populating the list ...
for(int i = 0; i<10; i++)
{
cout<<"populating the list -- "<<i<<endl;
}// after populate the list , set event, then the worker thread 2 will stop waiting and do operation on the list... ::SetEvent(hEvt1); return 0;
}
DWORD __stdcall ThreadProc_2( LPVOID lpParameter )
{
// wait
while(::WaitForSingleObject(hEvt1, 5) == WAIT_TIMEOUT)
{
//keep waiting ...
Sleep(5);
continue;
}// To do operation on that list... for(int i = 0; i<10; i++) { cout<<"doing operation on that list -- "<<i<<endl; } return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
hEvt1 = CreateEvent(NULL, TRUE, FALSE, NULL);//Create the two worker threads hThread1 = CreateThread(NULL, 0, ThreadProc\_1, NULL, 0, NULL); hThread2 = CreateThread(NULL, 0, ThreadProc\_2, NULL, 0, NULL); // waiting , while(::WaitForSingleObject(hThread2, 5) == WAIT\_TIMEOUT) { Sleep(5); continue; } if(hThread1) ::CloseHandle(hThread1); if(hThread2) ::CloseHandle(hThread2); if(hEvt1) ::CloseHandle(hEvt1); return 0;
}
-
Xie Jinghui wrote:
while(::WaitForSingleObject(hEvt1, 5) == WAIT_TIMEOUT)
{
//keep waiting ...
Sleep(5);
continue;
}This has the same effect than:
::WaitForSingleObject(hEvt1, INFINITE);
but is more complex and less efficient as the OS every 5 seconds schedule the thread to be runt again; using a wait timeout of
INFINITE
is better because the OS suspend the thread and resume it only when the eventis signeled. -
You don't really need to create an event: the second thread could simply wait for the first thread to terminate
::WaitForSingleObject(hThread1, INFINITE);
-
-
-
Xie Jinghui wrote:
while(::WaitForSingleObject(hEvt1, 5) == WAIT_TIMEOUT)
{
//keep waiting ...
Sleep(5);
continue;
}This has the same effect than:
::WaitForSingleObject(hEvt1, INFINITE);
but is more complex and less efficient as the OS every 5 seconds schedule the thread to be runt again; using a wait timeout of
INFINITE
is better because the OS suspend the thread and resume it only when the eventis signeled. -
You don't really need to create an event: the second thread could simply wait for the first thread to terminate
::WaitForSingleObject(hThread1, INFINITE);
Thank you, :) I always overlook the details,,, the program will be more efficient,
-
-
Hi all, I have two worker thread. one thread is populating the list and second thread is doing operation on that list. my problem is i want that after i have populated the list then only second thread should start. How can i do it? Thanks in advance