pthread_cond_write

本文通过一个具体的C语言程序示例介绍了如何使用互斥锁和条件变量实现两个线程之间的同步操作。其中一个线程负责计数并打印特定数字,另一个线程则等待特定条件信号并响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 1 #include<pthread.h>   
  2.   2 #include<unistd.h>   
  3.   3 #include<stdio.h>   
  4.   4 #include<stdlib.h>   
  5.   5    
  6.   6 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/  
  7.   7 pthread_cond_t  cond = PTHREAD_COND_INITIALIZER;//init cond   
  8.   8    
  9.   9 void *thread1(void*);   
  10.  10 void *thread2(void*);   
  11.  11    
  12.  12 int i = 1; //global   
  13.  13    
  14.  14 int main(void){   
  15.  15     pthread_t t_a;   
  16.  16     pthread_t t_b;//two thread   
  17.  17    
  18.  18     pthread_create(&t_a,NULL,thread2,(void*)NULL);   
  19.  19     pthread_create(&t_b,NULL,thread1,(void*)NULL);//Create thread   
  20.  20    
  21.  21     pthread_join(t_b,NULL);//wait a_b thread end   
  22.  22     pthread_mutex_destroy(&mutex);   
  23.  23     pthread_cond_destroy(&cond);   
  24. 24     exit(0);   
  25.  25 }   
  26.  26    
  27.  27 void *thread1(void *junk){   
  28.  28     for(i = 1;i<= 9; i++){   
  29.  29         pthread_mutex_lock(&mutex); //互斥锁   
  30.  30         printf("call thread1 \n");   
  31.  31         if(i%3 == 0)   
  32.  32             pthread_cond_signal(&cond); //send sianal to t_b   
  33.  33         else  
  34.  34             printf("thread1: %d\n",i);   
  35.  35         pthread_mutex_unlock(&mutex);   
  36.  36         sleep(1);   
  37.  37     }   
  38.  38 }   
  39.  39    
  40.  40 void *thread2(void*junk){   
  41.  41     while(i < 9)   
  42.  42     {   
  43.  43         pthread_mutex_lock(&mutex);   
  44.  44         printf("call thread2 \n");   
  45.  45         if(i%3 != 0)   
  46.  46             pthread_cond_wait(&cond,&mutex); //wait   
  47.   47         printf("thread2: %d\n",i);   
  48.  48         pthread_mutex_unlock(&mutex);   
  49.  49         sleep(1);   
  50.  50     }   
  51.  51 }   
