作用:又名互斥锁,让多个线程时,保证同时只能有一个线程执行任务
#include
#include
#include
int main(int argc, char const *argv[])
{
//动态获取互斥锁,推荐
pthread_mutex_t lock;
pthread_mutex_init(&lock,NULL);
//静态获取互斥锁,声明与赋值必须同时进程
pthread_mutex_t lock02 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_destroy(&lock);
pthread_mutex_destroy(&lock02);
return 0;
}
#include
#include
#include
int main(int argc, char const *argv[])
{
//动态获取互斥锁,推荐
pthread_mutex_t lock;
pthread_mutex_init(&lock,NULL);
//静态获取互斥锁
pthread_mutex_t lock02 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_destroy(&lock);
pthread_mutex_destroy(&lock02);
return 0;
}
#include
#include
#include
#include
pthread_mutex_t lockA,lockB;
void *testA(void *argv)
{
pthread_mutex_lock(&lockA);
printf("线程%ld进入锁A中\n",pthread_self());
sleep(1);
pthread_mutex_lock(&lockB);
printf("线程%ld进入锁B中\n",pthread_self());
sleep(1);
pthread_mutex_unlock(&lockA);
pthread_mutex_unlock(&lockB);
return NULL;
}
void *testB(void *argv)
{
pthread_mutex_lock(&lockB);
printf("线程%ld进入锁B中\n",pthread_self());
sleep(1);
pthread_mutex_lock(&lockA);
printf("线程%ld进入锁A中\n",pthread_self());
sleep(1);
pthread_mutex_unlock(&lockA);
pthread_mutex_unlock(&lockB);
return NULL;
}
int main(int argc, char const *argv[])
{
pthread_mutex_init(&lockA,NULL);
pthread_mutex_init(&lockB,NULL);
pthread_t t1,t2;
pthread_create(&t1,NULL,testA,NULL);
pthread_create(&t2,NULL,testB,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_mutex_destroy(&lockB);
pthread_mutex_destroy(&lockA);
printf("主线程OVER\n");
return 0;
}
作用:用来初始化 rwlock 所指向的读写锁。
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
//动态初始化,推荐
pthread_rwlock_t rwlock01;
pthread_rwlock_init(&rwlock01,NULL);
//静态初始化,不建议使用,声明与复制必须同时进行
pthread_rwlock_t rwlock02 = PTHREAD_RWLOCK_INITIALIZER;
return 0;
}
//注意:vscode编写时不会提示,需要手动编写
作用:用于销毁一个读写锁,并释放所有相关联的资源(所谓的所有指的是pthread_rwlock_init() 自动申请的资源)
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
//动态初始化,推荐
pthread_rwlock_t rwlock01;
pthread_rwlock_init(&rwlock01,NULL);
//静态初始化,不建议使用,声明与复制必须同时进行
pthread_rwlock_t rwlock02 = PTHREAD_RWLOCK_INITIALIZER;
pthread_rwlock_destroy(&rwlock01);
pthread_rwlock_destroy(&rwlock02);
return 0;
}
作用:无论是读锁或写锁,都可以通过此函数解锁
#include
#include
#include
#include
#include
#include
pthread_rwlock_t rwlock;
//多线程公共读写数据
int num = 0;
void *writeNum(void *x)
{
pthread_rwlock_wrlock(&rwlock);
sleep(2);
num = rand()%100;
printf("线程%ld写入后num=%d\n",pthread_self(),num);
pthread_rwlock_unlock(&rwlock);
return NULL;
}
void *readNum(void *argv)
{
pthread_rwlock_rdlock(&rwlock);
sleep(2);
printf("线程%ld读取到的num=%d\n",pthread_self(),num);
pthread_rwlock_unlock(&rwlock);
return NULL;
}
void closeThread(pthread_t ps[],int len)
{
int i;
for (i = 0; i < len; i++)
{
pthread_t t = ps[i];
pthread_join(t,NULL);
}
}
int main(int argc, char const *argv[])
{
//初始化读写锁
pthread_rwlock_init(&rwlock,NULL);
//设置随机数种子
srand(time(NULL));
//声明3个线程写
pthread_t tw[3];
for(int i = 10; i < 13; i++)
{
pthread_create(&tw[i-10],NULL,writeNum,NULL);
}
//声明10个线程读
pthread_t tr[10];
for(int i = 0; i < 10; i++)
{
pthread_create(&tr[i],NULL,readNum,NULL);
}
int wlen = sizeof(tw)/sizeof(pthread_t);
closeThread(tw,wlen);
int rlen = sizeof(tw)/sizeof(pthread_t);
closeThread(tr,rlen);
pthread_rwlock_destroy(&rwlock);
return 0;
}
#include
#include
#include
//声明互斥锁
pthread_mutex_t lock;
//声明条件变量
pthread_cond_t cond;
void *test01()
{
pthread_mutex_lock(&lock);
printf("线程%ld陷入休眠\n",pthread_self());
pthread_cond_wait(&cond,&lock);
printf("线程%ld被唤醒\n",pthread_self());
pthread_mutex_unlock(&lock);
}
int main(int argc, char const *argv[])
{
//声明线程
pthread_t t01,t02;
//初始化互斥锁
pthread_mutex_init(&lock,NULL);
//初始化条件变量
pthread_cond_init(&cond,NULL);
//创建线程
pthread_create(&t01,NULL,test01,NULL);
pthread_create(&t02,NULL,test01,NULL);
sleep(5);
//随机唤醒一个
//pthread_cond_signal(&cond);
//唤醒所有
pthread_cond_broadcast(&cond);
//销毁显示
pthread_join(t01,NULL);
pthread_join(t02,NULL);
//释放互斥锁
pthread_mutex_destroy(&lock);
//释放条件变量
pthread_cond_destroy(&cond);
return 0;
}
#include
#include
#include
#include
#include
#include
//声明互斥锁
pthread_mutex_t lock;
//声明条件变量
pthread_cond_t cond;
//声明记录库存数量的变量
int num = 0;
//声明生产的方法
void *produce(void *argv);
//声明销售的方法
void *sale(void *argv);
//声明释放线程的方法
void closeThread(pthread_t ts[],int len)
{
for(int i = 0; i < len; i++)
{
pthread_join(ts[i],NULL);
}
}
int main(int argc, char const *argv[])
{
srand(time(NULL));
//初始化互斥锁
pthread_mutex_init(&lock,NULL);
//初始化条件变量
pthread_cond_init(&cond,NULL);
//声明生产者线程组
pthread_t ps[3];
//声明销售者线程组
pthread_t ss[5];
//创建线程并执行
int i;
for(i = 0; i < 3; i++)
{
pthread_create(&ps[i],NULL,produce,NULL);
}
for(int i = 0; i < 5; i++)
{
pthread_create(&ps[i],NULL,sale,NULL);
}
//释放生产者线程
int plen = sizeof(ps)/sizeof(pthread_t);
closeThread(ps,plen);
//释放消费者线程
int slen = sizeof(ss)/sizeof(pthread_t);
closeThread(ss,slen);
//释放互斥锁
pthread_mutex_destroy(&lock);
//释放条件变量
pthread_cond_destroy(&cond);
return 0;
}
void *produce(void *argv)
{
while(1){
pthread_mutex_lock(&lock);
while(num >= 10)
{
printf("库存已满,线程%ld停止生产\n",pthread_self());
pthread_cond_wait(&cond,&lock);
}
num++;
printf("线程%ld生产了一个商品,当前库存数量为:%d\n",pthread_self(),num);
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&lock);
sleep(1);
}
return NULL;
}
void *sale(void *argv)
{
while(1)
{
pthread_mutex_lock(&lock);
while(num <= 0)
{
printf("库存为0,线程%ld停止销售\n",pthread_self());
pthread_cond_wait(&cond,&lock);
}
num--;
printf("线程%ld销售了一个商品,当前库存数量为:%d\n",pthread_self(),num);
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&lock);
int t = rand()%5;
sleep(t);
}
}
#include
#include
#include
#include
sem_t sem;
void *printstr(void *argv)
{
sem_wait(&sem);
char * str = (char *)argv;
int i = 0;
while(str[i] != '\0')
{
printf("%c\n",str[i]);
i++;
sleep(1);
}
sem_post(&sem);
return NULL;
}
int main(int argc, char const *argv[])
{
//初始化信号量,0表示线程 1初始值
sem_init(&sem,0,1);
pthread_t t01,t02,t03;
pthread_create(&t01,NULL,printstr,"HELLO");
pthread_create(&t02,NULL,printstr,"c++");
pthread_create(&t03,NULL,printstr,"Java");
pthread_join(t01,NULL);
pthread_join(t02,NULL);
pthread_join(t03,NULL);
//释放信号量
sem_destroy(&sem);
return 0;
}
#include
#include
#include
#include
sem_t s01,s02,s03;
void *print01(void *argv)
{
sem_wait(&s01);
printf("线程1输入\n");
sem_post(&s02);
return NULL;
}
void *print02(void *argv)
{
sem_wait(&s02);
printf("线程2输入\n");
sem_post(&s03);
return NULL;
}
void *print03(void *argv)
{
sem_wait(&s03);
printf("线程3输入\n");
sem_post(&s01);
return NULL;
}
int main(int argc, char const *argv[])
{
sem_init(&s01,0,1);
sem_init(&s02,0,0);
sem_init(&s03,0,0);
pthread_t t01,t02,t03;
pthread_create(&t01,NULL,print01,NULL);
pthread_create(&t02,NULL,print02,NULL);
pthread_create(&t03,NULL,print03,NULL);
pthread_join(t01,NULL);
pthread_join(t02,NULL);
pthread_join(t03,NULL);
sem_destroy(&s01);
sem_destroy(&s02);
sem_destroy(&s03);
return 0;
}
#include
#include
#include //信号量
#include //mmap
#include //wait
void print_string(void *str)
{
char *p = (char *)str;
int i = 0;
while (p[i] != '\0')
{
printf("%c", p[i++]);
fflush(stdout);
sleep(1);
}
}
int main(int argc, char const *argv[])
{
//创建无名信号量
// MAP_ANONYMOUS匿名映射 -1不需要文件描述符
sem_t *sem = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
//初始化信号量 1表示作用于进程 1初始值
sem_init(sem, 1, 1);
pid_t pid = fork();
if (pid == 0) //子进程
{
// p 操作
sem_wait(sem);
print_string("ni hao");
// V 操作
sem_post(sem);
_exit(-1);
}
else if (pid > 0) //父进程
{
// p 操作
sem_wait(sem);
print_string("hello world");
// V 操作
sem_post(sem);
wait(NULL);
}
//销毁信号量
sem_destroy(sem);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
void printStr(char *str)
{
int i = 0;
while(str[i] != '\0')
{
printf("%c\n",str[i]);
fflush(stdout);
i++;
sleep(1);
}
printf("\n");
}
int main(int argc, char const *argv[])
{
//通过mmap(磁盘映射)创建有缘信号量
sem_t *sem = (sem_t *)mmap(NULL,//映射区域地址,给NULL内核会自己选择合适的区域
sizeof(sem_t),//映射区域大学
PROT_READ | PROT_WRITE,//权限:可读,可写
MAP_SHARED | MAP_ANONYMOUS,//标志位:共享的 | 映射没有任何文件支持;其内容被初始化为零
-1,//文件标识符,当标志位有MAP_ANONYMOUS,文件标识符必须为-1
0);//偏移量
//初始化信号量
//1参:要初始化的信号量指针
//2参:0线程间共享,非0进程间共享
//3参:信号量初始值
sem_init(sem,1,1);
int i = 0;
for (i = 0; i < 2; i++)
{
pid_t pid = fork();
if (pid == 0)
{
printf("进程%d被创建了\n",getpid());
break;
}
}
if (i == 0)
{
//子进程1
//p操作,信号量-1
sem_wait(sem);
printStr("hello");
sem_post(sem);
_exit(-1);
}
else if(i == 1)
{
//子进程2
sem_wait(sem);
printStr("c++");
sem_post(sem);
_exit(-1);
}
else if(i == 2)
{
//父进程
while (1)
{
//-1,等待任意子进程结束回收,
//WNOHANG:不阻塞
//返回值:被回收的子进程id
pid_t pid = waitpid(-1,NULL,WNOHANG);
if (pid > 1)
{
printf("进程%d,被回收了\n",pid);
}
else if (pid == 0)
{
//当pid为0说明当前并没有回收到子进程,还有子进程在运行
continue;
}
else if(pid < 0)
{
//当pid小于0说明当前父进程中已经没有子进程了
break;
}
}
//销毁信号量
sem_destroy(sem);
}
getchar();
return 0;
}
#include
#include
#include //信号量
#include //mmap
#include //wait
void print_string(void *str)
{
char *p = (char *)str;
int i = 0;
while (p[i] != '\0')
{
printf("%c", p[i++]);
fflush(stdout);
sleep(1);
}
}
int main(int argc, char const *argv[])
{
//创建无名信号量
// MAP_ANONYMOUS匿名映射 -1不需要文件描述符
sem_t *sem1 = (sem_t *)mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
sem_t *sem2 = (sem_t *)mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
//初始化信号量 1表示作用于进程 1初始值
sem_init(sem1, 1, 1);
sem_init(sem2, 1, 0);
pid_t pid = fork();
if (pid == 0) //子进程
{
// p 操作
sem_wait(sem1);
print_string("ni hao");
// V 操作
sem_post(sem2);
_exit(-1);
}
else if (pid > 0) //父进程
{
// p 操作
sem_wait(sem2);
print_string("hello world");
// V 操作
sem_post(sem1);
wait(NULL);
}
//销毁信号量
sem_destroy(sem1);
sem_destroy(sem2);
return 0;
}
//18_codeA.c
#include
#include
#include
#include
#include
void printStr(char *str)
{
int i = 0;
while(str[i] != '\0')
{
printf("%c\n",str[i]);
i++;
sleep(1);
}
}
int main(int argc, char const *argv[])
{
sem_t *sem = sem_open("sem",O_RDWR | O_CREAT,0666,1);
sem_wait(sem);
printStr("hello 123");
sem_post(sem);
sem_close(sem);
sem_destroy(sem);
return 0;
}
//18_codeB.c
#include
#include
#include
#include
#include
void printStr(char *str)
{
int i = 0;
while(str[i] != '\0')
{
printf("%c\n",str[i]);
i++;
sleep(1);
}
}
int main(int argc, char const *argv[])
{
sem_t *sem = sem_open("sem",O_RDWR | O_CREAT,0666,1);
sem_wait(sem);
printStr("Hi C++");
sem_post(sem);
sem_close(sem);
sem_destroy(sem);
return 0;
}
//19_codeA.c
#include /* For O_* constants */
#include /* For mode constants */
#include
#include
#include
void print_string(void *str)
{
char *p = (char *)str;
int i = 0;
while (p[i] != '\0')
{
printf("%c", p[i++]);
fflush(stdout);
sleep(1);
}
}
int main(int argc, char const *argv[])
{
//创建一个有名信号量
sem_t *sem1 = sem_open("sem1", O_RDWR | O_CREAT, 0666, 1);
sem_t *sem2 = sem_open("sem2", O_RDWR | O_CREAT, 0666, 0);
// P 操作
sem_wait(sem1);
print_string("nihao xian");
// V 操作
sem_post(sem2);
//关闭信号量
sem_close(sem1);
sem_close(sem2);
//销毁信号
sem_destroy(sem1);
sem_destroy(sem2);
return 0;
}
//19_codeB.c
#include /* For O_* constants */
#include /* For mode constants */
#include
#include
#include
void print_string(void *str)
{
char *p = (char *)str;
int i = 0;
while (p[i] != '\0')
{
printf("%c", p[i++]);
fflush(stdout);
sleep(1);
}
}
int main(int argc, char const *argv[])
{
//创建一个有名信号量
sem_t *sem1 = sem_open("sem1", O_RDWR | O_CREAT, 0666, 1);
sem_t *sem2 = sem_open("sem2", O_RDWR | O_CREAT, 0666, 0);
// P 操作
sem_wait(sem2);
print_string("hello world");
// V 操作
sem_post(sem1);
//关闭信号量
sem_close(sem1);
sem_close(sem2);
//销毁信号
sem_destroy(sem1);
sem_destroy(sem2);
return 0;
}