Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29391 Discussions

Accessing data from secondary thread

Ilie__Daniel
Beginner
403 Views
How can I access the data I need from a secondary thread?

Consider this program:

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(4) :: ivalue1(5) = (/1,2,3,4,5/)

ThreadHandle1 = CreateThread(0,0,Thread_Proc1,loc(ivalue1(1)),CREATE_SUSPENDED, j )
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


This example (found in the documentation) works fine.
But what if the dimension ofivalue1 array is not known until some stage in the main thread? Then the array is allocated and filled inthe main thread. I do not have its explicit size in the secondary thread.

So I thought I could pass the pointer to it and its size in another array (called vector), like this:

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(4) :: ivalue1(5) = (/1,2,3,4,5/)
integer(INT_PTR_KIND()), dimension(2) :: vector

vector(1) = loc(ivalue1(1))
vector(2) = 5

ThreadHandle1 = CreateThread(0,0,Thread_Proc1,loc(vector(1)), CREATE_SUSPENDED, j )
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(INT_PTR_KIND())vector(2)
POINTER(parg,vector)

integer, dimension(*) :: store
integer :: pstore
pointer(pstore,store)

parg = lpThreadParameter

! At his stage I know the contents of array vector
! 1. the address to ivalue1
! 2. the size of ivalue1
! What do I do to read ivalue1 here?
! The following does not seem to do the job(code in italics)

pstore = malloc(vector(2)*4)
pstore = vector(1)

! At this stage I should be able to access myivalue1 from store but I can only get the first element
! Do you have any ideas on how to do this correctly?

Thread_Proc1 = 0

call ExitThread(0)
end function



Thank you for any suggestions.
0 Kudos
0 Replies
Reply