您当前的位置: 首页 > 

SDL 多线程创建同步

发布时间:2022-08-26 22:10:32 ,浏览量:5

目的

线程创建后让线程等待外部信号再继续运行,外部判断线程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; } 

在这里插入图片描述

关注
打赏
1688896170
查看更多评论

暂无认证

  • 5浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0564s