#include <stdio.h> #include <pthread.h> #include <errno.h> #include <signal.h> #include <unistd.h> #include <sys/shm.h> #include <sys/sem.h> #include <sys/ipc.h> #include "data_global.h" extern pthread_cond_t cond_sqlite; extern pthread_cond_t cond_analysis; extern pthread_cond_t cond_client_send; extern pthread_cond_t cond_uart_cmd; extern pthread_cond_t cond_client_request; extern pthread_cond_t cond_infrared; extern pthread_cond_t cond_buzzer; extern pthread_cond_t cond_led; extern pthread_cond_t cond_camera; extern pthread_cond_t cond_sms; extern pthread_cond_t cond_refresh; extern pthread_cond_t cond_refresh_updata; extern pthread_mutex_t mutex_slinklist; extern pthread_mutex_t mutex_sqlite; extern pthread_mutex_t mutex_analysis; extern pthread_mutex_t mutex_client_send; extern pthread_mutex_t mutex_client_receive; extern pthread_mutex_t mutex_uart_cmd; extern pthread_mutex_t mutex_client_request; extern pthread_mutex_t mutex_infrared; extern pthread_mutex_t mutex_buzzer; extern pthread_mutex_t mutex_led; extern pthread_mutex_t mutex_camera; extern pthread_mutex_t mutex_sms; extern pthread_mutex_t mutex_refresh; extern pthread_mutex_t mutex_refresh_updata; extern pthread_mutex_t mutex_global; extern pthread_mutex_t mutex_linklist; extern int dev_infrared_fd; extern int dev_buzzer_fd; extern int dev_led_fd; extern int dev_camera_fd; extern int dev_sms_fd; extern int dev_uart_fd; extern int msgid; extern int shmid; extern int semid; extern void Create_table (); extern struct env_info_clien_addr all_info_RT; pthread_t id_sqlite, id_analysis, id_transfer, id_client_send, id_client_receive, id_uart_cmd, id_client_request, id_infrared, id_buzzer, id_led, id_camera, id_sms, id_refresh; void ReleaseResource (int signo) { pthread_mutex_destroy (&mutex_linklist); pthread_mutex_destroy (&mutex_global); pthread_mutex_destroy (&mutex_refresh_updata); pthread_mutex_destroy (&mutex_refresh); pthread_mutex_destroy (&mutex_sms); pthread_mutex_destroy (&mutex_camera); pthread_mutex_destroy (&mutex_led); pthread_mutex_destroy (&mutex_buzzer); pthread_mutex_destroy (&mutex_infrared); pthread_mutex_destroy (&mutex_client_request); pthread_mutex_destroy (&mutex_uart_cmd); pthread_mutex_destroy (&mutex_analysis); pthread_mutex_destroy (&mutex_client_send); pthread_mutex_destroy (&mutex_client_receive); pthread_mutex_destroy (&mutex_sqlite); pthread_mutex_destroy (&mutex_slinklist); pthread_cond_destroy (&cond_client_send); pthread_cond_destroy (&cond_refresh_updata); pthread_cond_destroy (&cond_refresh); pthread_cond_destroy (&cond_sms); pthread_cond_destroy (&cond_camera); pthread_cond_destroy (&cond_led); pthread_cond_destroy (&cond_buzzer); pthread_cond_destroy (&cond_infrared); pthread_cond_destroy (&cond_client_request); pthread_cond_destroy (&cond_uart_cmd); pthread_cond_destroy (&cond_analysis); pthread_cond_destroy (&cond_sqlite); msgctl (msgid, IPC_RMID, NULL); shmctl (shmid, IPC_RMID, NULL); pthread_cancel (id_refresh); pthread_cancel (id_sms); pthread_cancel (id_camera); pthread_cancel (id_led); pthread_cancel (id_buzzer); pthread_cancel (id_infrared); pthread_cancel (id_client_request); pthread_cancel (id_uart_cmd); pthread_cancel (id_transfer); pthread_cancel (id_analysis); pthread_cancel (id_client_send); pthread_cancel (id_client_receive); pthread_cancel (id_sqlite); close (dev_camera_fd); close (dev_led_fd); close (dev_buzzer_fd); close (dev_infrared_fd); close (dev_sms_fd); close (dev_uart_fd); printf ("All quit\n"); exit(0); } void setLimit (int sto_no, float temMAX, float temMIN, float humMAX, float humMIN, float illMAX, float illMIN) { if (sto_no >= 0 && (sto_no <=STORAGE_NUM - 1)) { all_info_RT.storage_no[sto_no].temperatureMAX = temMAX; all_info_RT.storage_no[sto_no].temperatureMIN = temMIN; all_info_RT.storage_no[sto_no].humidityMAX = humMAX; all_info_RT.storage_no[sto_no].humidityMIN = humMIN; all_info_RT.storage_no[sto_no].illuminationMAX = illMAX; all_info_RT.storage_no[sto_no].illuminationMIN = illMIN; } } int main(int argc, char **argv) { #if 1 / Create_table (); // setLimit (1, 50, 5, 50, 10, 500, 10); setLimit (2, 50, 5, 50, 10, 500, 10); pthread_mutex_init (&mutex_slinklist, NULL);// pthread_mutex_init (&mutex_sqlite, NULL); pthread_mutex_init (&mutex_analysis, NULL); pthread_mutex_init (&mutex_uart_cmd, NULL); pthread_mutex_init (&mutex_client_request, NULL); pthread_mutex_init (&mutex_infrared, NULL); pthread_mutex_init (&mutex_buzzer, NULL); pthread_mutex_init (&mutex_led, NULL); pthread_mutex_init (&mutex_camera, NULL); pthread_mutex_init (&mutex_sms, NULL); pthread_mutex_init (&mutex_refresh, NULL); pthread_mutex_init (&mutex_refresh_updata, NULL); pthread_mutex_init (&mutex_global, NULL); pthread_mutex_init (&mutex_linklist, NULL); pthread_mutex_init (&mutex_client_send, NULL); pthread_cond_init (&cond_client_send, NULL); pthread_cond_init (&cond_sqlite, NULL); pthread_cond_init (&cond_analysis, NULL); pthread_cond_init (&cond_uart_cmd, NULL); pthread_cond_init (&cond_client_request, NULL); pthread_cond_init (&cond_infrared, NULL); pthread_cond_init (&cond_buzzer, NULL); pthread_cond_init (&cond_led, NULL); pthread_cond_init (&cond_camera, NULL); pthread_cond_init (&cond_sms, NULL); pthread_cond_init (&cond_refresh, NULL); pthread_cond_init (&cond_refresh_updata, NULL); //signal (SIGINT, ReleaseResource); // pthread_create (&id_sqlite, 0, pthread_sqlite, NULL); sleep (1); pthread_create (&id_analysis, 0, pthread_analysis, NULL); pthread_create (&id_client_send, 0, pthread_client_send, NULL); pthread_create (&id_transfer, 0, pthread_transfer, NULL); sleep (1); pthread_create (&id_uart_cmd, 0, pthread_uart_cmd, NULL); pthread_create (&id_client_request, 0, pthread_client_request, NULL); pthread_create (&id_infrared, 0, pthread_infrared, NULL); pthread_create (&id_buzzer, 0, pthread_buzzer, NULL); pthread_create (&id_led, 0, pthread_led, NULL); pthread_create (&id_camera, 0, pthread_camera, NULL); pthread_create (&id_sms, 0, pthread_sms, NULL); pthread_create (&id_refresh, 0, pthread_refresh, NULL); // pthread_join (id_sqlite, NULL); pthread_join (id_client_send, NULL); printf ("g1\n"); pthread_join (id_analysis, NULL); printf ("g2\n"); pthread_join (id_transfer, NULL); printf ("g3\n"); pthread_join (id_uart_cmd, NULL); printf ("g4\n"); pthread_join (id_client_request, NULL); printf ("g5\n"); pthread_join (id_infrared, NULL); printf ("g6\n"); pthread_join (id_buzzer, NULL); printf ("g7\n"); pthread_join (id_led, NULL); printf ("g8\n"); pthread_join (id_camera, NULL); printf ("g9\n"); pthread_join (id_sms, NULL); printf ("g10\n"); pthread_join (id_refresh, NULL); printf ("g11\n"); return 0; }
最新发布
07-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值