A
Abubakar
Hi,
I created 2 functions which run in seperate threads. what they do simply is
that thread1 starts and does something while the thread 2 waits for it. Then
after thread1 is done, it resumes the second thread by calling SetEvent and
itself goes into wait condition by calling WaitForSingleObject(). Now Thread
2 does something and when its done it resumes the thread1 and goes to wait
itself. This goes on for a variable number of time than ends. For this I
wrote a sample code that does this and I'm pasting it over here. My question
is that can anyone tell me that in the following code are there any chances
of the synchronisation getting disturbed? Can this happen in the following
code or is it just perfectly written (from threading point of view)? By
synchronisation getting disturbed I mean thread1 doing what it does more
than one 1 time without thread2 doing its work.
You can also assume that a lot of other threads will also be getting
executed in the process, like some thread getting data from the database,
some thread establishing socket connections and doing communication there,
some threads updating GUI etc ...
And plz also tell me that I'm even writing this code properly or r there any
fundamental problems in there.
I wrote the following code:
//==========================================
// cs.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
HANDLE thread1wait, thread2wait;
int a,b, t1count = 0, t2count = 0, looplimit = 100; //looplimit can be in
millions
DWORD WINAPI thread1 (LPVOID param)
{
::Sleep(2000);
cout<<"now..."<<endl;
while (t1count++ < looplimit)
{
if (a == 0 && b == 0)
:
ebugBreak();
int z = a+ b;
if (z!= 13)
cout<<"not 13 !"<<endl;
a = b = 0;
::SetEvent (thread2wait);
::WaitForSingleObject (thread1wait, INFINITE);
}
cout<<"thread 1 count is "<<t1count<<endl;
return NULL;
}
DWORD WINAPI thread2 (LPVOID param)
{
while (t2count++ < looplimit)
{
::WaitForSingleObject (thread2wait, INFINITE);
a = 4; b = 9;
::SetEvent (thread1wait );
}
cout<<"thread 2 count is "<<t2count<<endl;
return NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
a = 4; b = 9;
cout<<"hello"<<endl;
HANDLE thid1, thid2;
DWORD junk;
thread1wait = ::CreateEventA(NULL, FALSE, FALSE, NULL);
thread2wait = ::CreateEventA(NULL, FALSE, FALSE, NULL);
thid1 = ::CreateThread (NULL, NULL, thread1, NULL, NULL, &junk);
thid2 = ::CreateThread (NULL, NULL, thread2, NULL, NULL, &junk);
HANDLE handles [] = {thid1, thid2};
::WaitForMultipleObjects(2, handles, TRUE, INFINITE);
return 0;
}
//==========================================
Regards,
...ab
I created 2 functions which run in seperate threads. what they do simply is
that thread1 starts and does something while the thread 2 waits for it. Then
after thread1 is done, it resumes the second thread by calling SetEvent and
itself goes into wait condition by calling WaitForSingleObject(). Now Thread
2 does something and when its done it resumes the thread1 and goes to wait
itself. This goes on for a variable number of time than ends. For this I
wrote a sample code that does this and I'm pasting it over here. My question
is that can anyone tell me that in the following code are there any chances
of the synchronisation getting disturbed? Can this happen in the following
code or is it just perfectly written (from threading point of view)? By
synchronisation getting disturbed I mean thread1 doing what it does more
than one 1 time without thread2 doing its work.
You can also assume that a lot of other threads will also be getting
executed in the process, like some thread getting data from the database,
some thread establishing socket connections and doing communication there,
some threads updating GUI etc ...
And plz also tell me that I'm even writing this code properly or r there any
fundamental problems in there.
I wrote the following code:
//==========================================
// cs.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
HANDLE thread1wait, thread2wait;
int a,b, t1count = 0, t2count = 0, looplimit = 100; //looplimit can be in
millions
DWORD WINAPI thread1 (LPVOID param)
{
::Sleep(2000);
cout<<"now..."<<endl;
while (t1count++ < looplimit)
{
if (a == 0 && b == 0)
:

int z = a+ b;
if (z!= 13)
cout<<"not 13 !"<<endl;
a = b = 0;
::SetEvent (thread2wait);
::WaitForSingleObject (thread1wait, INFINITE);
}
cout<<"thread 1 count is "<<t1count<<endl;
return NULL;
}
DWORD WINAPI thread2 (LPVOID param)
{
while (t2count++ < looplimit)
{
::WaitForSingleObject (thread2wait, INFINITE);
a = 4; b = 9;
::SetEvent (thread1wait );
}
cout<<"thread 2 count is "<<t2count<<endl;
return NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
a = 4; b = 9;
cout<<"hello"<<endl;
HANDLE thid1, thid2;
DWORD junk;
thread1wait = ::CreateEventA(NULL, FALSE, FALSE, NULL);
thread2wait = ::CreateEventA(NULL, FALSE, FALSE, NULL);
thid1 = ::CreateThread (NULL, NULL, thread1, NULL, NULL, &junk);
thid2 = ::CreateThread (NULL, NULL, thread2, NULL, NULL, &junk);
HANDLE handles [] = {thid1, thid2};
::WaitForMultipleObjects(2, handles, TRUE, INFINITE);
return 0;
}
//==========================================
Regards,
...ab