简体   繁体   中英

OpenMP threads, how to use omp atomic clauses correctly?

I'm trying to parallel a program that reads repetitively strings in pairs from a file. I want to use the omp atomic so as to make sure that in every loop the pair will be read correctly. I'm gonna use it like this in my code :

#pragma omp atomic 
{
if(a = getmystring(fp)) fprintf(fpw, "A: %s\n", a);
if(b = getmystring(fp)) fprintf(fpw, "B: %s\n", b);
}

My problem is that I don't know what is the difference between the clases (read,write, update, capture) so as to use the correct one. getmystring(fp) is the function that reads one string at a time from the file.

It looks like you've chosen the wrong OpenMP construct. The objective seems to be to allow only one thread to execute in your block at a time. That means the block is intended to be a critical region , and the directive to use for declaring it one is omp critical :

#pragma omp critical 
{
    if(a = getmystring(fp)) fprintf(fpw, "A: %s\n", a);
    if(b = getmystring(fp)) fprintf(fpw, "B: %s\n", b);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM