1、概述
同步和异步是多并发中的两种运行情况,异步是各自干各自的,同步是做同一件事
2、同步方式
互斥锁、读写锁、信号量、条件变量
3、互斥锁用例
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
int g_iNum = 0; //全局变量
pthread_mutex_t mutex; //生成一个线程锁,互斥锁
void *thread_one(void *arg)
{
while (1)
{
//没锁:mutex --> 1
//锁上:mutex --> 0
//lock的时候检查mutex的值,如果为1,则表示该锁没有锁上
//然后锁上,对其值减一操作,mutex的值就变成0
//lock的时候检查mutex的值,如果为0,则表示该锁已经锁上了
//则阻塞等待,直到mutex解锁,值为1
pthread_mutex_lock(&mutex);
g_iNum = g_iNum+1;
printf("in thread one --> %d\n", g_iNum);
//sleep(1);
pthread_mutex_unlock(&mutex);
//解锁:mutex --> 1
}
return (void*)66;
}
void *thread_two(void *arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
g_iNum = g_iNum+1;
printf("in thread two --> %d\n", g_iNum);
//sleep(1);
pthread_mutex_unlock(&mutex);
}
return (void*)88;
}
int main(void)
{
pthread_t one; //long unsigned int;
pthread_create(&one, NULL, thread_one, NULL);
pthread_t two; //long unsigned int;
pthread_create(&two, NULL, thread_two, NULL);
//进程称之为主线程
int i = 0;
while (1)
{
pause();
}
return 0;
}
4、读写锁用例
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
//读写锁应用于共享资源访问并发量高于修改并发量的场景
//A线程获得读锁,后续其他读线程也可以获得该锁进行读操作。后续写线程阻塞等待
//A线程获得写锁,后续其他读线程阻塞等待,后续写线程阻塞等待
//即可以多线程同时读,不能多线程同时读写,不能多线程同时写
int g_iNum = 0;
//pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; //生成一个读写锁
pthread_rwlock_t rwlock;
void *thread_one(void *arg)
{
int i = 0;
char *name = (char*)arg;
while (1)
{
pthread_rwlock_rdlock(&rwlock);
for (i=0; i<10; i++)
{
printf("in thread %s --> %d\n", name, g_iNum);
sleep(1);
}
pthread_rwlock_unlock(&rwlock);
sleep(1);
}
return (void*)66;
}
void *thread_two(void *arg)
{
int i = 0;
char *name = (char*)arg;
while (1)
{
pthread_rwlock_wrlock(&rwlock);
for (i=0; i<5; i++)
{
g_iNum = g_iNum+1;
printf("in thread %s --> %d\n", name, g_iNum);
sleep(1);
}
pthread_rwlock_unlock(&rwlock);
sleep(4);
}
return (void*)88;
}
int main(void)
{
pthread_rwlock_init(&rwlock, NULL);
pthread_t one_1; //long unsigned int;
pthread_create(&one_1, NULL, thread_one, "jack");
pthread_t one_2; //long unsigned int;
pthread_create(&one_2, NULL, thread_one, "lucy");
pthread_t one_3; //long unsigned int;
pthread_create(&one_3, NULL, thread_one, "tony");
pthread_t one_4; //long unsigned int;
pthread_create(&one_4, NULL, thread_one, "pitter");
//写优先
pthread_t two_1; //long unsigned int;
pthread_create(&two_1, NULL, thread_two, "#############");
pthread_t two_2; //long unsigned int;
pthread_create(&two_2, NULL, thread_two, "@@@@@@");
//进程称之为主线程
int i = 0;
while (1)
{
pause();
}
return 0;
}
5、信号量用例
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
//信号量:1,标记共享资源数量的一个对象
// 2,发送信号唤醒等待的线程
sem_t sem;
//表示10个共享资源
int g_iData[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int g_iIndex = 0;
pthread_mutex_t lock;
void *handle(void*arg)
{
int num = (int)arg;
while (1)
{
//1,检查sem的值是否大于0,是则表示有资源可用,对sem-1,往下运行
//若sem等于0,则表示没有资源可用,则阻塞等待,直到sem>0
sem_wait(&sem);
pthread_mutex_lock(&lock);
g_iData[g_iIndex] = 100+num;
printf("第%d个线程使用了第%d个资源,资源值:%d\n", num, g_iIndex, g_iData[g_iIndex]);
g_iIndex = (g_iIndex+1)%10;
pthread_mutex_unlock(&lock);
sleep(2);
//释放资源,并唤醒其他等待的线程,对sem+1
sem_post(&sem);
sleep(1);
}
return NULL;
}
int main(void)
{
//无名信号量初始化
//参数二:0表示该信号量用于同一个进程下的线程间
// 非0表示该信号量用于多进程间
//参数三:设置信号量的值,用于标记共享资源的可用数量
sem_init(&sem, 0, 10);
pthread_t thr[15];
int i = 0;
for (; i<15; i++)
{
pthread_create(thr, NULL, handle, (void *)(i+1));
}
//进程称之为主线程
while (1)
{
pause();
}
return 0;
}