当前位置:网站首页>Sdl-2 thread logic
Sdl-2 thread logic
2022-06-11 07:26:00 【Nwafu_ zyccc】
SDL Threads
SDL Multithreading
SDL Thread creation :SDL_CreateThread
SDL Thread waiting :SDL_WaitThead
SDL The mutex :SDL_CreateMutex/SDL_DestroyMutex
SDL Lock mutex :SDL_LockMutex/SDL_UnlockMutex
SDL Condition variables, ( Semaphore ):SDL_CreateCond/SDL_DestoryCond
SDL Condition variables, ( Semaphore ) wait for / notice :SDL_CondWait/SDL_CondSingal
#include <stdio.h>
#include<stdlib.h>
#include <unistd.h> // for close
#include"SDL2/SDL.h"
#define FF_QUIT_EVENT (SDL_USEREVENT + 2) // User defined events
SDL_mutex *s_lock = NULL;
SDL_cond *s_cond = NULL;
int thread_work(void *arg){
SDL_LockMutex(s_lock);
printf(" <============thread_work sleep\n");
sleep(10);
printf(" <============thread_work wait\n");
// Release s_lock resources , And wait for signal. Why release s_lock Is to enable other threads to get s_lock
SDL_CondWait(s_cond, s_lock); // Another thread (1) send out signal and (2) Release lock after , This function exits
}
int main()
{
s_lock = SDL_CreateMutex();
s_cond = SDL_CreateCond();
SDL_Thread *t = SDL_CreateThread(thread_work,"thread_work",NULL);
if(!t)
{
printf(" %s",SDL_GetError);
return -1;
}
for(int i = 0;i< 2;i++)
{
sleep(2);
printf("main execute =====>\n");
}
printf("main SDL_LockMutex(s_lock) before ====================>\n");
SDL_LockMutex(s_lock); // Get the lock , But the child thread still holds the lock
printf("main ready send signal====================>\n");
printf("main SDL_CondSignal(s_cond) before ====================>\n");
printf("main SDL_LockMutex(s_lock) before ====================>\n");
SDL_LockMutex(s_lock); // Get the lock , But the child thread still holds the lock
printf("main ready send signal====================>\n");
printf("main SDL_CondSignal(s_cond) before ====================>\n");
SDL_CondSignal(s_cond); // Sending signal , Wakes up the waiting thread
printf("main SDL_CondSignal(s_cond) after ====================>\n");
sleep(10);
SDL_UnlockMutex(s_lock);// Release the lock , Let other threads get the lock
printf("main SDL_UnlockMutex(s_lock) after ====================>\n");
SDL_WaitThread(t, NULL);
SDL_DestroyMutex(s_lock);
SDL_DestroyCond(s_cond);
return 0;
}
Output results 
Result analysis
First main Function to create a child thread t And enter the corresponding function “thread_work”
The child thread gets the lock SDL_LockMutex(s_lock);
Then sleep 10 second

The main thread runs 4 Seconds later Get the lock But the lock is always occupied

Wake up after waiting for the signal You must get two conditions at the same time 1 Get the lock 2 Get the semaphore
Pictured 
边栏推荐
- 【CF#693 (Div. 3)】B. Fair Division
- 421. maximum XOR value of two numbers in the array
- gaussDB for redis和 redis的区别?
- [STL source code analysis] summary notes (7): ingenious deque
- Bat (batch processing) processing special symbols (exclamation point, percent sign), etc
- Interview question 02.06 Palindrome linked list
- CMAP of Matplotlib
- Education expert wangzhongze shared his experience for many years: family education is not a vassal
- 【Oracle 数据库】奶妈式教程day03 排序查询
- Mobile console Gobang (first draft of detailed design)
猜你喜欢

Gobang interface of mobile console (C language)

Crmeb/v4.4 Standard Version open version mall source code applet official account h5+app mall source code
![[STL source code analysis] summary notes (12): functors and adapters](/img/6d/a3a9cde2c8792579af7505c2226914.jpg)
[STL source code analysis] summary notes (12): functors and adapters

Education expert wangzhongze solves students' problems with one move

2、 User login and registration

一、SQLServer2008安裝(帶密碼)、創建數據庫、C#窗體項目測試
![[Oracle database] mammy tutorial day03 Sorting Query](/img/ea/24c9495a2ef4f1786f7b7852bde321.png)
[Oracle database] mammy tutorial day03 Sorting Query

big. Js-- use / instance

Multi thread review summary parsing volatile keyword

Biological sequence intelligent analysis platform blog (1)
随机推荐
Listen to the left width of the browser to calculate the distance
P5431 [template] multiplicative inverse 2
服务器调参实录
Matplotlib,设置坐标刻度大小,字体/设置图例大小及字体/设置纵横坐标名称及字体及大小
农房一体脚本的心得记录
Nodejs database (Part 2)
Several transaction modes of Seata
Library management system 1- project approval
First day of database
Crmeb/v4.4 Standard Version open version mall source code applet official account h5+app mall source code
[analysis of STL source code] summary notes (6): Design of iterator and magical traits
正则表达式匹配
337. house raiding III
模块化笔记
Console program for viewing BMP files
[STL source code analysis] summary note (2): overview of containers
Adventure of small X
Cartland number application
SQLZOO刷题记录-3
1300. the array closest to the target value after transforming the array and