一、线程同步之信号量
1、信号量相关操作函数
#include <semaphore.h>
//信号量初始化
int sem_init(sem_t *sem, int pshared, unsigned int value);
sem_destroy(&sem) // 销毁信号量
sem_post(&sem), // 等待信号量
sem_wait(&sem), // 释放信号量
————————————————————————————————————————
参数:
sem 信号量
pshared 0 -- 在线程之间共享信号量 非0 在不同进程之间共享信号量
value -- 信号量的初始值
代码案例
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
char buf[200] = {0};
sem_t sem;
unsigned int flag = 0;
// 子线程程序,作用是统计buf中的字符个数并打印
void *func(void *arg)
{
// 子线程首先应该有个循环
// 循环中阻塞在等待主线程激活的时候,子线程被激活后就去获取buf中的字符
// 长度,然后打印;完成后再次被阻塞
sem_wait(&sem);
while (flag == 0)
{
printf("本次输入了%d个字符\n", strlen(buf));
memset(buf, 0, sizeof(buf));
sem_wait(&sem);
}
pthread_exit(NULL);
}
int main(void)
{
int ret = -1;
pthread_t th = -1;
sem_init(&sem, 0, 0);
ret = pthread_create(&th, NULL, func, NULL);
if (ret != 0)
{
printf("pthread_create error.\n");
exit(-1);
}
printf("输入一个字符串,以回车结束\n");
while (scanf("%s", buf))
{
// 去比较用户输入的是不是end,如果是则退出,如果不是则继续
if (!strncmp(buf, "end", 3))
{
printf("程序结束\n");
flag = 1;
sem_post(&sem);
break;
}
// 主线程在收到用户收入的字符串,并且确认不是end后
// 就去发信号激活子线程来计数。
// 子线程被阻塞,主线程可以激活,这就是线程的同步问题。
// 信号量就可以用来实现这个线程同步
sem_post(&sem);
}
// 回收子线程
printf("等待回收子线程\n");
ret = pthread_join(th, NULL);
if (ret != 0)
{
printf("pthread_join error.\n");
exit(-1);
}
printf("子线程回收成功\n");
sem_destroy(&sem);
return 0;
}
二、线程同步之互斥锁
1、什么是互斥锁
(1)互斥锁又叫互斥量(mutex)
(2)相关函数:
pthread_mutex_init || pthread_mutex_destroy
pthread_mutex_lock || pthread_mutex_unlock
(3)互斥锁和信号量的关系:可以认为互斥锁是一种特殊的信号量
2、用互斥锁来实现上节代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
char buf[200] = {0};
pthread_mutex_t mutex;
unsigned int flag = 0;
// 子线程程序,作用是统计buf中的字符个数并打印
void *func(void *arg)
{
sleep(1);//防止子线程先拿到锁
while (flag == 0)
{
pthread_mutex_lock(&mutex);
printf("本次输入了%d个字符\n", strlen(buf));
memset(buf, 0, sizeof(buf));
pthread_mutex_unlock(&mutex);
sleep(1);//防止释放锁之后 再次被该线程拿到锁
}
pthread_exit(NULL);
}
int main(void)
{
int ret = -1;
pthread_t th = -1;
pthread_mutex_init(&mutex, NULL);
ret = pthread_create(&th, NULL, func, NULL);
if (ret != 0)
{
printf("pthread_create error.\n");
exit(-1);
}
printf("输入一个字符串,以回车结束\n");
while (1)
{
pthread_mutex_lock(&mutex);
scanf("%s", buf);
pthread_mutex_unlock(&mutex);
// 去比较用户输入的是不是end,如果是则退出,如果不是则继续
if (!strncmp(buf, "end", 3))
{
printf("程序结束\n");
flag = 1;
break;
}
sleep(1);//防止主线程释放锁之后又拿到锁
}
// 回收子线程
printf("等待回收子线程\n");
ret = pthread_join(th, NULL);
if (ret != 0)
{
printf("pthread_join error.\n");
exit(-1);
}
printf("子线程回收成功\n");
pthread_mutex_destroy(&mutex);
return 0;
}
注意
man 3 pthread_mutex_init
时提示找不到函数,说明你没有安装pthread相关的man手册。安装方法:1、虚拟机上网;2、sudo apt-get install manpages-posix-dev
上面的例程试图用锁来实现两个线程之间的同步问题,但是锁的作用不在于用于同步,所以主线程和子线程用了两个sleep函数来让出时间片给彼此去获取锁,但是这样会降低程序的整体运行效率,所以,
互斥锁的用处 在于保护一段代码被有效执行
,而不是实现线程间的同步。
三、线程同步之条件变量
1、什么是条件变量
与互斥锁不同,条件变量是用来
等待
而不是用来上锁的。条件变量用来自动阻塞一个线程,直到某特殊情况发生为止。通常条件变量和互斥锁同时使用。
2、相关函数
#include <pthread.h>
//销毁条件变量
int pthread_cond_destroy(pthread_cond_t *cond);
//初始化条件变量
int pthread_cond_init(pthread_cond_t *restrict cond,
const pthread_condattr_t *restrict attr);
//阻塞在条件变量上
int pthread_cond_wait(pthread_cond_t *cv,
pthread_mutex_t *mutex);
//解除在条件变量上的阻塞
int pthread_cond_signal(pthread_cond_t *cv);
//释放阻塞的所有线程
int pthread_cond_broadcast(pthread_cond_t *cv);
//阻塞直到指定时间
int pthread_cond_timedwait(pthread_cond_t *cv,
pthread_mutex_t *mp, const structtimespec * abstime);
代码例程
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
char buf[200] = {0};
pthread_mutex_t mutex;
pthread_cond_t cond;
unsigned int flag = 0;
// 子线程程序,作用是统计buf中的字符个数并打印
void *func(void *arg)
{
while (flag == 0)
{
pthread_mutex_lock(&mutex); //加锁和解锁是必须得加上得。
pthread_cond_wait(&cond, &mutex);
printf("本次输入了%d个字符\n", strlen(buf));
memset(buf, 0, sizeof(buf));
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(void)
{
int ret = -1;
pthread_t th = -1;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
ret = pthread_create(&th, NULL, func, NULL);
if (ret != 0)
{
printf("pthread_create error.\n");
exit(-1);
}
printf("输入一个字符串,以回车结束\n");
while (1)
{
//pthread_mutex_lock(&mutex);
scanf("%s", buf);
pthread_cond_signal(&cond);
//pthread_mutex_unlock(&mutex);
if (!strncmp(buf, "end", 3))
{
printf("程序结束\n");
flag = 1;
break;
}
}
// 回收子线程
printf("等待回收子线程\n");
ret = pthread_join(th, NULL);
if (ret != 0)
{
printf("pthread_join error.\n");
exit(-1);
}
printf("子线程回收成功\n");
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}