SDL SDL_cond和SDL_mutex 原理

#include//#include "stdafx.h"#define __STDC_CONSTANT_MACROS#define SDL_MAIN_HANDLED#define SDL_THREAD_FINISH (SDL_USEREVENT+1)// 导入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;

}

SDL SDL_cond和SDL_mutex 原理_第1张图片

你可能感兴趣的:(SDL SDL_cond和SDL_mutex 原理)