Operating System - OpenVMS
cancel
Showing results for 
Search instead for 
Did you mean: 

pthread_cleanup_push in FORTRAN

Michael Menge
Frequent Advisor

pthread_cleanup_push in FORTRAN

Working on Alpha/IA64 V8.3 I have a FORTRAN V8.0 program which creates a thread (to assign some computations to a second cpu) using the pthread library (pthread_create).
Thread creator and thread are both FORTRAN90 subroutines. When the thread is no longer required, the creator calls pthread_cancel.
Before the thread exits it has to release some resources like locked mutexes. For this purpose there is a pthread_cleanup_push function in the pthread interface, but it is implemented as a C macro so cannot be called from a FORTRAN subroutine (guide to POSIX Thread Library, pthread-54).
How can I implement this macro in FORTRAN? Both examples in the documentation are writen in C.
2 REPLIES
Steven Schweda
Honored Contributor

Re: pthread_cleanup_push in FORTRAN

I know nothing, but I'd guess that the easy
way would be to write C wrapper functions
which _could_ use the C macros (in pthread.h
and/or pthread_exception.h). Otherwise, I'd
probably try to draw out what these macros
would get expanded into, and see if it's
practical to re-code the resulting mess in
Fortran. (Looks nasty to me, but I'd assume
that it's possible, at least until I actually
sat down and tried to do it.)
John Gillings
Honored Contributor

Re: pthread_cleanup_push in FORTRAN

Michael,

FORTRAN isn't really the best choice of language for playing with threads. Although FORTRAN90 has come a long way, (some would say to turning FORTRAN into PASCAL ;-) there are still quite a few FORTRANish things like static variables and, in some case, argument lists which can make threading very difficult to use reliably. You need to be aware of any reentrancy and sharing of storage. Stack based languages make it all much simpler.

If you can't implement your threading stuff in C, you will need to reimplement the macro in FORTRAN. First locate where the definition comes from (if you have a C compiler, generate a listing with expanded includes and macros), now all you have to do is work out what the macro does! Of course we all know that C code is always readable :-)
A crucible of informative mistakes