pthread_mutex_destroy 信号量将阻塞!

本文提供了一个使用C语言实现的多线程程序示例,通过pthread库中的mutex和condition变量来演示如何在多线程环境中进行同步操作。程序展示了mutex的创建、锁定、解锁及销毁过程,并初始化了condition变量。
#include<stdio.h>
2 #include<stdlib.h>
3 #include <pthread.h>
4 pthread_mutex_t command_mutex_ = PTHREAD_MUTEX_INITIALIZER;
5 pthread_cond_t command_cond_ = PTHREAD_COND_INITIALIZER;
6 int main(){
7
8 pthread_mutex_destroy(&command_mutex_);
9 printf("--------0000\n");
10 pthread_cond_destroy(&command_cond_);
11 printf("--------1111\n");
12 pthread_mutex_lock(&command_mutex_);
13 printf("--------222\n");
14 // stop pthread_cond_signal(&command_cond_);在此阻塞
15 // stop pthread_cond_wait(&command_mutex_,&command_cond_);在此阻塞
16 printf("--------333\n");
17 pthread_mutex_unlock(&command_mutex_);
18
19 printf("--------4444\n");
20 printf("over\n");
21
22 }
#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
实验二 线程同步 一、实验目的 1.加深对并发执行、共享资源访问等概念的理解; 2.初步掌握多线程同步技术,如互斥锁、信号量、条件变量等。 二、实验题 生产者-消费者模型是多线程编程中一个经典的同步问题。在这个模型中,生产者线程负责生产产品并将其放入缓冲区,消费者线程负责从缓冲区中取出产品并消费。为了确保线程安全,需要使用互斥锁和条件变量来同步生产者和消费者的操作。 用C/C++模拟实现一个生产者—消费者模型。 要求:设计一个循环缓冲区用于存放生产者生产的产品(用一个整数表示产品,缓冲区用数组表示),设计两个线程,一个用于生产产品并放入缓冲区,一个从缓冲区中取出产品消费。注意对缓冲区互斥访问,使用C或C++中有关线程同步的函数实现。 提示: 可以使用互斥锁访问缓冲区。 1.先创建一个互斥锁的对象。互斥锁的类型是pthread_mutex_t,所以定义一个变量 pthread_mutex_t mutex; 就创建了一个互斥锁。 如果想使用这个互斥锁,我们还需要对这个互斥锁进行初始化,使用函数 pthread_mutex_init() 对互斥锁进行初始化操作。 方法为:pthread_mutex_init(&mutex, NULL); 2.使用互斥锁进行互斥操作。 在进行互斥操作的时候,应该先"拿到锁"再执行需要互斥的操作,否则可能会导致多个线程都需要访问的数据结果不一致。例如在一个线程在试图修改一个变量的时候,另一个线程也试图去修改这个变量,那就很可能让后修改的这个线程把前面线程所做的修改覆盖了。 (1)阻塞调用 pthread_mutex_lock(&mutex); 这个操作是阻塞调用。如果这个锁此时正在被其它线程占用,那么 pthread_mutex_lock() 调用会进入到这个锁的排队队列中,并会进入阻塞状态, 直到拿到锁之后才会返回。 (2)释放互斥锁。用完了互斥锁,一定要记得释放,不然下一个想要获得这个锁的线程,就会一直等待。使用 pthread_mutex_unlock() 即可释放互斥锁。 pthread_mutex_unlock(&mutex); 用条件变量判断缓冲区空或者满。 pthread库中用于线程同步的一种机制。在多线程程序中,条件变量通常与互斥锁(pthread_mutex_t)一起使用,以防止并发问题,如竞态条件和死锁。 (1)条件变量(pthread_cond_t)操作函数pthread_cond_init() 初始化一个条件变量。 int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) 如: pthread_cond_t not_full; pthread_cond_init(&not_full, NULL); (2) pthread_cond_wait 在等待条件变量时,阻塞线程并释放互斥锁。当被pthread_cond_signal或pthread_cond_broadcast唤醒时,线程重新获得锁,并继续执行。 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); (3)pthread_cond_signal 唤醒至少一个等待给定条件变量的线程。 int pthread_cond_signal(pthread_cond_t *cond); (4) pthread_cond_destroy 销毁条件变量。 int pthread_cond_destroy(pthread_cond_t *cond); 对实验进行总结200字
最新发布
10-29
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <semaphore.h> #include <pthread.h> sem_t apple; sem_t orange; sem_t empty; pthread_mutex_t mutex; void *Dad() { int nextp = 0; int i = 0; for(i = 0; i < 10; ++i) { int time = rand() % 10 + 1; //随机使程序睡眠0点几秒 usleep(time*100000); sem_wait(&empty); pthread_mutex_lock(&mutex); if(nextp == 0) { printf("爸爸放入了一个苹果\n"); } else { printf("爸爸放入了一个桔子\n"); } fflush(stdout); pthread_mutex_unlock(&mutex); //互斥锁解锁 if(nextp == 0) { sem_post(&apple); } else { sem_post(&orange); } nextp = 1 - nextp; } } void *Daughter() { while(1) { int time = rand() % 10 + 1; //随机使程序睡眠0点几秒 usleep(time * 100000); sem_wait(&apple); pthread_mutex_lock(&mutex); printf("女儿取了一个苹果\n") ; fflush(stdout); pthread_mutex_unlock(&mutex); //互斥锁解锁 sem_post(&empty); } } void *Son() { //请添加儿子线程的函数代码 } int main() { sem_init(&empty, 0, 5); //信号量初始化 sem_init(&orange, 0, 0); sem_init(&apple, 0, 0); pthread_mutex_init(&mutex, NULL); //互斥锁初始化 pthread_t dadid; pthread_t daughterid; pthread_t sonid; pthread_create(&dadid, NULL, Dad, NULL); //创建爸爸线程 pthread_create(&daughterid, NULL, Daughter, NULL); //创建女儿线程 pthread_create(&sonid, NULL, Son, NULL); //创建儿子线程 pthread_join(daughterid, NULL); pthread_join(sonid, NULL); sem_destroy(&empty); //信号量的销毁 sem_destroy(&apple); sem_destroy(&orange); pthread_mutex_destroy(&mutex); //互斥锁的销毁 return 0; }
09-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值