Abstraction layer of POSIX threads. Has to be linked with -lpthread
.
The thread routine must match the C-interoperable abstract interface
dm_thread_callback(arg)
, for example:
subroutine thread_callback(arg) bind(c)
!! C-interoperable POSIX thread routine.
use, intrinsic :: iso_c_binding
type(c_ptr), intent(in), value :: arg !! C pointer to client data.
integer, pointer :: i !! Fortran pointer to client data.
if (.not. c_associated(arg)) return
call c_f_pointer(arg, i)
print '("value: ", i0)', i
end subroutine thread_callback
The dummy argument arg
can be of any type. The thread routine and the
argument have to be passed to the create function:
integer, target :: arg
integer :: rc
type(thread_type) :: thread
arg = 123
rc = dm_thread_create(thread, thread_callback, arg)
rc = dm_thread_join(thread)
The functions return E_SYSTEM
on error.
C-interoperable POSIX thread routine.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(c_ptr), | intent(in), | value | :: | arg |
Client data as C pointer. |
Opaque POSIX thread type.
Creates POSIX thread. The function returns E_SYSTEM
on error.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(thread_type), | intent(out) | :: | thread |
Thread type. |
||
procedure(dm_thread_callback) | :: | callback |
Callback procedure of POSIX thread. |
|||
type(*), | intent(inout), | target | :: | arg |
Client data to be passed to thread procedure. |
Join POSIX thread, and optionally returns value as C pointer.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(thread_type), | intent(inout) | :: | thread |
Thread type. |
||
type(c_ptr), | intent(out), | optional | :: | value |
Returned thread value. |