POSIX多线程编程实例

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#define NUM 3
pthread_mutex_t mutex;
pthread_cond_t cond;
int n = 0;

void *thread_func(void *arg);

int main(int argc, char *argv[]){
	pthread_t tid[NUM];
	pthread_mutex_init(&mutex, NULL);
	pthread_cond_init(&cond, NULL);
	
	int thread;
	for (thread = 0; thread < NUM; thread ++){
		pthread_create(&tid[thread], NULL, thread_func, (void *)thread);
	}

	for (thread = 0; thread < NUM; thread ++){
		pthread_join(tid[thread], NULL);
	}

	pthread_cond_destroy(&cond);
    	pthread_mutex_destroy(&mutex);
	pthread_exit(NULL);
	return 0;
}

void *thread_func(void *arg){
	int param = (int)arg;
	int i;
	for (i=0; i<10; i++){
		pthread_mutex_lock(&mutex);
		while(param != n){
			pthread_cond_wait(&cond, &mutex);	
		}
		printf("%d ", param+1);
		n = (n + 1) % NUM;
		pthread_mutex_unlock(&mutex);
		pthread_cond_broadcast(&cond);
	}
	return NULL;
}

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

int num_odd = 0;
int num_even = 0;
int buffer;
pthread_mutex_t mutex;
pthread_cond_t is_empty, is_odd, is_even;
void *Producer(void *rank);
void *Consumer_odd(void *rank);
void *Consumer_even(void *rank);

int main(int argc, char *argv[]){
	long int thread;
	srand(time(NULL));
	pthread_t thread_handles[3];
	pthread_mutex_init(&mutex, NULL);
	pthread_cond_init(&is_empty, NULL);
	pthread_cond_init(&is_odd, NULL);
	pthread_cond_init(&is_even, NULL);

	pthread_create(&thread_handles[0], NULL, Producer, NULL);
        pthread_create(&thread_handles[1], NULL, Consumer_odd, NULL);
        pthread_create(&thread_handles[2], NULL, Consumer_even, NULL);

	for (thread = 0; thread < 3; thread ++){
		pthread_join(thread_handles[thread], NULL);
	}
	pthread_cond_destroy(&is_odd);
	pthread_cond_destroy(&is_even);
	pthread_cond_destroy(&is_empty);
        pthread_mutex_destroy(&mutex);
	pthread_exit(NULL);
	return 0;
}
void *Producer(void *rank){
	int k;
	for (int i=0; i<5; i++){
		pthread_mutex_lock(&mutex);
		if((num_odd + num_even) != 0)
			pthread_cond_wait(&is_empty, &mutex);
		k = rand() % 100;
		printf("Producer puts %d\n", k);
		buffer = k;
		if(k % 2 != 0){
			num_odd++;
			pthread_cond_signal(&is_odd);
		}
		else{
			num_even++;
			pthread_cond_signal(&is_even);
		}
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}
	printf("Producer has finished. \n");
	return NULL;
}

void *Consumer_odd(void *rank){
	int k;
	while(1)
	{
		pthread_mutex_lock(&mutex);
		if(num_odd == 0){
			pthread_cond_wait(&is_odd, &mutex);
		}
		num_odd --;
		k = buffer;
		printf("Consumer_odd gets %d\n", k);
		pthread_cond_signal(&is_empty);
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}
	return NULL;
}

void *Consumer_even(void *rank){
	int k;
	while(1){
		pthread_mutex_lock(&mutex);
		if(num_even == 0){
			pthread_cond_wait(&is_even, &mutex);
		}
		num_even -- ;
		k = buffer;
		printf("Consumer_even gets %d\n",k);
		pthread_cond_signal(&is_empty);
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}	
	return NULL;
}
posted @ 2022-12-31 16:50  wsl_lld  阅读(42)  评论(0编辑  收藏  举报