信号量
信号量可以用于进程之间的同步与互斥也可用于线程的同步与互斥,其本质就是操作系统中的P/V原语,通过P操作让进程挂起,V操作让挂起的进程继续运行
要用到的头文件:#include<semaphore.h>
(以下函数成功返回0,失败返回-1。)
初始化
int sem_init __P ((sem_t *_sem, int pshared, unsigned int value));
sem为指向信号量结构的一个指针;pshared不为0时此信号量在进程间共享,否则只能为当前进程的所有线程共享;value给出了信号量的初始值。
信号量P操作
增加信号量的值。当有线程阻塞在这个信号量上时,调用这个函数会使其中的一个线程不在阻塞
用来阻塞当前线程直到信号量sem的值大于0,解除阻塞后将sem的值减一,表明公共资源经使用后减少
int sem_wait(sem_t * sem);int sem_post(sem_t * sem);
信号量V操作
int sem_post(sem_t *sem)
信号量销毁
不使用信号量时,销毁信号量以释放其所占用资源
int sem_destroy(sem_t *sem)
线程互斥
创建两个线程,线程1对全局变量(初始值1)加1并显示出来,线程2对全局变量的值加倍并打印出来。通过信号量,可以让两个线程不会同时访问操作全局变量,实现线程的互斥,但无法控制线程的执行顺序
#include<pthread.h>
#include<stdio.h>
#include<semaphore.h>
#include<stdlib.h>
#include<errno.h>
#define MAX 100
static sem_t sem;
static int global=1;
void *t1_exe(void*arg){
while (global<MAX)
{
sem_wait(&sem);
printf("In thread 1 before increment global=%d\n",global);
global++;
printf("In thread 1 after increment global=%d\n",global);
sem_post(&sem);
sleep(5);
}
}
void *t2_exe(void*arg){
while (global<MAX)
{
sem_wait(&sem);
printf("In thread 2 before increment global=%d\n",global);
global*=2;
printf("In thread 2 after increment global=%d\n",global);
sem_post(&sem);
sleep(6);
}
}
void main(){
pthread_t pid1,pid2;
int error1,error2;
if(sem_init(&sem,0,1)==-1){
perror("sem initialized failed");
exit(0);
}
error1=pthread_create(&pid1,NULL,t1_exe,NULL);
error2=pthread_create(&pid2,NULL,t2_exe,NULL);
if(error1!=0||error2!=0){
printf("pthread_create failed");
return;
}
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
sem_destroy(&sem);
return;
}
线程同步
利用信号量,将上例线程执行顺序设置为线程1,线程2交替执行,实现线程的同步
#include<pthread.h>
#include<stdio.h>
#include<semaphore.h>
#include<stdlib.h>
#include<errno.h>
#define MAX 100
static sem_t sem1,sem2;
static int global=1;
void *t1_exe(void*arg){
while (global<MAX)
{
sem_wait(&sem1);
printf("In thread 1 before increment global=%d\n",global);
global++;
printf("In thread 1 after increment global=%d\n",global);
sem_post(&sem2);
sleep(5);
}
}
void *t2_exe(void*arg){
while (global<MAX)
{d
sem_wait(&sem2);
printf("In thread 2 before increment global=%d\n",global);
global*=2;
printf("In thread 2 after increment global=%d\n",global);
sem_post(&sem1);
sleep(6);
}
}
void main(){
pthread_t pid1,pid2;
int error1,error2;
if(sem_init(&sem1,0,1)||sem_init(&sem2,0,0)==-1){
perror("sem initialized failed");
exit(0);
}
error1=pthread_create(&pid1,NULL,t1_exe,NULL);
error2=pthread_create(&pid2,NULL,t2_exe,NULL);
if(error1!=0||error2!=0){
printf("pthread_create failed");
return;
}
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
return;
}