其实和 C 的线程使用差不多,只是又封装了一层而已。
#include <stdio.h>
#include <SDL2/SDL.h>
SDL_mutex* mutex;
SDL_cond* cond;
int sdl_work_func(void* v) {
SDL_LockMutex(mutex);
printf("sdl_work_func lock mutex\n");
printf("sdl_work_func begin wait condition\n");
SDL_CondWait(cond, mutex);
printf("sdl_work_fun get condtion\n");
SDL_UnlockMutex(mutex);
return 0;
}
int main(int argc, char* argv[]) {
mutex = SDL_CreateMutex();
cond = SDL_CreateCond();
SDL_Thread* t = SDL_CreateThread(sdl_work_func, "func", NULL);
for(int i = 0; i < 5; i++) {
printf("wait %d\n", i);
}
SDL_LockMutex(mutex);
printf("main get mutex\n");
SDL_CondSignal(cond);
printf("main send signal\n");
for(int i = 0; i < 50000; i++) {
}
SDL_UnlockMutex(mutex);
printf("main unlock mutex\n");
SDL_WaitThread(t, NULL);
SDL_DestroyMutex(mutex);
SDL_DestroyCond(cond);
return 0;
}
评论