1. 用条件变量的方式实现:现有ID号为a b c的三个线程,每个线程的任务都是循环打印自己id号,要求打印的顺序为abc
#include <stdio.h>
#include <pthread.h>
#include <string.h>
//创建条件变量
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
//创建互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int flag = 0;
void* callBack1(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 0)
{
//休眠,等待其他线程通过条件变量唤醒我
//1.解开互斥锁,同时阻塞当前线程
pthread_cond_wait(&cond, &mutex);
//2.成功唤醒后,会尝试上锁
//3.如果上锁成功,则从当前位置继续向后运行
//4.如果上锁失败,则从当前位置继续阻塞。
}
else
{
//todo something
printf("tid1=%ld a\n",*(pthread_t*)arg);
flag = 1;
}
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* callBack2(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 1)
{
//休眠,等待其他线程通过条件变量唤醒我
//1.解开互斥锁,同时阻塞当前线程
pthread_cond_wait(&cond, &mutex);
//2.成功唤醒后,会尝试上锁
//3.如果上锁成功,则从当前位置继续向后运行
//4.如果上锁失败,则从当前位置继续阻塞。
}
//todo something
else
{
printf("tid2=%ld b\n",*(pthread_t*)arg);
flag = 2;
}
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* callBack3(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(flag != 2)
{
//休眠,等待其他线程通过条件变量唤醒我
//1.解开互斥锁,同时阻塞当前线程
pthread_cond_wait(&cond, &mutex);
//2.成功唤醒后,会尝试上锁
//3.如果上锁成功,则从当前位置继续向后运行
//4.如果上锁失败,则从当前位置继续阻塞。
}
//todo something
else
{
printf("tid3=%ld c\n",*(pthread_t*)arg);
flag = 0;
}
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1, tid2,tid3;
if(pthread_create(&tid1, NULL, callBack1, &tid1) != 0)
{
return -1;
}
if(pthread_create(&tid2, NULL, callBack2, &tid2) != 0)
{ return -1;
}
if(pthread_create(&tid3, NULL, callBack3, &tid3) != 0)
{
return -1;
}
//主线程阻塞等待
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
结果:
linux@linux:~/hqyjC基础/IO/day7$ gcc zuoye1.c -pthread
linux@linux:~/hqyjC基础/IO/day7$ ./a.out
tid1=139711050716928 a
tid2=139711042324224 b
tid3=139711033931520 c
tid1=139711050716928 a
tid2=139711042324224 b
tid3=139711033931520 c
tid1=139711050716928 a
tid2=139711042324224 b
tid3=139711033931520 c
tid1=139711050716928 a
tid2=139711042324224 b
tid3=139711033931520 c
tid1=139711050716928 a
tid2=139711042324224 b
tid3=139711033931520 c
tid1=139711050716928 a
tid2=139711042324224 b
tid3=139711033931520 c
tid1=139711050716928 a
tid2=139711042324224 b
tid3=139711033931520 c
tid1=139711050716928 a
tid2=139711042324224 b
2.创建两个线程,实现将一个文件的内容打印到终端上,类似cat一个文件
一个线程读取文件中的内容
另一个线程将读取到的内容打印到终端上。
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
char c;
off_t size;
sem_t sem1, sem2;
void* callBack1(void* arg)
{
int fd=*(int *)arg;
int i=0;
while(i<size)
{
/临界区
//P操作
sem_wait(&sem1);
read(fd,&c,1);
i++;
//v操作
sem_post(&sem2);
/临界区
}
}
void* callBack2(void* arg)
{ int i=0;
while(i<size)
{
/临界区
//P操作
sem_wait(&sem2);
write(1,&c,1);
i++;
//v操作
sem_post(&sem1);
/临界区
}
}
int main(int argc, const char *argv[])
{
int fd=open(argv[1],O_RDONLY);
if(fd==-1)
{
perror("open");
return -1;
}
size =lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
//创建信号量
if(sem_init(&sem1, 0, 1) < 0)
{
perror("sem_init");
return -1;
}
if(sem_init(&sem2, 0, 0) < 0)
{
perror("sem_init");
return -1;
}
pthread_t tid1, tid2;
if(pthread_create(&tid1, NULL, callBack1, &fd) != 0)
{
fprintf(stderr, "pthread_create failed %d\n", __LINE__);
return -1;
}
pthread_detach(tid1);
if(pthread_create(&tid2, NULL, callBack2, NULL) != 0)
{
fprintf(stderr, "pthread_create failed %d\n", __LINE__);
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
//销毁信号量
sem_destroy(&sem1);
sem_destroy(&sem2);
close(fd);
return 0;
}