-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.f90
70 lines (55 loc) · 2.09 KB
/
thread.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module util
use, intrinsic :: iso_c_binding
implicit none
interface
! extern int my_pthread_create(pthread_t *thread, void *(*start_routine)(void *), void *arg)
function my_pthread_create(start_routine, arg, rc) bind(c, name='my_pthread_create')
import :: c_int, c_ptr, c_funptr
implicit none
type(c_funptr), intent(in), value :: start_routine
type(c_ptr), intent(in), value :: arg
type(c_ptr) :: my_pthread_create
integer(c_int) :: rc
end function my_pthread_create
! extern int my_pthread_detach(pthread_t thread)
function my_pthread_detach(thread) bind(c, name='my_pthread_detach')
import :: c_int, c_ptr
implicit none
! type(my_pthread_t), intent(in), value :: thread
type(c_ptr), intent(in), value :: thread
integer(kind=c_int) :: my_pthread_detach
end function my_pthread_detach
! extern int my_pthread_join(pthread_t thread)
function my_pthread_join(thread) bind(c, name='my_pthread_join')
import :: c_int, c_ptr
implicit none
! type(my_pthread_t), intent(in), value :: thread
type(c_ptr), intent(in), value :: thread
integer(kind=c_int) :: my_pthread_join
end function my_pthread_join
end interface
contains
recursive subroutine foo(arg) bind(c)
type(c_ptr), intent(in), value :: arg ! Client data.
real :: r
! get a random interval between 1 and 10 seconds
call random_number(r)
call sleep(nint(10*r))
print *, "foo"
end subroutine foo
end module util
program main
use, intrinsic :: iso_c_binding
! use :: unix_pthread
use :: util
implicit none
integer :: rc
type(c_ptr) :: tid
print '(a)', 'Starting a thread ...'
tid = my_pthread_create(start_routine=c_funloc(foo), arg=c_null_ptr, rc=rc)
print *, "my_pthread_create::rc", rc
print *, "my_pthread_create::tid", tid
print '(a)', 'Joining a thread ...'
rc = my_pthread_join(tid)
print *, "my_pthread_join::rc", rc
end program main