如何在OpenMP中获取和释放锁

时间:2017-04-14 11:30:26

标签: c openmp

这是我的程序,它创建了一定数量的并行线程,并且每个读取器线程都无法在任何写入器线程写入时读取数据

#include<omp.h>
#include<stdio.h>
#include<stdlib.h>

omp_lock_t rw_lock;
char data[50];

void readData(int tid){

    while(!omp_test_lock(&rw_lock))
        printf("\n[Thread %d - Reader]: Waiting for data ...",tid);
    printf("\n[Thread %d - Reader]:\t%s",tid,data);
    omp_unset_lock(&rw_lock);

}

void writeData(int tid){

    while(!omp_test_lock(&rw_lock))
        printf("\n[Thread %d - Writer]: Waiting for data ...",tid);
    printf("\n[Thread %d - Writer]\n\tEnter new data:\t",tid);
    scanf(" %[^\n]49s",&data);
    omp_unset_lock(&rw_lock);
}

int main(){

    int read_cnt,write_cnt,wrote=0;

    setbuf(stdout,NULL);

    printf("\nEnter data:\t");
    scanf(" %[^\n]49s",&data);

    printf("\nEnter total Reader and Writer thread count:\t");
    scanf(" %d%d",&read_cnt,&write_cnt);
    omp_init_lock(&rw_lock);

    #pragma omp parallel for num_threads(read_cnt+write_cnt)
    for(int i=0; i<(read_cnt+write_cnt);i++){
        int tid = omp_get_thread_num();
        if(i<read_cnt){
            readData(tid);
        }else
            writeData(tid);
    }
    omp_destroy_lock(&rw_lock);
    return 0;
}

使用的命令

gcc -fopenmp readerwriter.c

此程序仅适用于1个作者线程:

Enter data: test word

Enter total Reader and Writer thread count: 4 1

[Thread 0 - Reader]: Waiting for data ...
[Thread 1 - Reader]:    test word
[Thread 3 - Reader]: Waiting for data ...
[Thread 0 - Reader]:    test word
[Thread 2 - Reader]: Waiting for data ...
[Thread 2 - Reader]:    test word
[Thread 3 - Reader]:    test word
[Thread 4 - Writer]
    Enter new data: hi

但是如果输入两个或更多个编写器线程:

[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...
[Thread 0 - Reader]: Waiting for data ...^C

请帮忙

1 个答案:

答案 0 :(得分:1)

未定义读者/作者获取锁定的顺序。在您的“坏”情况下,您根本看不到“输入新数据:”提示,因为读取器线程在尝试获取锁定时会向控制台发送垃圾邮件。

第一步是摆脱test_lock循环中的输出。然后只需将test - 循环替换为每个omp_set_lock

但这仍然不能保证读/写的顺序。如果你想实现一个真正的生产者/消费者,你应该用适当的队列来处理它。请注意,OpenMP不太适合实现生产者/消费者程序,因此您无法从中获得大量帮助。