目录
两个线程,一个生产者一个消费者
2k个线程,奇数消费者偶数生产者
2k个线程,每个既可以是生产者又可以是消费者
两个线程,一个生产者一个消费者
#include<iostream>
#include<unistd.h>
#include <pthread.h>
#include <string.h>
#include <map>
using namespace std;
#define MAX 20
int number;
pthread_mutex_t mutex;
bool message_available=false;
void *consumer(void* arg){
cout<<"consumer\n";
while(1){
pthread_mutex_lock(&mutex);
if(message_available){
printf("consumer use message\n");
message_available=false;
}
pthread_mutex_unlock(&mutex);
}
}
void *producer(void* arg){
cout<<"producer\n";
while(1){
pthread_mutex_lock(&mutex);
if(!message_available){
//pthread_mutex_lock(&mutex);
printf("producer use message\n");
message_available=true;
//pthread_mutex_unlock(&mutex);
}
pthread_mutex_unlock(&mutex);
}
}
int main(){
pthread_t p1,p2;
pthread_mutex_init(&mutex,NULL);
pthread_create(&p1,NULL,consumer,NULL);
pthread_create(&p2,NULL,producer,NULL);
sleep(1);
pthread_mutex_destroy(&mutex);
return 0;
}
2k个线程,奇数消费者偶数生产者
#include<iostream>
#include<unistd.h>
#include <pthread.h>
#include <string.h>
#include <map>
using namespace std;
#define MAX 20
int number;
map<long long,long long> id_to_thread;
map<long long,long long> thread_to_id;
pthread_mutex_t mutex;
int message_available=0;
void *consumer(void* arg){
while(1){
pthread_mutex_lock(&mutex);
if(message_available){
message_available-=1;
printf("consumer index=%lld use message now messgae=%d\n",thread_to_id[pthread_self()],message_available);
}
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
void *producer(void* arg){
while(1){
pthread_mutex_lock(&mutex);
message_available+=1;
printf("producer index=%lld use message now messgae=%d\n",thread_to_id[pthread_self()],message_available);
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main(){
pthread_t p1[2005];
pthread_mutex_init(&mutex,NULL);
for(int i=1;i<=2000;++i){
if(i%2){
pthread_create(&p1[i],NULL,consumer,NULL);
}
else{
pthread_create(&p1[i],NULL,producer,NULL);
}
thread_to_id[p1[i]]=i;
}
sleep(2.1);
pthread_mutex_destroy(&mutex);
return 0;
}
2k个线程,每个既是生产者又可以是消费者 ,向下一个线程发送信号
#include<iostream>
#include<unistd.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#include <map>
using namespace std;
#define MAX 20
//producer
sem_t semp[2005];
//consumer
sem_t semc[2005];
int value[2005];
pthread_mutex_t mutex[2005];
map<long long,long long> id_to_thread;
map<long long,long long> thread_to_id;
void *user(void *arg){
sleep(1);
sem_wait(&semp[thread_to_id[pthread_self()]]);
value[(thread_to_id[pthread_self()]+1)%2000]=1;
printf("core index %lld send value\n",thread_to_id[pthread_self()]);
sem_post(&semc[(thread_to_id[pthread_self()]+1)%2000]);
sem_wait(&semc[thread_to_id[pthread_self()]]);
if(value[thread_to_id[pthread_self()]]==1){
printf("core index %lld accept value\n",thread_to_id[pthread_self()]);
}
else{
printf("error\n");
}
return NULL;
}
int main(){
for(int i=0;i<=1999;++i){
sem_init(&semp[i],0,1);
sem_init(&semc[i],0,0);
pthread_mutex_init(&mutex[i],NULL);
}
pthread_t t[2005];
for(int i=0;i<=1999;++i){
pthread_create(&t[i],NULL,user,NULL);
thread_to_id[t[i]]=i;
id_to_thread[i]=t[i];
}
for(int i=0;i<=1999;++i){
pthread_join(t[i],NULL);
}
for(int i=0;i<=1999;++i){
sem_destroy(&semp[i]);
sem_destroy(&semc[i]);
pthread_mutex_destroy(&mutex[i]);
}
return 0;
}