C言語では、スレッドを作成して、読んで、分離を書くためにロックを使用するには

スレッドを使用してください

1.スレッドを作成します。

<pthread.hの>内のスレッドに関連操作。

1.1我々はスレッドを定義し、我々は最初の関数を定義する必要があり、我々は同様のスレッドAを作成します

void *thread_a(void *in){
  printf("Im thread_a\n");
  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\n");
  pthread_exit((void*)0);
}
void *thread_b(void *in){
  printf("Im thread_b\n");
  pthread_exit((void*)0);
}
void *thread_c(void *in){
  printf("Im thread_c\n");
  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\n");
  return 0;
}

3.私たちは、Cのための印刷順序をスレッドにしたい場合は、B、我々は信号を定義することができます。

信号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\n");
  pthread_exit((void*)0);
}
void *thread_b(void *in){
  sem_wait(&sem2);/*wait for sem2*/
  printf("Im thread_b\n");
  sem_post(&sem1);/*increase sem1 by 1,make thread_a run*/
  pthread_exit((void*)0);
}
void *thread_c(void *in){
  printf("Im thread_c\n");
  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\n");
  /*destroy sem1,sem2*/
  sem_destroy(&sem1);
  sem_destroy(&sem2);
  return 0;
}

2.ロックの作成と使用

2.1.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("等待从键盘读一个非空串:\n");
      if((n=read(0,buf,sizeof(buf)))>0)
         buf_has_item=1;
      printf("A thread write:%s\n",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\n",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;
}

おすすめ

転載: www.cnblogs.com/littlepage/p/11598006.html