Is there a way to make a function/subroutine atomic? I am attempting to create mutex like structures within Fortran but for them to be proper I need to use atomic function calls.
I am using the GNU compiler to compile my program. To make it multithreaded I have a multithreaded C++ program that calls fortran functions. Each C++ thread passes its fortran application an integer so that only one of the fortran functions will open a file at a time(they write several lines and they used to be interlaced which is undesirable). I do have it working this way by doing the following in a method:
Code:
do while(int.EQ.1)
sleep(50)
enddo
int = 1
(or something like this, I don't actually have my code in front of me). The problem is that if there is a context switch between the enddo and the int = 1 then there can be huge problems (which I have had happen in C programs before). I need to have the lock method run to completion once it has been started.
Yes if there is a context switch int will not be set but that is the problem. If another thread then sets int to 1 and gets ready to write to the file the original thread can then come back and also set int to 1. Both threads will then have a lock on the file which is what the variable int is trying to stop.
I am using pthreads to do my multithreding and spent some time researching it but could not find clarification on whether or not pthread functions (like pthread_mutex_lock) could be used with fortran. If they can be than that is a very simple solution to my problem, although I am still curious as to whether a function call in fortran can be made atomic.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.