Reputation: 757
I am using openmp and my program looks like as follows:
\#pragma omp parallel for
for(x = 0, y = 0, x < 5, x++, y++)
function(x, y, fp);
void function(int x , int y, FILE* fp);
{
fprintf(fp, "(%d, %d)\n", x y);
}
I want that the content of file as
(0, 0)
(2, 2)
(1, 1)
(3, 3)
(4, 4)
The ordering doesn't matter but the coordinates x, y should be in order, ie the program should not generate something like (2, 3). Is this behavior always guaranteed? I am using gcc compiler on linux.
Upvotes: 5
Views: 1480
Reputation: 78963
You have incompatible assumptions in your question. OpenMp is not part of the C standard, so the C specification can't say anything about the thread model of OpenMp and ensure anything about safety of its proper functions. Until recently C didn't even have a thread model.
C11 now has its own thread model, and in that thread model the functions that operate on IO streams are thread safe:
Each stream has an associated lock that is used to prevent data races when multiple threads of execution access a stream, and to restrict the interleaving of stream operations performed by multiple threads. Only one thread may hold this lock at a time. The lock is reentrant: a single thread may hold the lock multiple times at a given time.
I don't think that there is yet a compiler out there that implements C11 fully, but typically the C library on POSIX systems would fulfill this particular requirement. When there will be such a complying implementation, it would be up to the OpenMp implementation that would sit on top of it to document if its thread model would be consistent with the one of C11.
Upvotes: 3