Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Atomic Function Calls

Status
Not open for further replies.

Travis214

Programmer
May 19, 2009
9
CA
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.
 
Depends on your compiler. If you're using IVF, have a look at the IFCORE routines (the ones ending with QQ). It may have a mutex built in.

What are you using to make your program multithreaded?
 
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.
 
Don't quite understand.

If there is no context switch, int will never change value or do you mean instead of sleep, there is something that does computations and sets int.

Assuming it is the latter, how are you creating your threads? Is it using pthreads? If it is you could use pthread_mutex_lock.
 
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.
 
You should be able to use pthread_mutex_lock with gfortran since it comes from the same stable as gcc. It may or may not work with g95.

Don't think it is possible to make function calls atomic in Fortran.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top