SDL SDL_cond和SDL_mutex 原理

#include//#include "stdafx.h"#define __STDC_CONSTANT_MACROS#define SDL_MAIN_HANDLED#define SDL_THREAD_FINISH (SDL_USEREVENT+1)// 導(dǎo)入SDL庫#includeint i = 0;

SDL_mutex* data_lock;

SDL_cond * cond;

SDL_Thread* pthread1;

SDL_Thread* pthread2;

SDL_Event event;

int? SDLThread1(void *data)

{

int * pi = (int*)data;

for (;;)

{

if ((*pi) > 99)

{

SDL_Event event;

event.type = SDL_THREAD_FINISH;

SDL_PushEvent(&event);

break;

}

SDL_LockMutex(data_lock);

(*pi)++;

printf("This is SDL Thread1, Current i is [%d]\n", (*pi));

if ((*pi)>=40&&50>=(*pi))

{

SDL_CondSignal(cond);

}

SDL_UnlockMutex(data_lock);

SDL_Delay(10);

}

return 0;

}

int? SDLThread2(void *data)

{

int * pi = (int*)data;

for (;;)

{

if ((*pi) > 99)

{

SDL_Event event;

event.type = SDL_THREAD_FINISH;

SDL_PushEvent(&event);

break;

}

SDL_LockMutex(data_lock);

SDL_CondWait(cond, data_lock);

(*pi)++;

printf("This is SDL Thread2, Current i is [%d]\n", (*pi));

SDL_UnlockMutex(data_lock);

SDL_Delay(10);

}

return 0;

}

int main()

{

SDL_Init(SDL_INIT_EVERYTHING);

data_lock = SDL_CreateMutex();

cond = SDL_CreateCond();

pthread2 = SDL_CreateThread(SDLThread2, "Thread2", &i);

pthread1 = SDL_CreateThread(SDLThread1, "Thread1", &i);

for (;;)

{

SDL_WaitEvent(&event);

if (event.type == SDL_THREAD_FINISH)

break;

}

SDL_DestroyCond(cond);

SDL_DestroyMutex(data_lock);

SDL_Quit();

system("pause");

return 0;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容