ProcedureReturn of a Thread

Mac OSX specific forum
Wolfram
Enthusiast
Enthusiast
Posts: 610
Joined: Thu May 30, 2013 4:39 pm

ProcedureReturn of a Thread

Post by Wolfram »

Hi!
Can someone explain me how to get the ProcedureReturn of a Thread?

Thanks

Wolfram
macOS Catalina 10.15.7
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: ProcedureReturn of a Thread

Post by ts-soft »

Threads do not have the concept of "return value" because threads run asynchronously.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: ProcedureReturn of a Thread

Post by luis »

Maybe you should explain what you are trying to accomplish.

Anyway, procedurereturn return a value to the caller of the procedure.
A procedure started in its own thread through CreateThread() is not "called", so how it could return something ? To what ? To whom ? Think about it.
This suggest you have to use some form of communication between the main process and its threads.
This could be a global variable, a message, or something else.

Threads are one of the most complex subjects, experiment a lot before using them in a real program, and consider carefully if they are really needed for what you are doing.
"Have you tried turning it off and on again ?"
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: ProcedureReturn of a Thread

Post by luis »

But to give you a general idea:

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()
Threadsafe is not really needed in this example, but when unsure better enable it.
"Have you tried turning it off and on again ?"
Wolfram
Enthusiast
Enthusiast
Posts: 610
Joined: Thu May 30, 2013 4:39 pm

Re: ProcedureReturn of a Thread

Post by Wolfram »

I'm running a thread and depended of its result I want to open a MassageRequester, FileRequester or another MassageRequester .

like this...

if threadresult = 2
MessageRequester("Error 1", "xyz")
elseif threadresult = 1
SaveFileRequester("save file", "my file", "*.txt", 0)
else
MessageRequester("Error 2", "abc")
endif
macOS Catalina 10.15.7
Wolfram
Enthusiast
Enthusiast
Posts: 610
Joined: Thu May 30, 2013 4:39 pm

Re: ProcedureReturn of a Thread

Post by Wolfram »

luis wrote:But to give you a general idea:

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()
Threadsafe is not really needed in this example, but when unsure better enable it.
Is there also a way to send an EventType at the end of the Thread.
So I can ask for the Result in my MainWindow loop instead of WaitThread()?
macOS Catalina 10.15.7
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: ProcedureReturn of a Thread

Post by luis »

Yes, just define something like

#My_Event_Thread_Completed = #PB_Event_FirstCustomValue

and use

PostEvent(#My_Event_Thread_Completed)

in the thread.
"Have you tried turning it off and on again ?"
Post Reply