中间用注释框起来的部份是有问题的部分,我在等线程执行了几秒钟以后,设置线程中止的标志,标志已经设置成为true了,按照道理,线程应该会正常的退出(循环中发现中止标志为true则会break;)可是却没有,在下一句WaitForSingleObject一直要等到线程time out,不知道是为什么.请哪位仁兄帮忙,谢谢
#include <windows.h>
#include <iostream>
using namespace std;
#define THREAD_COUNT 3CRITICAL_SECTION cs;typedef struct ThreadStruct
{
int id;
bool stop;
} THREAD_STRUCT;DWORD WINAPI ThreadProc(LPVOID lpParam)
{
EnterCriticalSection(&cs); THREAD_STRUCT* pts = (THREAD_STRUCT*)lpParam; for (int i = 0; i < 10000; i ++)
{
cout << "Now executing thread, " << pts->id << endl;
if (pts->stop) break;
} if (pts->stop)
{
cout << "about thread " << pts->id << endl;
LeaveCriticalSection(&cs);
return -1;
} LeaveCriticalSection(&cs);
return 0;
}int main(void)
{
InitializeCriticalSection(&cs); HANDLE dwHandles[THREAD_COUNT];
THREAD_STRUCT ts[3];
memset(ts, 0, sizeof(ts));
for (int i = 0; i < THREAD_COUNT; i ++)
{
DWORD dwThreadID;
dwHandles[i] = CreateThread(NULL, 0, ThreadProc, (LPVOID)&ts[i], 0, &dwThreadID);
}
Sleep(3000); //在这里等三秒,让线程执行一段先 //************************************************************
ts[0].stop = true;
cout << "before stop thread" << endl;
DWORD dwRet = WaitForSingleObject(dwHandles[0], 1000);
if (dwRet == WAIT_TIMEOUT) TerminateThread(dwHandles[0], -3);
cout << "after stop thread" << endl;
//************************************************************
WaitForMultipleObjects(THREAD_COUNT, dwHandles, TRUE, INFINITE);
DeleteCriticalSection(&cs);
}
#include <windows.h>
#include <iostream>
using namespace std;
#define THREAD_COUNT 3CRITICAL_SECTION cs;typedef struct ThreadStruct
{
int id;
bool stop;
} THREAD_STRUCT;DWORD WINAPI ThreadProc(LPVOID lpParam)
{
EnterCriticalSection(&cs); THREAD_STRUCT* pts = (THREAD_STRUCT*)lpParam; for (int i = 0; i < 10000; i ++)
{
cout << "Now executing thread, " << pts->id << endl;
if (pts->stop) break;
} if (pts->stop)
{
cout << "about thread " << pts->id << endl;
LeaveCriticalSection(&cs);
return -1;
} LeaveCriticalSection(&cs);
return 0;
}int main(void)
{
InitializeCriticalSection(&cs); HANDLE dwHandles[THREAD_COUNT];
THREAD_STRUCT ts[3];
memset(ts, 0, sizeof(ts));
for (int i = 0; i < THREAD_COUNT; i ++)
{
DWORD dwThreadID;
dwHandles[i] = CreateThread(NULL, 0, ThreadProc, (LPVOID)&ts[i], 0, &dwThreadID);
}
Sleep(3000); //在这里等三秒,让线程执行一段先 //************************************************************
ts[0].stop = true;
cout << "before stop thread" << endl;
DWORD dwRet = WaitForSingleObject(dwHandles[0], 1000);
if (dwRet == WAIT_TIMEOUT) TerminateThread(dwHandles[0], -3);
cout << "after stop thread" << endl;
//************************************************************
WaitForMultipleObjects(THREAD_COUNT, dwHandles, TRUE, INFINITE);
DeleteCriticalSection(&cs);
}
The CriticalSection is locking two threads but only one thread is running, and now the WaitForSingleObject is wait the thread which is not running(in EnterCriticalSection), so, the main program is dead.Should first set ALL flags of threads, next wait ALL thread for termination.
thanks again.
===========================#include <windows.h>
#include <iostream>
using namespace std;
#define THREAD_COUNT 3CRITICAL_SECTION cs;typedef struct ThreadStruct
{
int id;
bool stop;
} THREAD_STRUCT;DWORD WINAPI ThreadProc(LPVOID lpParam)
{
EnterCriticalSection(&cs); THREAD_STRUCT* pts = (THREAD_STRUCT*)lpParam; for (int i = 0; i < 300; i ++)
{
cout << "Now executing thread, " << pts->id << endl;
if (pts->stop)
{
// cout << "tracking pts->stop is true!" << endl;
break;
}
} if (pts->stop)
{
cout << "about thread " << pts->id << endl;
LeaveCriticalSection(&cs);
return -1;
} LeaveCriticalSection(&cs);
return 0;
}int main(void)
{
InitializeCriticalSection(&cs); HANDLE dwHandles[THREAD_COUNT];
THREAD_STRUCT ts[3];
memset(ts, 0, sizeof(ts));
for (int i = 0; i < THREAD_COUNT; i ++)
{
DWORD dwThreadID;
dwHandles[i] = CreateThread(NULL, 0, ThreadProc, (LPVOID)&ts[i], 0, &dwThreadID);
}
Sleep(1000); // set flags
for (int i = 0; i < THREAD_COUNT; i ++)
ts[i].stop = true; // wait threads
for (int i = 0; i < THREAD_COUNT; i ++)
{
cout << "stop thread: " << i << endl;
cout << "before stop thread" << endl;
if (WaitForSingleObject(dwHandles[i], 1000) == WAIT_TIMEOUT)
{
cout << "using TerminateThread function!!!!!!!!!!!!!!!!!!!!" << endl;
TerminateThread(dwHandles[i], -2);
}
cout << "after stop thread" << endl;
} DeleteCriticalSection(&cs);
}