目的
线程创建后让线程等待外部信号再继续运行,外部判断线程1 到达一个状态,再通知线程2 执行,代码如下所示:
#include #include #include SDL_bool condition = SDL_FALSE; SDL_mutex *lock; SDL_cond *cond; #pragma comment(lib,"../lib/sdl2.lib") #pragma comment(lib,"../lib/sdl2main.lib") int test = 10; int threadA(void *arg) { SDL_LockMutex(lock); printf("in thread A\n"); test = 12; printf("test is %d\n",test); SDL_UnlockMutex(lock); return 0; } int threadB(void *arg) { SDL_LockMutex(lock); printf("in thread B\n"); test = 13; printf("test is %d\n", test); SDL_CondWait(cond, lock); test = 14; printf("test is %d\n", test); SDL_UnlockMutex(lock); return 0; } int main(int argv, char* argc[]) { lock = SDL_CreateMutex(); cond = SDL_CreateCond(); SDL_Thread * t1 = SDL_CreateThread(threadA, "threada", NULL); SDL_Thread * t2 = SDL_CreateThread(threadB, "threadb", NULL); SDL_LockMutex(lock); printf("send signal====================>\n"); //condition = SDL_TRUE; SDL_CondSignal(cond); SDL_UnlockMutex(lock); SDL_DestroyCond(cond); SDL_DestroyMutex(lock); SDL_Quit(); getchar(); return 0; }