当前位置:网站首页>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
 Insert picture description here

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);
 Insert picture description here
Then sleep 10 second

 Insert picture description here
The main thread runs 4 Seconds later Get the lock But the lock is always occupied

 Insert picture description here
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
 Insert picture description here

原网站

版权声明
本文为[Nwafu_ zyccc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020521358691.html