ProcedureReturn of a Thread
Posted: Sun Jan 26, 2014 12:49 pm
Hi!
Can someone explain me how to get the ProcedureReturn of a Thread?
Thanks
Wolfram
Can someone explain me how to get the ProcedureReturn of a Thread?
Thanks
Wolfram
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
CompilerIf (#PB_Compiler_Thread = 0)
CompilerError "Please enable THREADSAFE in the compiler options"
CompilerEndIf
Structure ThreadData
num_a.i
num_b.i
result.i
EndStructure
Procedure TProc(*p.ThreadData)
Debug "[THREAD] starting to work on the problem"
Delay(1000)
Debug "[THREAD] almost there..."
Delay(1000)
*p\result = *p\num_a + *p\num_b
Debug "[THREAD] done !"
EndProcedure
Procedure Main()
Protected param.ThreadData ; communication structure
Protected TID ; thread id
param\num_a = 10
param\num_b = 3
; the thread will calculate the sum of 10 + 3
TID = CreateThread(@TProc(), @param)
Debug "[MAIN PROCESS] waiting ..."
WaitThread(TID)
Debug "[MAIN PROCESS] Thread has finished, result is = " + param\result
EndProcedure
Main()
Is there also a way to send an EventType at the end of the Thread.luis wrote:But to give you a general idea:
Threadsafe is not really needed in this example, but when unsure better enable it.Code: Select all
CompilerIf (#PB_Compiler_Thread = 0) CompilerError "Please enable THREADSAFE in the compiler options" CompilerEndIf Structure ThreadData num_a.i num_b.i result.i EndStructure Procedure TProc(*p.ThreadData) Debug "[THREAD] starting to work on the problem" Delay(1000) Debug "[THREAD] almost there..." Delay(1000) *p\result = *p\num_a + *p\num_b Debug "[THREAD] done !" EndProcedure Procedure Main() Protected param.ThreadData ; communication structure Protected TID ; thread id param\num_a = 10 param\num_b = 3 ; the thread will calculate the sum of 10 + 3 TID = CreateThread(@TProc(), @param) Debug "[MAIN PROCESS] waiting ..." WaitThread(TID) Debug "[MAIN PROCESS] Thread has finished, result is = " + param\result EndProcedure Main()