怎么等待一个线程结束
在cocoa下不知道怎么实现等待一个线程结束.
超级怀念windows的WaitForSingle函数.
下面window下一个包装,有谁可以翻译成mac的功能?
#pragma once
#include <windows.h>
#define INVALID_PRIORITY -88888888
class thread {
public:
thread(): hThread(0) {
hRequestEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
}
virtual ~thread() {
stop();
CloseHandle(hRequestEvent);
}
//set thread name, that's usually invoked in the OnExecute of subclass
void named(const char* name) { //fixed by rene/2012-05-21
typedef struct tagTHREADNAME_INFO {
DWORD dwType; //must be 0x1000
LPCSTR szName; //pointer to name (in user addr space)
DWORD dwThreadID; //thread ID (-1=caller thread)
DWORD dwFlags; //reserved for future use, must be zero
} THREADNAME_INFO;
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = name;
info.dwThreadID = -1;
info.dwFlags = 0;
__try {
RaiseException(0x406D1388, 0, sizeof(info) / sizeof(DWORD), (const DWORD*)&info);
}
__except(EXCEPTION_CONTINUE_EXECUTION) {}
}
bool start() {
stop();
ResetEvent(hRequestEvent);
DWORD dwThreadId;
hThread = CreateThread(NULL, 0, ThreadProc, this, 0, &dwThreadId);
return true;
}
//stop thread safely, suggest.
bool stop() { //fixed by rene/2012-04-24
SetEvent(hRequestEvent);
join();
if(hThread)
CloseHandle(hThread);
hThread = NULL;
return true;
}
//terminate thread violently, no suggest.
bool exit() { //fixed by rene/2012-05-22
TerminateThread(hThread, 0); //ExitThread(0);
if(hThread)
CloseHandle(hThread);
hThread = NULL;
return true;
}
bool join() {
DWORD exitCode;
if(GetExitCodeThread(hThread, &exitCode) && exitCode == STILL_ACTIVE) {
DWORD dwRet = 0;
MSG msg;
while(TRUE) {
//using MsgWaitForMultipleObjects instead of WaitForSingleObject.
//because of invoking SendMessage in thread.
//otherwise, it will block the thread.
dwRet = MsgWaitForMultipleObjects(1, &hThread, FALSE, INFINITE, QS_ALLINPUT); //fixed by rene/2012-04-05
if(dwRet == WAIT_OBJECT_0 + 1) {
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { //fixed by rene/2012-04-05
TranslateMessage(&msg);
DispatchMessage(&msg);
}
continue;
}
break;
}
}
return true;
}
bool pause() {
//if(hThread)
SuspendThread(hThread);
return true;
}
bool resume() {
//if(hThread)
ResumeThread(hThread);
return true;
}
/******************************************************
set the thread priority as below:
THREAD_PRIORITY_TIME_CRITICAL=15. Indicates 3 points above normal priority.
THREAD_PRIORITY_HIGHEST=2. Indicates 2 points above normal priority.
THREAD_PRIORITY_ABOVE_NORMAL=1. Indicates 1 point above normal priority.
THREAD_PRIORITY_NORMAL=0. Indicates normal priority.
THREAD_PRIORITY_BELOW_NORMAL=-1. Indicates 1 point below normal priority.
THREAD_PRIORITY_LOWEST=-2. Indicates 2 points below normal priority.
//THREAD_PRIORITY_ABOVE_IDLE=. Indicates 3 points below normal priority.
THREAD_PRIORITY_IDLE=-15. Indicates 4 points below normal priority.
******************************************************/
bool setPriority(int priority) {
//if(hThread)
SetThreadPriority(hThread, priority);
return true;
}
int getPriority() {
if(!hThread) return INVALID_PRIORITY;
return GetThreadPriority(hThread);
}
protected:
HANDLE hRequestEvent;
virtual void OnExecute() = 0;
private:
HANDLE hThread;
static DWORD WINAPI ThreadProc(LPVOID lParam) {
thread* th = (thread*)lParam;
if(th)
th->OnExecute();
return 0;
}
};
class CThreadEx: public thread {
public:
CThreadEx(HWND wnd): m_wnd(wnd) {}
private:
HWND m_wnd;
void OnExecute() {
for(int i = 0; i < 20; i++) {
if(WaitForSingleObject(hRequestEvent, 0) != WAIT_TIMEOUT)
goto exit;
char buf[200] = {0};
sprintf(buf, "on thread msg---%d times", i);
::SendMessage(m_wnd, WM_THREAD_MSG, 0, (LPARAM)buf);
Sleep(1000);
}
exit:
return;
}
};
void test() {
thread* th = new CThreadEx(GetSafeHwnd());
th->start();
th->stop();
th->pause();
th->resume();
//some times, stop and free the thread
delete th;
}
2.Thread 有一个这样的方法
NSThread *a = nil;
[a performSelectorOnMainThread:<#(SEL)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>]
这个的意思是,Thread a的代码执行是加在 mainThrad runLoop的末端,当然也可以是
[a performSelector:<#(SEL)#> onThread:<#(NSThread *)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>]
3.发送通知。更新UI最好是返回到 mainThread的时候发送。