Try it :) :
...
class CYourThreadsOwner
{
CCriticalSection m_cCritSection;
CWinThread* m_pcThreads[3];
bool m_bWorking;
BYTE m_byBuffer[1024];
static DWORD WINAPI thread1(CYourThreadsOwner*);
static DWORD WINAPI thread2(CYourThreadsOwner*);
static DWORD WINAPI thread3(CYourThreadsOwner*);
public:
CYourThreadsOwner();
~CYourThreadsOwner();
};
...
CYourThreadsOwner::CYourThreadsOwner()
{
memset(m_byBuffer, 0, sizeof(m_byBuffer));
m_bWorking = true;
m_pcThreads[0] = AfxBeginThread(thread1, this);
m_pcThreads[1] = AfxBeginThread(thread2, this);
m_pcThreads[2] = AfxBeginThread(thread3, this);
}
CYourThreadsOwner::~CYourThreadsOwner()
{
m_bWorking = false;
HANDLE ahTherads[3];
ahTherads[0] = m_pcThreads[0]->m_hThread;
ahTherads[1] = m_pcThreads[1]->m_hThread;
ahTherads[2] = m_pcThreads[2]->m_hThread;
WaitForMultipleObjects(3, ahTherads, TRUE, INFINITE);
}
DWORD WINAPI CYourThreadsOwner::thread1(CYourThreadsOwner* pcOwnner)
{
while (pcOwnner && pcOwnner->m_bWorking) {
CSingleLock cLock(pcOwnner->m_cCritSection);
cLock.Lock();
cLock.Unlock();
Sleep(10);
}
return 0;
}
DWORD WINAPI CYourThreadsOwner::thread2(CYourThreadsOwner* pcOwnner)
{
while (pcOwnner && pcOwnner->m_bWorking) {
CSingleLock cLock(pcOwnner->m_cCritSection);
cLock.Lock();
cLock.Unlock();
Sleep(10);
}
return 0;
}
DWORD WINAPI CYourThreadsOwner::thread3(CYourThreadsOwner* pcOwnner)
{
while (pcOwnner && pcOwnner->m_bWorking) {
CSingleLock cLock(pcOwnner->m_cCritSection);
cLock.Lock();
cLock.Unlock();
Sleep(10);
}
return 0;
}