信号量可以使用于进程或线程间的同步,所以函数名没有带pthread。信号量相当于初始化值为N的互斥量(锁)的升级版,N代表可以同时访问共享数据区的线程数,而互斥锁某个时刻仅支持一个线程访问。
使用信号量时,每次sem_post之后相当于一次++操作,加到N时再执行sem_post就会阻塞;sem_wait相当于 - - 操作,减到0时再执行sem_wait就会阻塞。
初始化
(注意:先初始化信号量,再创建线程)
#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
参数 | 说明 |
---|---|
sem | 信号量 |
pshared | 0:线程使用;非0:任何可以访问到这个信号量的进程都可以使用 |
value | 信号量的初始值。 |
销毁一个指定的信号量:
int sem_destroy(sem_t *sem);
对指定信号量的操作
int sem_post(sem_t *sem); // 类比于互斥锁的unlock解锁, ++操作,到N时阻塞
int sem_wait(sem_t *sem); // 类比于互斥锁的lock加锁,--操作,到0时阻塞
int sem_trywait(sem_t *sem);
以上函数返回值均为:成功0,失败-1。
例子
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <string.h>
char buf[256];
sem_t sem1;
sem_t sem2;
void *threadfunc1(void *pvoid)
{
while(1)
{
printf("Please input a word: ");
scanf("%s", buf);
sem_post(&sem1);
if(0 == strcmp(buf, "quit"))
pthread_exit(NULL);
sem_wait(&sem2);
}
return NULL;
}
void *threadfunc2(void *pvoid)
{
while(1)
{
sem_wait(&sem1);
if(0 == strcmp(buf, "quit"))
pthread_exit(NULL);
printf("You had input: %s\n", buf);
memset(buf, 0, 256);
sem_post(&sem2);
}
return NULL;
}
int main()
{
pthread_t tid1;
pthread_t tid2;
sem_init(&sem1, 0, 1);
sem_init(&sem2, 0, 1);
sem_wait(&sem1);
sem_wait(&sem2);
pthread_create(&tid1, NULL, &threadfunc1, NULL);
pthread_create(&tid2, NULL, &threadfunc2, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
printf("End!\n");
sem_destroy(&sem1);
sem_destroy(&sem2);
return 0;
}
输出结果:
Please input a word: abcdefg
You had input: abcdefg
Please input a word: hijklmn
You had input: hijklmn
Please input a word: quit
End!