溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SDL SDL_CondWait及SDL_CondSignal使用

發(fā)布時(shí)間:2020-09-30 18:44:54 來(lái)源:網(wǎng)絡(luò) 閱讀:5988 作者:fengyuzaitu 欄目:編程語(yǔ)言

頭文件
#include "SDL.h"
#include "SDL_thread.h"

int SDL_CondWait(SDL_cond *cond, SDL_mutex *mut);//等待條件變量
SDL_CondWait:釋放傳遞進(jìn)來(lái)的鎖,然后當(dāng)條件變量滿足的時(shí)候,等待其他的線程調(diào)用SDL_CondSignal或者SDL_CondBroadcast
改變條件變量cond,然后重新獲取鎖,然后返回。在調(diào)用該函數(shù)之前必須先調(diào)用SDL_LockMutex獲取
鎖。當(dāng)信號(hào)被觸發(fā)的時(shí)候就返回0,錯(cuò)誤的時(shí)候返回-1

簡(jiǎn)而言之,就是釋放鎖,讓其他的線程有機(jī)會(huì)獲取到鎖,滿足條件的時(shí)候,其他的線程會(huì)調(diào)用SDL_CondSignal或者SDL_CondBroadcast
釋放鎖,并且讓當(dāng)前的線程繼續(xù)執(zhí)行下去

int SDL_CondSignal(SDL_cond *cond);//喚醒正在等待條件變量發(fā)生改變的線程

代碼

typedef struct PacketQueue {
AVPacketList *first_pkt, *last_pkt;
int nb_packets;??????? //為包的總數(shù)
int size;??? //為所有包的大小
SDL_mutex *mutex; //互斥鎖
SDL_cond *cond; //條件變量
} PacketQueue;
我們自己創(chuàng)建的用于構(gòu)建Packet隊(duì)列的數(shù)據(jù)結(jié)構(gòu)


AVPacketList
A simple linked list for packets.
AVPacket pkt
AVPacketList * next

void packet_queue_init(PacketQueue *q)
{

??? memset(q,0,sizeof(PacketQueue));
??? q->mutex = SDL_CreateMutex();
??? q->cond=SDL_CreateCond();
}

對(duì)PacketQueue數(shù)據(jù)結(jié)構(gòu)進(jìn)行初始化

用于給PacketQueue數(shù)據(jù)結(jié)構(gòu)中填入包的函數(shù)
int??? packet_queue_put(PacketQueue??? *q,AVPacket??? *pkt)
{
??? AVPacketList??? *pkt1;
??? if(av_dup_packet(pkt)<0)
??? {
??????? return??? -1;??? //檢查是否為NULL,為NULL則自己填充,否則返回-1
??? }
??? pkt1??? =??? av_malloc(sizeof(AVPacketList));//給AVPacketList分配空間
? ?

??? if (!pkt1)
??????? return -1;
??? pkt1->pkt = *pkt;
??? pkt1->next = NULL;
??? SDL_LockMutex(q->mutex); //對(duì)PacketQueue進(jìn)行操作,先鎖定互斥變量
??? if (!q->last_pkt)
??????? q->first_pkt = pkt1;
??? else
??????? q->last_pkt->next = pkt1;
??? q->last_pkt = pkt1;
??? q->nb_packets++;
??? q->size += pkt1->pkt.size;
??? SDL_CondSignal(q->cond); //發(fā)送條件信號(hào),方便等待數(shù)據(jù)的地方喚醒
??? SDL_UnlockMutex(q->mutex); //解鎖
??? return 0;
}

接收數(shù)據(jù)
static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
{
??? AVPacketList??? *pkt1;
??? int??? ret;
??? SDL_LockMutex(q->mutex);//鎖定mutex
??? for(;;)
??? {
??????? if(quit)
??????? {
??????????? ret??? =-1;
??????????? break;
??????? }
??????? pkt1=q->first_pkt;
??????? if (pkt1) {
??????????? q->first_pkt = pkt1->next;
??????????? if (!q->first_pkt)
??????????????? q->last_pkt = NULL;
??????????? q->nb_packets--;
??????????? q->size -= pkt1->pkt.size;
??????????? *pkt = pkt1->pkt;
??????????? av_free(pkt1);
??????????? ret = 1;
??????????? break;
??????? }

//如果block設(shè)置為false,說(shuō)明不阻塞等待數(shù)據(jù)包,直接返回
??????? else if (!block) {
??????????? ret = 0;
??????????? break;
??????? }
??????? else {

//等待數(shù)據(jù)包進(jìn)入隊(duì)列,然后才返回
??????????? SDL_CondWait(q->cond, q->mutex);
??????? }
??? }
??? SDL_UnlockMutex(q->mutex);
??? return ret;
}
SDL_CondWait先檢測(cè)是否滿足條件,若不滿足,解鎖mutex,wait,
直至被SDL_CondSignal()函數(shù)或者SDL_CondBroadcast()函數(shù)通知,
則鎖定mutex并返回

參考
http://blog.sina.com.cn/s/blog_4ae178ba01010fns.html

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI