一、信号量
1、和进程类似,信号量就是特殊的计数器。
可以参考进程间通信——信号量
2、多线程的信号量操作是在全局进行定义
信号量类型sem_t
3、初始化
int sem_init(sem_t *sem, int shared, int val);
4、P操作
int sem_wait(sem_t *sem);
5、V操作
int sem_post(sem_t *sem);
6、销毁
int sem_destory(sem_t *sem);
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<assert.h>
#include <pthread.h>
#include <semaphore.h>
sem_t sem1;
sem_t sem2;
char buff[128] = {0};
void * fun(void *arg)
{
while(1)
{
sem_wait(&sem1);
if(strncmp(buff, "end", 3) == 0)
{
break;
}
printf("fun: %s", buff);
memset(buff, 0, 128);
sem_post(&sem2); // 函数线程唤醒主线程的
}
}
int main()
{
sem_init(&sem1, 0, 0);
sem_init(&sem2, 0, 1);
pthread_t id;
int res = pthread_create(&id, NULL, fun, NULL);
assert(0 == res);
while(1)
{
sem_wait(&sem2);
printf("input: ");
fgets(buff, 127, stdin);
sem_post(&sem1); // 主线程唤醒函数线程
if(strncmp(buff, "end", 3) == 0)
{
break;
}
}
pthread_join(id, NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
exit(0);
}
二、读写锁
1、读写锁实际是一种特殊的自旋锁,它把对共享资源的访问者划分成读者和写者,读者只对共享资源进行读访问,写者则需要对共享资源进行写操作。
2、当读写锁是写加锁状态时, 在这个锁被解锁之前, 所有试图对这个锁加锁的线程都会被阻塞.
3、当读写锁在读加锁状态时, 所有试图以读模式对它进行加锁的线程都可以得到访问权, 但是如果线程希望以写模式对此锁进行加锁, 它必须直到所有的线程释放锁.
4、初始化读写锁
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);
attr:读写锁属性,通常使用默认属性,传NULL即可。
5、销毁读写锁
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
6、请求读锁
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
7、请求写锁
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
8、解锁
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
9、非阻塞请求读锁
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
10、非阻塞请求写锁
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
三、条件变量
1、条件变量是线程中的东西,就是等待某一条件的发生。
2、条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起;另一个线程使"条件成立"(给出条件成立信号)。为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起。
3、初始化一个条件变量
int pthread_cond_init(pthread_cond_t *cond,pthread_condattr_t *cond_attr);
- attr表条件变量属性,通常为默认值,传NULL即可
- 变量cond只有两种取值1、0。
4、阻塞等待一个条件变量
int pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex);
- 函数作用:
- 阻塞等待条件变量cond(参1)满足
- 释放已掌握的互斥锁(解锁互斥量)相当于
pthread_mutex_unlock(&mutex);
- 当被唤醒,函数返回时,解除阻塞并重新申请获取互斥锁
pthread_mutex_lock(&mutex);
5、限时等待一个条件变量
int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex *mutex,const timespec *abstime);
6、销毁
int pthread_cond_destroy(pthread_cond_t *cond);
7、唤醒至少一个阻塞在条件变量上的线程
int pthread_cond_signal(pthread_cond_t *cond);
8、唤醒全部阻塞在条件变量上的线程
int pthread_cond_broadcast(pthread_cond_t *cond);
9、代码实例
char buff[128] = {0};
pthread_cond_t cond; // 条件变量
pthread_mutex_t mutex; // 互斥锁
// thread1 -->arg
// thread2 -->arg
void *fun(void *arg)
{
char *s = (char*)arg;
while(1)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex); // wait是要阻塞的,直到有线程对cond进行signal或者broadcast
pthread_mutex_unlock(&mutex);
printf("%s: %s", s, buff);
if(strncmp(buff, "end", 3) == 0)
{
break;
}
}
}
int main()
{
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_t id[2];
int res = pthread_create(&id[0], NULL, fun, "thread1");
assert(res == 0);
res = pthread_create(&id[1], NULL, fun, "thread2");
assert(res == 0);
while(1)
{
printf("input: ");
fgets(buff, 127, stdin);
if(strncmp(buff, "end", 3) == 0) // 唤醒在条件变量cond上等待的所有线程
{
pthread_mutex_lock(&mutex);
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
break;
}
else // 唤醒在条件变量cond上等待的一个线程
{
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
}
pthread_join(id[0], NULL);
pthread_join(id[1], NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
exit(0);
}