您当前的位置: 首页 > 

qianbo_insist

暂无认证

  • 3浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SDL线程和互斥

qianbo_insist 发布时间:2022-03-06 15:47:54 ,浏览量:3

sdl

sdl 是一个集成了opengl, d3d, 声音等功能得lib,同时封装了线程和互斥等待等方法,在制作播放器得过程中,可以使用sdl 封装得方法来达到自己想要得过程,又有时间,定时器,图形,这些封装可以使用,所以播放器最好直接使用sdl很多封装好得功能,其好处在于: 1 支持c和c++ 2 方法简单 3 良好配合ffmpeg 4 将opengl和d3d等细节过滤,不用过多涉及

互斥sample

#include 

#include 
#include 

SDL_bool condition = SDL_FALSE;
SDL_mutex *lock;
SDL_cond *cond;

#ifdef WIN32
#pragma comment(lib,"../lib/sdl2.lib")
#pragma comment(lib,"../lib/sdl2main.lib")
#endif
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");
	SDL_CondSignal(cond);
	SDL_UnlockMutex(lock);
	SDL_DestroyCond(cond);
	SDL_DestroyMutex(lock);
	SDL_Quit();
	getchar();
	return 0;
}

结果如图在这里插入图片描述

关注
打赏
1663161521
查看更多评论
立即登录/注册

微信扫码登录

0.0819s