linux 互斥锁和条件变量

Posted xiaoshiwang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux 互斥锁和条件变量相关的知识,希望对你有一定的参考价值。

草稿(编辑中)

版本1

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

#define MAXITEM  100000000
#define MAXTHREAD  100
#define min(x,y) ( x>y?y:x )

int nitem;

struct 
  pthread_mutex_t mutex;
  int buf[MAXITEM];
  int idx;
  int val;
shared = 
  PTHREAD_MUTEX_INITIALIZER
;

void* produce(void*);
void* consume(void*);

int main(int argc, char** argv)
  int i;
  int nthreads;
  int count[MAXTHREAD];

  pthread_t tid_produce[MAXTHREAD], tid_consume;

  if(argc != 3)
    printf("arg error\n");
    return 1;
  

  nitem = min(MAXITEM,atoi(argv[1]));
  nthreads = min(MAXTHREAD, atoi(argv[2]));

  for(i = 0; i < nthreads; ++i)
    count[i] = 0;
    pthread_create(&tid_produce[i], NULL, produce, &count[i]);
  

  for(i = 0; i < nthreads; ++i)
    pthread_join(tid_produce[i], NULL);
    printf("cout[%d] = %d\n", i, count[i]);
  

  pthread_create(&tid_consume, NULL, consume, NULL);
  pthread_join(tid_consume, NULL);

  return 0;


void* produce(void* arg)
  while(1)
    pthread_mutex_lock(&shared.mutex);
    if(shared.idx >= nitem)
      pthread_mutex_unlock(&shared.mutex);
      return NULL;
    
    shared.buf[shared.idx] = shared.val;
    shared.idx++;
    shared.val++;
    pthread_mutex_unlock(&shared.mutex);
    *((int*)arg) +=1;
  


void* consume(void* arg)
  int i;
  for(i = 0; i < nitem; ++i)
    if(shared.buf[i] != i)
      printf("buf[%d] = %d\n", i, shared.buf[i]);
    
  

版本2

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

#define MAXITEM  100000000
#define MAXTHREAD  100
#define min(x,y) ( x>y?y:x )

int nitem;

struct 
  pthread_mutex_t mutex;
  int buf[MAXITEM];
  int idx;
  int val;
shared = 
  PTHREAD_MUTEX_INITIALIZER
;

void* produce(void*);
void* consume(void*);

int main(int argc, char** argv)
  int i;
  int nthreads;
  int count[MAXTHREAD];

  pthread_t tid_produce[MAXTHREAD], tid_consume;

  if(argc != 3)
    printf("arg error\n");
    return 1;
  

  nitem = min(MAXITEM,atoi(argv[1]));
  nthreads = min(MAXTHREAD, atoi(argv[2]));

  for(i = 0; i < nthreads; ++i)
    count[i] = 0;
    pthread_create(&tid_produce[i], NULL, produce, &count[i]);
  
  pthread_create(&tid_consume, NULL, consume, NULL);
  
  for(i = 0; i < nthreads; ++i)
    pthread_join(tid_produce[i], NULL);
    printf("cout[%d] = %d\n", i, count[i]);
  
  pthread_join(tid_consume, NULL);

  return 0;


void* produce(void* arg)
  while(1)
    pthread_mutex_lock(&shared.mutex);
    if(shared.idx >= nitem)
      pthread_mutex_unlock(&shared.mutex);
      return NULL;
    
    shared.buf[shared.idx] = shared.val;
    shared.idx++;
    shared.val++;
    pthread_mutex_unlock(&shared.mutex);
    *((int*)arg) +=1;
  


void consume_wait(int i)
  while(1)
    pthread_mutex_lock(&shared.mutex);
    if(i < shared.idx)
      pthread_mutex_unlock(&shared.mutex);
      return;
    
    pthread_mutex_unlock(&shared.mutex);
  


void* consume(void* arg)
  int i;
  for(i = 0; i < nitem; ++i)
    consume_wait(i);
    if(shared.buf[i] != i)
      printf("buf[%d] = %d\n", i, shared.buf[i]);
    
  
  return NULL;

版本2

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

#define MAXITEM  100000000
#define MAXTHREAD  100
#define min(x,y) ( x>y?y:x )

int nitem;
int buf[MAXITEM];

struct 
  pthread_mutex_t mutex;
  int idx;
  int val;
 shared = 
  PTHREAD_MUTEX_INITIALIZER
;

struct 
  pthread_mutex_t mutex;
  pthread_cond_t  cond;
  int nready;
 nready = 
  PTHREAD_MUTEX_INITIALIZER,
  PTHREAD_COND_INITIALIZER
;

void* produce(void*);
void* consume(void*);

int main(int argc, char** argv)
  int i;
  int nthreads;
  int count[MAXTHREAD];

  pthread_t tid_produce[MAXTHREAD], tid_consume;

  if(argc != 3)
    printf("arg error\n");
    return 1;
  

  nitem = min(MAXITEM,atoi(argv[1]));
  nthreads = min(MAXTHREAD, atoi(argv[2]));

  for(i = 0; i < nthreads; ++i)
    count[i] = 0;
    pthread_create(&tid_produce[i], NULL, produce, &count[i]);
  
  pthread_create(&tid_consume, NULL, consume, NULL);
  
  for(i = 0; i < nthreads; ++i)
    pthread_join(tid_produce[i], NULL);
    printf("cout[%d] = %d\n", i, count[i]);
  
  pthread_join(tid_consume, NULL);

  return 0;


void* produce(void* arg)
  while(1)
    pthread_mutex_lock(&shared.mutex);
    if(shared.idx >= nitem)
      pthread_mutex_unlock(&shared.mutex);
      return NULL;
    
    buf[shared.idx] = shared.val;
    shared.idx++;
    shared.val++;
    pthread_mutex_unlock(&shared.mutex);

    pthread_mutex_lock(&nready.mutex);
    if(nready.nready == 0)
      pthread_cond_signal(&nready.cond);
    
    nready.nready++;
    pthread_mutex_unlock(&nready.mutex);

    *((int*) arg) += 1;
  


void* consume(void* arg)
  int i;
  for(i = 0; i < nitem; ++i)
    pthread_mutex_lock(&nready.mutex);
    while(nready.nready == 0)
      pthread_cond_wait(&nready.cond, &nready.mutex);
    
    nready.nready--;
    pthread_mutex_unlock(&nready.mutex);

    if(buf[i] != i)
      printf("buf[%d] = %d\n", i, buf[i]);
    
  
  printf("buf[%d] = %d\n", nitem-1, buf[nitem-1]);

以上是关于linux 互斥锁和条件变量的主要内容,如果未能解决你的问题,请参考以下文章

linux 互斥锁和条件变量

在 C++ 中使用互斥锁和条件变量实现带有信号的监视器

信号量互斥锁和条件变量的区别

使用互斥锁和条件变量而不是信号量在 c++14 中打印从 1 到 10 的数字?

UNIX网络编程:互斥锁和条件变量

提升进程间共享互斥锁和提升共享互斥锁的进程间条件变量