スレッドルーチンの書式

スレッドルーチンは、メインプログラムとは別のスレッドで実行され、1 つの引数を受け取る関数です。次のインターフェイスでは、integer(4) 変数のアドレスを引数とします。引数は、POINTER 属性を使用するため、thread_proc 内で INTEGER(4) として参照されます。

INTERFACE

 integer(4) FUNCTION thread_proc(arg)

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

 integer(4),POINTER :: arg

 END FUNCTION

END INTERFACE

次のインターフェイスの例では、引数はメモリー内にあるいくつかの未定義データのアドレスとして渡されます。POINTER 属性を使用して、特定のデータ型変数のポインターを作成し、thread_proc ルーチン内で lpThreadParameter 引数をそのポインターに割り当てる必要があります。このメソッドを使用して、ポインターをデータブロック全体に渡すことができます。

INTERFACE

  integer(4) FUNCTION thread_proc(lpThreadParameter)

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

  integer(INT_PTR_KIND()) lpThreadParameter

 END FUNCTION

END INTERFACE

例 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 value of the Thread_Proc0 argument is 12345678

例 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 value of the Thread_Proc1 argument is 1 2 3 4 5