Linux线程——互斥锁限制共享资源的访问,线程条件实现线程同步,代码脚本(实现数据导入)

互斥锁限制共享资源的访问

我们的目的是当全局变量a等于3时,线程1退出。

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

//功能:创建一个新的线程 原型 
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine)(void*), void *arg); 
//参数 
// thread:返回线程ID
// attr:设置线程的属性,attr为NULL表示使用默认属性
// start_routine:是个函数地址,线程启动后要执行的函数
// arg:传给线程启动函数的参数,若参数过多,可用结构体传入
// 返回值:成功返回0;失败返回错误码
//int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); 
//int pthread_mutex_lock(pthread_mutex_t *mutex);
//int pthread_mutex_trylock(pthread_mutex_t *mutex);
//int pthread_mutex_unlock(pthread_mutex_t *mutex);
//int pthread_mutex_destroy(pthread_mutex_t *mutex);
pthread_mutex_t mutex0;
int a=0;
void *fun1()
{
   pthread_mutex_lock(&mutex0);
   while(1){
      a++;
      printf("t1:%d\n",a);
      if(a==3){
         printf("t1 quit========================\n");
         pthread_mutex_unlock(&mutex0);
         exit(0);
      }
   }
}

void *fun2()
{

   while(1){
      pthread_mutex_lock(&mutex0);
      a++;
      printf("t1:%d\n",a);
      pthread_mutex_unlock(&mutex0);
      sleep(1);
   }
}

int main()
{
   pthread_t t1;
   pthread_t t2;
   pthread_create(&t1,NULL,fun1,NULL);
   pthread_create(&t2,NULL,fun2,NULL);
   pthread_mutex_init(&mutex0,NULL);
   pthread_join(t1,NULL);
   pthread_join(t2,NULL);

}

脚本执行文件

方法一 .sh文件

我们现在通过脚本来执行一百次

1.创建  vi xxx.sh文件
2..sh文件中直接写入自己的命令
3.chmod +x test.sh
4../xxx.sh

在这里插入图片描述
在这里插入图片描述
便会执行三次

方法2 .c文件

#include <stdlib.h>
int main()
{
        int i;
        for(i=0;i<100;i++)
        {
        system("./8");
        }
}

需要用信号杀死

方法三:可获取输出内容

#include <stdlib.h>
int main(int argc,char **argv)
{
    int i;
    int time = atoi(argv[1]);//argv为运行次数
    for(i=0;i<time;i++){
       system("./9");
    }
}

我们输入./test+次数
在这里插入图片描述
代码就会运行我们想要的次数

./test 20 >> test.ret.txt &//将所有结果导入到txt文本里

在这里插入图片描述
我们输入后会出现一个进程号,杀死它即可
在这里插入图片描述
就可以看到我们想要的结果

线程条件实现线程同步

线程条件概述

条件变量是线程另一可用的同步机制。条件变量给多个线程提供了一个会合的场所。条件变量与互斥量一起使用时,允许线程以无竞争的方式等待特定的条件发生。

条件本身是由互斥量保护的。线程在改变条件状态前必须首先锁住互斥量,其他线程在获得互斥量之前不会察觉到这种改变,因为必须锁定互斥量以后才能计算条件。

条件变量使用之前必须首先初始化,pthread_cond_t数据类型代表的条件变量可以用两种方式进行初始化,可以把常量PTHREAD_COND_INITIALIZER赋给静态分配的条件变量,但是如果条件变量是动态分配的,可以使用pthread_cond_destroy函数对条件变量进行去除初始化(deinitialize)。

函数原型:

1. 创建及销毁条件变量

#include <pthread.h>
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t cond);
// 返回:若成功返回0,否则返回错误编号

2. 等待
#include <pthread.h>
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, cond struct timespec *restrict timeout);
// 返回:若成功返回0,否则返回错误编号
  pthread_cond_wait等待条件变为真。如果在给定的时间内条件不能满足,那么会生成一个代表一个出错码的返回变量。传递给pthread_cond_wait的互斥量对条件进行保护,调用者把锁住的互斥量传给函数。函数把调用线程放到等待条件的线程列表上,然后对互斥量解锁,这两个操作都是原子操作。这样就关闭了条件检查和线程进入休眠状态等待条件改变这两个操作之间的时间通道,这样线程就不会错过条件的任何变化。pthread_cond_wait返回时,互斥量再次被锁住。
pthread_cond_timedwait函数的工作方式与pthread_cond_wait函数类似,只是多了一个timeout。timeout指定了等待的时间,它是通过timespec结构指定。

3. 触发
#include <pthread.h>
int pthread_cond_signal(pthread_cond_t cond);
int pthread_cond_broadcast(pthread_cond_t cond);
// 返回:若成功返回0,否则返回错误编号
  这两个函数可以用于通知线程条件已经满足。pthread_cond_signal函数将唤醒等待该条件的某个线程,而pthread_cond_broadcast函数将唤醒等待该条件的所有进程。
注意一定要在改变条件状态以后再给线程发信号。

代码示例:
当a=3,线程2打印t2run=============;打印五次后退出

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

//功能:创建一个新的线程 原型 
//int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine)(void*), void *arg); 
//参数 
// thread:返回线程ID
// attr:设置线程的属性,attr为NULL表示使用默认属性
// start_routine:是个函数地址,线程启动后要执行的函数
// arg:传给线程启动函数的参数,若参数过多,可用结构体传入
// 返回值:成功返回0;失败返回错误码
//int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); 
//int pthread_mutex_lock(pthread_mutex_t *mutex);
//int pthread_mutex_trylock(pthread_mutex_t *mutex);
//int pthread_mutex_unlock(pthread_mutex_t *mutex);
//int pthread_mutex_destroy(pthread_mutex_t *mutex);
//1. 创建及销毁条件变量

//#include <pthread.h>
//int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
//int pthread_cond_destroy(pthread_cond_t cond);

//2. 等待
//#include <pthread.h>
//int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
//int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, cond struct timespec *restrict timeout);

//3. 触发
//#include <pthread.h>
//int pthread_cond_signal(pthread_cond_t cond);
//int pthread_cond_broadcast(pthread_cond_t cond);
// 返回:若成功返回0,否则返回错误编号
int a=0;
int cnt=0;
pthread_cond_t cond;
pthread_mutex_t mutex0;
void *fun1()
{
   while(1){
      pthread_mutex_lock(&mutex0);
      a++;
      pthread_mutex_unlock(&mutex0);
      printf("t1:%d\n",a);
      if(a==3){
           pthread_cond_signal(&cond);//发出指令
       }
       sleep(1);
      }
}

void *fun2()
{
    while(1){
       pthread_cond_wait(&cond,&mutex0);//等待
       printf("t2:%d\n",a);
       printf("t2 quit =========================\n");
       a=0;
       cnt++;
       if(cnt==5){
          exit(0);
       }
       sleep(1);
   }
}

int main()
{
   pthread_t t1;
   pthread_t t2;
   pthread_cond_init(&cond,NULL);
   pthread_create(&t1,NULL,fun1,NULL);
   pthread_create(&t2,NULL,fun2,NULL);
   pthread_mutex_init(&mutex0,NULL);
   pthread_join(t1,NULL);
   pthread_join(t2,NULL);
    pthread_cond_destroy(&cond);

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pg_hj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值