多线程程序中,使用信号量需遵守以下几条规则:原子操作是指当多个线程试图修改同一个信号量的值时,各线程修改值的过程不会互相干扰。例如信号量的初始值为 1,此时有 2 个线程试图对信号量做“加 1”操作,则信号量的值最终一定是 3,而不会是其它的值。反之若不以“原子操作”方式修改信号量的值,那么最终的计算结果还可能是 2(两个线程同时读取到的值为 1,各自在其基础上加 1,得到的结果即为 2)。
<semaphore.h>
头文件中。例如,下面代码定义了名为 mySem 的信号量:
#include <semaphore.h> sem_t mySem;由此,我们就成功定义了一个 mySem 信号量。但要想使用它,还必须完成初始化操作。
int sem_init(sem_t *sem, int pshared, unsigned int value);各个参数的含义分别为:
int sem_post(sem_t* sem); int sem_wait(sem_t* sem); int sem_trywait(sem_t* sem); int sem_destroy(sem_t* sem);参数 sem 都表示要操作的目标信号量。各个函数的功能如下:
#include<stdio.h> #include<stdlib.h> #include<pthread.h> #include<semaphore.h> #include<unistd.h> //创建信号量 sem_t mySem; //设置总票数 int ticket_sum = 10; //模拟买票过程 void *sell_ticket(void *arg) { printf("当前线程ID:%u\n", pthread_self()); int i; int flag; for (i = 0; i < 10; i++) { //完成信号量"减 1"操作,否则暂停执行 flag = sem_wait(&mySem); if (flag == 0) { if (ticket_sum > 0) { sleep(1); printf("%u 卖第 %d 张票\n", pthread_self(), 10 - ticket_sum + 1); ticket_sum--; } //执行“加1”操作 sem_post(&mySem); sleep(1); } } return 0; } int main() { int flag; int i; void *ans; //创建 4 个线程 pthread_t tids[4]; //初始化信号量 flag = sem_init(&mySem, 0, 1); if (flag != 0) { printf("初始化信号量失败\n"); } for (i = 0; i < 4; i++) { flag = pthread_create(&tids[i], NULL, &sell_ticket, NULL); if (flag != 0) { printf("线程创建失败!"); return 0; } } sleep(10); for (i = 0; i < 4; i++) { flag = pthread_join(tids[i], &ans); if (flag != 0) { printf("tid=%d 等待失败!", tids[i]); return 0; } } //执行结束前,销毁信号量 sem_destroy(&mySem); return 0; }假设程序编写在 thread.c 文件中,执行过程如下:
[root@localhost ~]# gcc thread.c -o thread.exe -lpthread
[root@localhost ~]# ./thread.exe
当前线程ID:1199965952
当前线程ID:1189476096
当前线程ID:1168496384
当前线程ID:1178986240
1199965952 卖第 1 张票
1189476096 卖第 2 张票
1199965952 卖第 3 张票
1178986240 卖第 4 张票
1168496384 卖第 5 张票
1189476096 卖第 6 张票
1199965952 卖第 7 张票
1178986240 卖第 8 张票
1168496384 卖第 9 张票
1189476096 卖第 10 张票
#include <stdio.h> #include<pthread.h> #include<stdlib.h> #include<semaphore.h> //设置办理业务的人数 int num = 5; //创建信号量 sem_t sem; //模拟办理业务的过程 void *get_service(void *arg) { int id = *((int*)arg); //信号量成功“减 1”后才能继续执行 if (sem_wait(&sem) == 0) { printf("---customer%d 正在办理业务\n", id); sleep(2); printf("---customer%d 已办完业务\n", id); //信号量“加 1” sem_post(&sem); } return 0; } int main() { int flag,i,j; //创建 5 个线程代表 5 个人 pthread_t customer[5]; //初始化信号量 sem_init(&sem, 0, 2); for (i = 0; i < num; i++) { flag = pthread_create(&customer[i], NULL, get_service, &i); if (flag != 0) { printf("线程创建失败!\n"); return 0; } else { printf("customer%d 来办理业务\n",i); } sleep(1); } for (j = 0; j < num; j++) { flag = pthread_join(customer[j], NULL); if (flag != 0) { printf("tid=%d 等待失败!", customer[i]); return 0; } } sem_destroy(&sem); return 0; }假设程序编写在 thread.c 文件中,执行过程为:
[root@localhost ~]# gcc thread.c -o thread.exe -lpthread
[root@localhost ~]# ./thread.exe
customer0 来办理业务
---customer0 正在办理业务
customer1 来办理业务
---customer1 正在办理业务
---customer0 已办完业务
customer2 来办理业务
---customer2 正在办理业务
---customer1 已办完业务
customer3 来办理业务
---customer3 正在办理业务
---customer2 已办完业务
customer4 来办理业务
---customer4 正在办理业务
---customer3 已办完业务
---customer4 已办完业务
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有