【windows线程】线程资源创建与释放

    xiaoxiao2021-12-14  18

    接口AfxBeginThread CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL ); Tips:1 创建线程缺省情况下,创建线程执行线程任务,任务完成后,创建线程的资源句柄CWinthread资源均会被释放。2 不需要调用CloseHandle CWinthread->m_hThread,也不需要delete CWinthread*,否则会出现异常3 如果设置m_bAutoDelete=False,那么需要delete CWinthread*释放资源否则句柄和内存泄露,释放是不需调用CloseHandle。 异常信息:处最可能的异常: 0xC0000008: An invalid handle was specified线程退出退出接口AfxEndThread 该接口只能在要退出线程中调用。获取线程退出的的GetExitCodeThread 2种方法。1、设置 m_bAutoDelete 不释放,其他线程调用该线程后释放线程2、The safest way to do this is to pass CREATE_SUSPENDED to AfxBeginThread, store the handle, and then resume the thread by calling ResumeThread ::DuplicateHandle执行线程前拷贝handle。 注意最后需要CloseHandle释放句柄资源。 否则会造成资源泄露。
    转载请注明原文地址: https://ju.6miu.com/read-964187.html

    最新回复(0)