Thread Routine Format

A thread routine is a function that runs in a separate thread from the main program and takes a single argument. In the first interface below, the argument is the address of a integer(4) variable. It will be accessed as an INTEGER(4) in the body of thread_proc because of the POINTER attribute.

Example

INTERFACE

integer(4) FUNCTION thread_proc(arg)

!DEC$ ATTRIBUTES STDCALL, ALIAS:"_thread_proc" :: thread_proc

integer(4),POINTER :: arg

END FUNCTION

END INTERFACE

In this second interface example, the argument is passed as an address of some undefined data in memory. You must create a pointer to a specific data type variable with the POINTER attribute and assign the lpThreadParameter argument to it in the body of the thread_proc routine. You can pass a pointer to an entire block of data with this method.

Example

INTERFACE

integer(4) FUNCTION thread_proc(lpThreadParameter)

!DEC$ ATTRIBUTES STDCALL, ALIAS:"_thread_proc" :: thread_proc

integer(INT_PTR_KIND()) lpThreadParameter

END FUNCTION

END INTERFACE

Example 1

Program TESTPROC0

use ifcore

use ifmt

INTERFACE

integer(4) FUNCTION Thread_Proc0(arg)

!DEC$ ATTRIBUTES STDCALL, ALIAS:"_thread_proc0" :: Thread_Proc0

integer(4),POINTER :: arg

END FUNCTION

END INTERFACE

integer(INT_PTR_KIND()) ThreadHandle

integer(INT_PTR_KIND()), PARAMETER :: security = 0

integer(INT_PTR_KIND()), PARAMETER :: stack_size = 0

integer(INT_PTR_KIND()) :: thread_id

integer(4) :: ivalue0 = 12345678

ThreadHandle = CreateThread(security,stack_size,Thread_Proc0,loc(ivalue0), &

CREATE_SUSPENDED, thread_id )

iretlog = SetThreadPriority(ThreadHandle, THREAD_PRIORITY_BELOW_NORMAL )

iretint = ResumeThread(ThreadHandle)

call sleepqq(100) ! let io complete

end

integer(4) function Thread_Proc0(arg)

USE IFCORE

USE IFMT

!DEC$ ATTRIBUTES STDCALL, ALIAS:"_thread_proc0" :: Thread_Proc0

integer(4), POINTER :: arg

write(6,*) "The value of the Thread_Proc0 argument is ",arg

Thread_Proc0 = 0

call ExitThread(0)

end function

The resulting output will be similar to the following example:

The value of the Thread_Proc0 argument is 12345678

Example 2

Program TESTPROC1

use ifcore

use ifmt

INTERFACE

integer(4) FUNCTION Thread_Proc1(lpThreadParameter)

!DEC$ ATTRIBUTES STDCALL,ALIAS:"_thread_proc1" :: Thread_Proc1

integer(INT_PTR_KIND()) lpThreadParameter

END FUNCTION

END INTERFACE

integer(INT_PTR_KIND()) ThreadHandle1

integer(INT_PTR_KIND()), PARAMETER :: security = 0

integer(INT_PTR_KIND()), PARAMETER :: stack_size = 0

integer(INT_PTR_KIND()) :: thread_id

integer(4) :: ivalue1(5) = (/1,2,3,4,5/)

ThreadHandle1 = CreateThread(security,stack_size,Thread_Proc1,loc(ivalue1(1)), &

CREATE_SUSPENDED, thread_id )

iretlog = SetThreadPriority(ThreadHandle1, THREAD_PRIORITY_BELOW_NORMAL )

iretint = ResumeThread(ThreadHandle1)

call sleepqq(100) ! let IO complete

end

integer(4) function Thread_Proc1(lpThreadParameter)

USE IFCORE

USE IFMT

!DEC$ ATTRIBUTES STDCALL, ALIAS:"_thread_proc1" :: Thread_Proc1

integer(INT_PTR_KIND()) lpThreadParameter

integer(4) arg(5)

POINTER(parg,arg)

parg = lpThreadParameter

write(6,*) "The value of the Thread_Proc1 argument is ",arg

Thread_Proc1 = 0

call ExitThread(0)

end function

The resulting output will be similar to the following example:

The value of the Thread_Proc1 argument is 1 2 3 4 5