好的,感谢您的所有建议。我设法得到了与我想要的相似的东西,它不一样,但会服务(^_^)
这是代码,它只是生成一个等待字符的线程,然后将报告数组从 0“切换”到 1。当每个 omp 线程开始每个“j”迭代时,它们将检查报告并在需要时报告.
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <pthread.h>
#include <ncurses.h>
unsigned char *report; // this will store whether a thread has to report or not
int max_threads;
void *check_keyboard(void *whatever)
{
int c;
int i;
newterm(NULL,stdin,stdout); // I want to still see the terminal :)
noecho();
cbreak();
while(1)
{
if(getch())
{
for(i = 0; i < max_threads; i++)
report[i]=1;
}
}
return NULL;
}
int main(int argc, const char *argv[])
{
int i,j;
int tid;
pthread_t thread0;
max_threads = omp_get_max_threads();
report = malloc(max_threads*sizeof(unsigned char));
pthread_create(&thread0,NULL,check_keyboard,NULL);
#pragma omp parallel private(i,j,tid) shared(report)
{
tid = omp_get_thread_num();
#pragma omp for
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
#pragma omp critical
{
if(report[tid])
{
fprintf(stderr,"Hello World from thread %d\n\r",tid);
fprintf(stderr,"This is iteration i: %d, j: %d\n\r",i,j);
report[tid] = 0;
}
}
sleep(1); // This 'is' a test for the long and ugly code
}
}
}
pthread_cancel(thread0);
pthread_exit(NULL);
return 0;
}