将请求的接口的动作放在互斥锁中进行

1: 
   2:  
   3: pthread_mutex_t mutex;
   4: pthread_t thread;
   5: sem_t * m_structSem;
   6:  
struct SimpleStructure
   8: {
int data;
float otherData;
  11: };
  12:  
void* arg)
  14: {
  15:     pthread_mutex_lock(&mutex);
  16:     SimpleStructure* args = (SimpleStructure*)arg;
  17:     
//todo...
  19:  
delete args;
  21:     pthread_mutex_unlock(&mutex);
  22:  
  23:     pthread_mutex_destroy(&mutex);
  24:     sem_destroy(m_structSem);
  25:  
return NULL;
  27: }
  28:  
  29: pthread_mutex_init(&mutex, NULL);
  30: m_structSem = sem_open(strThreadName.c_str(), O_CREAT, 0644, 0)
  31:  
//
new SimpleStructure();
  34: args->data = 1;
  35: args->otherData = 2.0f;
//
  37: pthread_create(&thread, NULL, &ThreadFunction, args);
  38:  
  39:  

相关文章:

  • 2021-04-22
  • 2021-12-06
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
猜你喜欢
  • 2021-07-18
  • 2022-12-23
  • 2021-07-21
  • 2022-01-17
  • 2022-03-06
  • 2021-07-20
  • 2021-11-05
相关资源
相似解决方案