SDL 多线程创建同步
Posted qianbo_insist
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SDL 多线程创建同步相关的知识,希望对你有一定的参考价值。
目的
线程创建后让线程等待外部信号再继续运行,外部判断线程1 到达一个状态,再通知线程2 执行,代码如下所示:
#include <SDL2/SDL.h>
#include <stdio.h>
#include <Windows.h>
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;
以上是关于SDL 多线程创建同步的主要内容,如果未能解决你的问题,请参考以下文章