Click to See Complete Forum and Search --> : multithread


sccx
May 3rd, 2006, 04:54 AM
using AfxBeginThread to create 2 threads.
How to synchronize these threads best way possible to get output like?:

Write
Read
Write
Read
.
.
.
UINT write(LPVOID lpData)
UINT read(LPVOID lpData)

CWinThread *thread1 = AfxBeginThread(write,(LPVOID)this);
CWinThread *thread2 = AfxBeginThread(read,(LPVOID)this);

UINT write(LPVOID lpData)
{
while (1)
{
cout << "Write" << endl;
}
return 0;
}


UINT read(LPVOID lpData)
{
while (1)
{
cout << "Read" << endl;
}
return 0;
}

ahoodin
May 3rd, 2006, 07:31 AM
I like to use events, however it is possible to do this with a semiphore.
Additionally, you do not terminate your threads properly. You can do this by simpley returning from the thread and putting while(!bterm) instead of while(true). Have a look at synchronisation objects in the MSDN, otherwise:


UINT write(LPVOID lpData);
UINT read(LPVOID lpData);
CEvent myevent1(FALSE, FALSE,"MY_EVENT1" , NULL);
CEvent myevent2(FALSE, FALSE,"MY_EVENT2" , NULL);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
CWinThread *thread1 = AfxBeginThread(write,(LPVOID)"");
CWinThread *thread2 = AfxBeginThread(read,(LPVOID)"");
while (!(_kbhit()));
}

return nRetCode;
}


UINT write(LPVOID lpData)
{
while (1)
{
myevent1.Lock();
cout << "Write" << endl;
myevent2.SetEvent();//allow thread 2 to go
}
return 0;
}


UINT read(LPVOID lpData)
{
while (1)
{
cout << "Read" << endl;
myevent1.SetEvent();
myevent2.Lock();
}
return 0;
}

sccx
May 3rd, 2006, 03:14 PM
this isn't working right. this portion seems not be correct. correction is not hard but like to see some different thoughts on how to fix this.
UINT write(LPVOID lpData)
{
while (1)
{
myevent1.Lock();
cout << "Write" << endl;
myevent2.SetEvent();//allow thread 2 to go
}
return 0;
}
UINT read(LPVOID lpData)
{
while (1)
{
myevent1.SetEvent();
cout << "Read" << endl;
myevent2.Lock();
}
return 0;
}
how to do the same thing using semaphore?.

Vanaj
May 3rd, 2006, 05:40 PM
how to do the same thing using semaphore?.Have you looked into Mutex's ?

ahoodin
May 4th, 2006, 07:22 AM
try that:

UINT read(LPVOID lpData)
{
while (1)
{
cout << "Read" << endl;
myevent1.SetEvent();
myevent2.Lock();
}
return 0;
}


this isn't working right. this portion seems not be correct. correction is not hard but like to see some different thoughts on how to fix this.
UINT write(LPVOID lpData)
{
while (1)
{
myevent1.Lock();
cout << "Write" << endl;
myevent2.SetEvent();//allow thread 2 to go
}
return 0;
}
UINT read(LPVOID lpData)
{
while (1)
{
myevent1.SetEvent();
cout << "Read" << endl;
myevent2.Lock();
}
return 0;
}
how to do the same thing using semaphore?.

humptydumpty
May 4th, 2006, 07:49 AM
use WaitForSingleObject() and then Proceed have a look in MSDN for more details