各位大侠,用AfxBeginThread创建10个线程,然后用HANDLE hThread[10]分别存储10个AfxBeginThread返回的CWinthread指针的->m_hThread,最后用
dwWaitResult = WaitForMultipleObjects(10,hThread,TRUE,10000);来wait这十个handle,
结果根本不要用1秒钟函数就返回了,而且返回的值在0到10之间,好像是将参数bWaitAll设为FALSE的那种效果,谁能解释这个问题?另外,线程结束后句柄都释放了,这样WaitForMultipleObjects是不是就会出现这个问题?因为我看了以下CWinThread类有个成员
m_bAutoDelete Specifies whether to destroy the object at thread termination.
就是说,线程结束对象被释放。大家都是怎么做的?就是想实现等线程都结束了,再做另一件事的功能。
多谢!!
dwWaitResult = WaitForMultipleObjects(10,hThread,TRUE,10000);来wait这十个handle,
结果根本不要用1秒钟函数就返回了,而且返回的值在0到10之间,好像是将参数bWaitAll设为FALSE的那种效果,谁能解释这个问题?另外,线程结束后句柄都释放了,这样WaitForMultipleObjects是不是就会出现这个问题?因为我看了以下CWinThread类有个成员
m_bAutoDelete Specifies whether to destroy the object at thread termination.
就是说,线程结束对象被释放。大家都是怎么做的?就是想实现等线程都结束了,再做另一件事的功能。
多谢!!
线程创建肯定成功了,因为我的系统一直在干活呢,而且从任务管理器观察线程数很正常,如果谁做过类似等待demo,希望能给点帮助,多谢!!
//初始化:
HANDLE hEvents[10];
for (int i = 0; i < 10; i++)
hEvents[i] = CreateEvent(NULL, FALSE, FALSE, NULL);//线程函数i (1<=i<=10)
{
...
//在线程i结束前调用:
SetEvent(hEvents[i]);
}//等待前10个线程执行完毕或者10s超时
dwWaitResult = WaitForMultipleObjects(10,hEvents,TRUE,10000);
WaitForMultipleObjects(10,hEvents,TRUE,60000);//hEvents为存放自动重置事件句柄的数组
的时候,它不会返回WAIT_OBJECT_0,而是返回0到WAIT_OBJECT_0 + 10之间的值。
后来我跟踪进去的时候,发现在线程函数中执行:BOOL bSet = SetEvent(hEvents[i]);此语句的时候bSet = 0;根据MSDN也就是SetEvent没有成功。请问在那种情况下SetEvent会失败呢?我找了很多地方都找不到这种原因。
关注
还有很奇怪的问题,我的这个函数是封装在动态库里面的,在别人的机器上调试可以通过,在我的机器上如果直接调用动态库的话老是在这个WaitForMultipleObjects经常出错,但是如果我把dll的源代码在我的机器上编译一下再调用,却有可以成功,请问谁知道这种情况是怎么回事情啊?
另外爱神兄弟好像没看懂我我们讨论的问题啊,线程结束了,句柄都释放了,哪里有什么等待可言?
::CloseHandle(hThread)之后才会释放。所以线程结束后,线程变为有信号,你才有可能等待。CWinThread::m_bAutoDelete
ResSpecifies whether the CWinThread object should be automatically deleted at thread termination. The m_bAutoDelete data member is a public variable of type BOOL.
[email protected]
1。AfxBeginThread()//设置CREATE_SUSPENDED标志,使线程挂起
2。设置m_bAutoDelete为False
3。保存10个m_hThread到HANDLE hThread[10]中
4。CWinThread::ResumeThread 启动线程
5。dwWaitResult = WaitForMultipleObjects(10,hThread,TRUE,10000);
6。当WaitForMultipleObjects函数返回时,查询10个线程的退出代码:GetExitCodeThread()
7。手动销毁线程
{
hThread[i]=AfxBeginThread((AFX_THREADPROC)WorkerThread,(LPVOID)pParam,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);
hThread[i]->m_bAutoDelete=FALSE;
hThread[i]->ResumeThread();
}WaitForMultipleObjects(10,hThread,TRUE,INFINITE);
for(i=0;i<10;i++)
CloseHandle(hThread[i]->m_hThread);这种结构就ok了。
CloseHandle(hThread[i]->m_hThread);
而不删除hThread[i]是否会造成内存泄漏?
error C2664: 'WaitForMultipleObjects' : cannot convert parameter 2 from 'class CWinThread *[10]' to 'void *const * '我的hThread是这样定义的:CWinThread *hThread[10]
不行吗?
DWORD nCount, // number of handles in the handle array
CONST HANDLE *lpHandles, // pointer to the object-handle array
BOOL fWaitAll, // wait flag
DWORD dwMilliseconds // time-out interval in milliseconds
);
第二个参数是对象句柄数组而不是线程数组啊,应用用 HANDLE hThread[10]。
CloseHandle(hThread[i]->m_hThread);
后,如果此线程对象的内核计数值减1,如果减1后此值为0的话,系统会自动销毁此线程。
[email protected]
#include <TCHAR.h>
#include <windows.h>DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
TCHAR lpszTempBuf[20];
_stprintf(lpszTempBuf, "%d", (INT)lpParameter);
MessageBox(NULL, lpszTempBuf, NULL, MB_OK);
return 0;
}INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
HANDLE hThreadArray[10];
DWORD dwThreadID;
for (INT i=0; i<10; i++)
{
hThreadArray[i] = CreateThread(NULL, 0, ThreadProc, (LPVOID)i, 0, &dwThreadID);
if (NULL == hThreadArray[i])
{
MessageBox(NULL, TEXT("CreateThread Failed"), NULL, MB_OK);
ExitProcess(0);
}
}
WaitForMultipleObjects(10, hThreadArray, TRUE, INFINITE);
MessageBox(NULL, TEXT("ALL Thread exit."), NULL, MB_OK);
return 0;
}
只有前10个对话框都弹完了,才弹ALL Thread exit这个对话框.