FFMpeg中的SDL多线程使用

在FFmpeg的player中用到了SDL多线程,去读取音视频文件,解码音频,解码视频,解码字幕,读写队列;来看SDL_Thread,SDL_cond,SDL_mutex关系

SDL是一个c语言写的多媒体库,功能非常强大,并且支持音视频渲染,有的游戏中也有用到;

直接看代码注释:

// sdlthread.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "SDL.h"

SDL_cond* cond = SDL_CreateCond(); //创建一个条件信号量
SDL_mutex* mutex = SDL_CreateMutex();创建一个互斥锁

int index = 0;

//第一个线程函数
static int MyThread(void *data) {
	while (1) {
		SDL_LockMutex(mutex);//线程启动后,先加锁
		index++;
		std::cout << "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" << index << "\n";
		if (index % 2 == 0) {
			SDL_CondSignal(cond);//条件满足,发送信号,告诉第二个线程可以执行了;
			SDL_UnlockMutex(mutex);//解锁
			SDL_CondWait(cond, mutex);//继续等待其他线程发信号;
		}
	}

	return 0;
}

//第二个线程函数
static int YouThread(void* data) {
	while (1) {
		SDL_LockMutex(mutex);//线程启动后,先加锁
		index++;
		std::cout << "***********************************************" << index << "\n";
		if (index % 2 != 0) {
			SDL_CondSignal(cond);//条件满足,发送信号,告诉第二个线程可以执行了;
			SDL_UnlockMutex(mutex);//解锁
			SDL_CondWait(cond, mutex);//继续等待其他线程发信号;
		}
	}
	return 0;
}

#undef main;
int main()
{
	SDL_Thread *threaMy = SDL_CreateThread(MyThread,"MyThread", NULL);创建线程1
	SDL_Thread *threadYou = SDL_CreateThread(YouThread, "YouThread", NULL);创建线程2

	SDL_WaitThread(threaMy, 0);//启动线程1
	SDL_WaitThread(threadYou, 0);//启动线程2
	return 0;
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FFmpeg123

你的鼓励,我会更加努力输出

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值