Files
sdl-thread/main.c

55 lines
1.3 KiB
C
Raw Permalink Normal View History

2025-09-21 22:25:51 +08:00
#include <stdio.h>
#include <SDL2/SDL.h>
SDL_mutex *s_lock = NULL;
SDL_cond *s_cond = NULL;
int work_thread(void *arg) {
SDL_LockMutex(s_lock);
printf("<=========work_thread sleep=========>\n");
sleep(10);
printf("<=========work_thread wait condition=========>\n");
SDL_CondWait(s_cond, s_lock);
printf("<=========work_thread receive signal=========>\n");
printf("<=========work_thread end=========>\n");
SDL_UnlockMutex(s_lock);
return 0;
}
#undef main
int main()
{
s_lock = SDL_CreateMutex();
s_cond = SDL_CreateCond();
SDL_Thread *t = SDL_CreateThread(work_thread, "work_thread", NULL);
if (!t) {
printf("SDL_CreateThread() failed\n");
return -1;
}
for (int i = 0; i < 2; i++) {
sleep(2);
printf("main execute\n");
}
printf("SDL_LockMutex(s_lock) before\n");
SDL_LockMutex(s_lock);
printf("SDL_LockMutex(s_lock) after\n");
printf("SDL_CondSignal(s_cond) before\n");
SDL_CondSignal(s_cond);
printf("SDL_CondSignal(s_cond) after\n");
sleep(10);
printf("SDL_UnlockMutex(s_lock) before\n");
SDL_UnlockMutex(s_lock);
printf("SDL_UnlockMutex(s_lock) after\n");
SDL_WaitThread(t, NULL);
SDL_DestroyMutex(s_lock);
SDL_DestroyCond(s_cond);
return 0;
}