gpt4 book ai didi

c - 如何在 OpenMP 中获取和释放锁

转载 作者:行者123 更新时间:2023-11-30 14:58:17 29 4
gpt4 key购买 nike

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

#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

请帮忙

最佳答案

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

第一步是删除 test_lock 循环中的输出。然后只需将 test 循环替换为单个 omp_set_lock 即可。

但这仍然不能保证读/写的顺序。如果您想实现真正的生产者/消费者,您应该使用适当的队列来处理它。请注意,OpenMP 并不是特别适合实现生产者/消费者程序,因此您不会从中获得很多帮助。

关于c - 如何在 OpenMP 中获取和释放锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43410664/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com