#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include <time.h>
using namespace std;
int thread_count;
int send_max;
int done_sending;
struct MesgQueue {
int *mesg;
int enqueued, dequeued;
omp_lock_t front_mutex, back_mutex;
};
struct MesgQueue* Msg;
void Enqueue(int dest, int mesg) {
int cur_p = omp_get_thread_num();
printf("Thread %d send message %d to %d success!\n",cur_p, mesg, dest);
Msg[dest].mesg[Msg[dest].enqueued] = mesg;
Msg[dest].enqueued++;
}
void init(MesgQueue* MQ) {
MQ->mesg = new int[send_max];
MQ->dequeued = 0;
MQ->enqueued = 0;
omp_init_lock(&(MQ->front_mutex));
omp_init_lock(&(MQ->back_mutex));
}
void Dequeue(int dest) {
printf("Thread %d receive message %d success!\n", dest, Msg[dest].mesg[Msg[dest].dequeued]);
Msg[dest].dequeued++;
}
void Sen
openmp 生产者 消费者 实现
最新推荐文章于 2020-12-05 23:08:19 发布