#include "stdio.h" #include<iostream> using std::cin; using std::cout; using std::endl; #include<time.h> #include<stdlib.h> #include <unistd.h> int main() { int nthreads, tid; int nprocs; char buf[32]; omp_set_num_threads(11); int q[20]; for(int i=0;i<20;i++) q[i]=rand()%1000000; int wait=1; int current=0; #pragma omp parallel private(nthreads, tid) { tid = omp_get_thread_num(); if (tid==0) { int c=0; while(c<19){ printf("question %d : %d + %d = ?n",c/2+1,q[c],q[c+1]); c+=2; } wait=0; }else{ #pragma omp critical(a) { while(true){ if(wait==0){ printf("my student number is %d question%d's answer is %d n",tid,current/2+1,q[current]+q[current+1]); current+=2; break; } } } } } return 0; }
|
近期评论