• C语言创建线程以及使用锁进行读写分离


    线程的使用

    1.线程的创建

    线程的相关操作放在<pthread.h>中。

    1.1我们定义一个线程,首先要进行定义一个函数,类似我们创建一个a线程

    void *thread_a(void *in){
      printf("Im thread_a
    ");
      pthread_exit((void*)0);
    }
    

    1.2.创建一个线程

    pthread_t a;//声明
    pthread_create(&a,NULL,thread_a,(void*)0);//创建线程
    

    1.3.创建3个线程并且打印(如果你打印1000个,并且睡一秒,会发现,这个进程实际上运行是无序的)

    #include <stdio.h>
    #include <pthread.h>
    void *thread_a(void *in){
      printf("Im thread_a
    ");
      pthread_exit((void*)0);
    }
    void *thread_b(void *in){
      printf("Im thread_b
    ");
      pthread_exit((void*)0);
    }
    void *thread_c(void *in){
      printf("Im thread_c
    ");
      pthread_exit((void*)0);
    }
    int main(){
      pthread_t a,b,c;
      int val;
      /**create thread a,b,c*/
      pthread_create(&a,NULL,thread_a,(void*)0);
      pthread_create(&b,NULL,thread_b,(void*)0);
      pthread_create(&c,NULL,thread_c,(void*)0);
      /**main thread waits for termination of a,b,c*/
      pthread_join(a,(void**)0);
      pthread_join(b,(void**)0);
      pthread_join(c,(void**)0);
      printf("Main thread is over
    ");
      return 0;
    }
    
    

    3.如果我们希望线程进行打印顺序为c,b,a,那么我们可以定义一个信号。

    线程a等待线程2的信号,线程2等待线程3的信号

    #include <stdio.h>
    #include <pthread.h>
    #include <semaphore.h>//sem_wait sem_init这些方法全在里面
    sem_t sem1;
    sem_t sem2;
    void *thread_a(void *in){
      sem_wait(&sem1);/*wait for sem1*/
      printf("Im thread_a
    ");
      pthread_exit((void*)0);
    }
    void *thread_b(void *in){
      sem_wait(&sem2);/*wait for sem2*/
      printf("Im thread_b
    ");
      sem_post(&sem1);/*increase sem1 by 1,make thread_a run*/
      pthread_exit((void*)0);
    }
    void *thread_c(void *in){
      printf("Im thread_c
    ");
      sem_post(&sem2);/*increase sem2 by 1,make thread_b run*/
      pthread_exit((void*)0);
    }
    int main(){
      pthread_t a,b,c;
      int val;
      
      /**init sem1 sem2 to 0,any thread waits for it will be blocked*/
      sem_init(&sem1,0,0);
      sem_init(&sem2,0,0);
      /**create thread a,b,c*/
      pthread_create(&a,NULL,thread_a,(void*)0);  
      pthread_create(&b,NULL,thread_b,(void*)0);  
      pthread_create(&c,NULL,thread_c,(void*)0);  
      /**main thread waits for termination of a,b,c*/  
      pthread_join(a,(void**)0);
      pthread_join(b,(void**)0);
      pthread_join(c,(void**)0);
      printf("Main thread is over
    ");
      /*destroy sem1,sem2*/
      sem_destroy(&sem1);
      sem_destroy(&sem2);
      return 0;
    }
    

    2.锁的创建和使用

    2.1.mutex互斥锁的创建

    互斥锁的内容:我们要想创建一个互斥锁,首先是对这个mutex进行初始化操作。

    互斥锁的头文件在<pthread.h>中。

    pthread_mutex_t mutex;//声明一个锁
    pthread_mutex_lock(&mutex);
    pthread_mutex_unlock(&mutex);
    //加锁和解锁操作
    pthread_mutex_destory(&mutex);//销毁锁的操作
    

    2.2.编写一个读写(线程分离)的程序,读一个空串,然后写进另一个数组里面

    #include <string.h>
    #include <unistd.h>
    #include <pthread.h>
    #include <stdio.h>
    
    pthread_mutex_t mutex;
    char buf[80],buf1[80];
    int n,buf_has_item=0;
    
    void writer_function(void){
      while(1){
        pthread_mutex_lock(&mutex);
        if(buf_has_item==0){
          printf("等待从键盘读一个非空串:
    ");
          if((n=read(0,buf,sizeof(buf)))>0)
             buf_has_item=1;
          printf("A thread write:%s
    ",buf);
        }
        pthread_mutex_unlock(&mutex);
      }
    }
    
    void reader_function(void){
      while(1){
        pthread_mutex_lock(&mutex);
        if(buf_has_item==1){
           strcpy(buf1,buf); 
           buf_has_item=0;
           printf("A thread read:%s
    ",buf1);
        }     
        pthread_mutex_unlock(&mutex);
      }     
    }
                 
    void *thread_reader(void *in){
      reader_function();
      pthread_exit((void**)0);
    }
    
    int main(){
      pthread_t reader,writer;pthread_t ptr;
      pthread_mutex_init(&mutex,NULL);
      pthread_create(&ptr,NULL,thread_reader,(void*)0);//创建一个线程
      writer_function();
      pthread_join(ptr,(void**)0);
      return 0;
    }
    
    
    
  • 相关阅读:
    测试思想-流程规范 关于预发布环境的一些看法
    Jenkins 开启用户注册机制及用户权限设置
    Jenkins 利用Dashboard View插件管理任务视图
    Loadrunner 脚本开发-从文件读取数据并参数化
    SVN SVN合并(Merge)与拉取分支(Branch/tag)操作简介
    测试思想-流程规范 SVN代码管理与版本控制
    Python 关于Python函数参数传递方式的一点探索
    接口自动化 基于python+Testlink+Jenkins实现的接口自动化测试框架[V2.0改进版]
    Python 解决Python安装包时提示Unable to find vcvarsall.bat的问题
    lintcode :链表插入排序
  • 原文地址:https://www.cnblogs.com/littlepage/p/11598006.html
Copyright © 2020-2023  润新知