C language to create threads and use locks to read and write separation

Use thread

1. Create a thread

Related operations on the thread <pthread.h> in.

1.1 We define a thread, we must first define a function, we create a similar thread a

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

1.2. Creating a thread

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

1.3. Creating three threads and print (if you print 1000, and sleep a second, you will find that this process is actually running are unordered)

#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. If we want to thread the print order for the c, b, a, then we can define a signal.

A signal waiting threads thread 2 and thread 3 to the thread waits for the signal 2

#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. Creating and Using locks

2.1.mutex mutex is created

Content mutex: We want to create a mutex, first initialize this mutex operation.

Mutex header file <pthread.h> in.

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

2.2 a write read (separate thread) program, read an empty string, which is then written into another array

#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;
}

Guess you like

Origin www.cnblogs.com/littlepage/p/11598006.html