#include
int pthread_mutex_init(pthread_mutex_t *restrict mutex,
const pthread_mutexattr_t *restrict attr);
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
功能:销毁或者创建mutex
参数:
#include
int pthread_mutex_destroy(pthread_mutex_t *mutex);
功能:销毁mutex指针指向的锁对象。
参数:mutex传入的锁
常量初始化,此时可以使用init
#include
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
功能:给共享资源加锁
参数:metux init 初始化的锁
如果当前未锁,成功,该线程给加锁。
如果已经加锁,阻塞等待!
互斥量使用步骤:
互斥量使用demo01:
#include
#include
#include
#include
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *thr1(void *arg)
{
while(1) {
// 先上锁
pthread_mutex_lock(&mutex);
printf("hello");
sleep(rand() % 3);
printf("world\n");
// 释放锁
pthread_mutex_unlock(&mutex);
sleep(rand() % 3);
}
}
void *thr2(void *arg)
{
while(1) {
// 先上锁
// 上锁的范围,称为临界区
pthread_mutex_lock(&mutex);
printf("HELLO");
sleep(rand() % 3);
printf("WORLD\n");
// 释放锁
pthread_mutex_unlock(&mutex);
sleep(rand() % 3);
}
}
int main(int argc, char *argv[])
{
pthread_t tid[2];
pthread_create(&tid[0], NULL, thr1, NULL);
pthread_create(&tid[0], NULL, thr2, NULL);
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}
注意事项:加锁需要最小粒度,不要一直占用临界区。
互斥量使用demo01:
#include
#include
#include
#include
#include
pthread_mutex_t mutex;
void *thr(void *arg)
{
while(1) {
pthread_mutex_lock(&mutex);
printf("hello world.\n");
sleep(30);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main()
{
pthread_mutex_init(&mutex, NULL);
pthread_t tid;
pthread_create(&tid, NULL, thr, NULL);
sleep(1);
while(1) {
int ret = pthread_mutex_trylock(&mutex);
if (ret > 0) {
printf("ret = %d, srrmsg:%s\n", ret, strerror(ret));
}
sleep(1);
}
return 0;
}
出现死锁的情况:
解决办法:每个线程申请锁的顺序要一致
如果申请到一把锁,申请另外一把锁的时候失败了,应该释放已经拥有的锁。
总结:读写锁的使用场景,适合读的场景多
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_relockattr_t *restrict attr);
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
int pthread_rwlock_destory(pthread_rwlock_t *rwlock);
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock); // 加读锁
int pthread_rwlock_wrlock(pthtread_rwlock_t *rwlcok); // 加写锁
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
读写锁demo
#include
#include
#include
#include
#include
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
int beginNum = 1000;
void *thr_write(void *arg)
{
while(1) {
pthread_rwlock_wrlock(&rwlock);
printf("-----%s-----self----%lu----beginNum -----%d\n",
__FUNCTION__, pthread_self(), ++beginNum);
pthread_rwlock_unlock(&rwlock);
sleep(3);
}
return NULL;
}
void *thr_read(void *arg)
{
while(1) {
pthread_rwlock_rdlock(&rwlock);
printf("-----%s-----self----%lu----beginNum -----%d\n",
__FUNCTION__, pthread_self(), beginNum);
pthread_rwlock_unlock(&rwlock);
sleep(3);
}
return NULL;
}
int main()
{
int n = 8;
int i = 0;
pthread_t tid[8];
for (i = 0; i < 5; i++) {
pthread_create(&tid[i], NULL, thr_read, NULL);
}
for (; i < 8; i++) {
pthread_creat(&tid[i], NULL, thr_write, NULL);
}
for (i = 0; i < 8; i++) {
pthread_join(tid[i]);
}
return 0;
}
#include
#include
#include
#include
#include
using namespace std;
int beginnum = 1000;
struct _ProdInfo {
int num;
struct _ProdInfo * next;
};
_ProdInfo *Head = nullptr;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *thr_producter(void *arg)
{
// 负责在链表添加数据
while (1) {
_ProdInfo *prod = (_ProdInfo *)malloc(sizeof(_ProdInfo));
prod->num = beginnum++;
printf("-------%s-------self=%lu-----%d\n",
__FUNCTION__, pthread_self(), prod->num);
pthread_mutex_lock(&mutex);
prod->next = Head;
Head = prod;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond);
sleep(rand() % 4);
}
return nullptr;
}
void *thr_customer(void *arg)
{
_ProdInfo *prod = nullptr;
while (1) {
// 获取链表数据
pthread_mutex_lock(&mutex);
if (Head == nullptr) {
// 链表数据没有数据
pthread_cond_wait(&cond, &mutex);
}
prod = Head;
Head = Head->next;
printf("-----%s-------self=%lu-----%d\n",
__FUNCTION__, pthread_self(), prod->num);
pthread_mutex_unlock(&mutex);
sleep(rand() % 4);
free(prod);
}
return nullptr;
}
int main()
{
pthread_t tid[2];
pthread_create(&tid[0], nullptr, thr_producter, nullptr);
pthread_create(&tid[1], nullptr, thr_customer, nullptr);
pthread_join(tid[0], nullptr);
pthread_join(tid[1], nullptr);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
#include
#include
#include
#include
#include
using namespace std;
int beginnum = 1000;
struct _ProdInfo {
int num;
struct _ProdInfo * next;
};
_ProdInfo *Head = nullptr;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *thr_producter(void *arg)
{
// 负责在链表添加数据
while (1) {
_ProdInfo *prod = (_ProdInfo *)malloc(sizeof(_ProdInfo));
prod->num = beginnum++;
printf("-------%s-------self=%lu-----%d\n",
__FUNCTION__, pthread_self(), prod->num);
pthread_mutex_lock(&mutex);
prod->next = Head;
Head = prod;
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond); // 唤醒阻塞的线程
sleep(rand() % 2);
}
return nullptr;
}
void *thr_customer(void *arg)
{
_ProdInfo *prod = nullptr;
while (1) {
// 获取链表数据
pthread_mutex_lock(&mutex);
while (Head == nullptr) { // 支持多个消费者
// 链表数据没有数据,会有多个消费者阻塞在此处
pthread_cond_wait(&cond, &mutex); // 添加条件变量
}
prod = Head;
Head = Head->next;
printf("-----%s-------self=%lu-----%d\n",
__FUNCTION__, pthread_self(), prod->num);
pthread_mutex_unlock(&mutex);
sleep(rand() % 3);
free(prod);
}
return nullptr;
}
int main()
{
pthread_t tid[3];
pthread_create(&tid[0], nullptr, thr_producter, nullptr);
pthread_create(&tid[1], nullptr, thr_customer, nullptr);
pthread_create(&tid[2], nullptr, thr_customer, nullptr);
pthread_join(tid[0], nullptr);
pthread_join(tid[1], nullptr);
pthread_join(tid[2], nullptr);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
简介
进化版的互斥锁(1->N)
由于互斥锁的粒度比较大,如果我们希望在多个线程某一对象的部分数据进行共享,使用互斥锁是没有办法实现的,只能将整个数据对象锁住。这样虽然达到了多线程操作共享数据时保证数据正确性的目的,却无形中导致线程的并发性下降。
信号量,是相对折中的一种处理方式,既能保证同步,数据不混乱,又能提高线程并发。
常用函数
sem_init函数
sem_destroy函数
sem_wait函数
sem_trywait函数
sem_timedwait函数
sem_post函数
信号量demo
#include
#include
#include
#include
#include
#include
sem_t blank,xfull;
const int SEMCNT = 5;
int queue[SEMCNT]; // 模拟饼框
int beginnum = 100;
void *thr_producter(void *arg)
{
int i = 0;
while (1) {
sem_wait(&blank); // 申请资源 blank--
printf("-------%s------self = %lu-----num = %d\n",
__FUNCTION__, pthread_self(), beginnum);
queue[(i++)%SEMCNT] = beginnum++;
sem_post(&xfull); // xfull++
sleep(rand() % 2);
}
return nullptr;
}
void *thr_customer(void *arg)
{
int i = 0;
int num = 0;
while (1) {
sem_wait(&xfull); // xfull++
num = queue[(i++)%SEMCNT];
printf("------------%s------self = %lu-----num = %d\n",
__FUNCTION__, pthread_self(), num);
sem_post(&blank); // blank++
sleep(rand() % 3);
}
return nullptr;
}
int main()
{
sem_init(&blank, 0, SEMCNT);
sem_init(&xfull, 0, 0); // 消费者一开始默认没有商品
pthread_t tid[2];
pthread_create(&tid[0], nullptr, thr_producter, nullptr);
pthread_create(&tid[1], nullptr, thr_customer, nullptr);
pthread_join(tid[0], nullptr);
pthread_join(tid[1], nullptr);
return 0;
}