Page 1 of 1

problems in use threads in linux

Posted: Tue Sep 13, 2016 7:27 pm
by skinkairewalker
hi !
i am using ubuntu 16.06 , i have problems with threads when i use repeat until ...

Linux works with thread ?

Re: problems in use threads in linux

Posted: Tue Sep 13, 2016 7:42 pm
by infratec
Of course :!:

Re: problems in use threads in linux

Posted: Tue Sep 13, 2016 8:06 pm
by skinkairewalker
infratec wrote:Of course :!:
i am trying this example code to test !

Code: Select all

OpenConsole("")

Procedure loop(*val)
  Global a.i = 0  
  Repeat  
    PrintN("loop 1 working")
    Delay(500)
  Until a = 1
EndProcedure  

Procedure loop2(*val)
  Global b.i = 0  
  Repeat  
    PrintN("HelloWorld")
    Delay (1000)
  Until b = 1
EndProcedure  
CreateThread(@loop(),1)
CreateThread(@loop2(),2)

but dont works !!!
when i start this application ... it close in same moment !!!

Re: problems in use threads in linux

Posted: Tue Sep 13, 2016 9:52 pm
by ts-soft
If you close the mainthread (process), the threads are stopped.

Code: Select all

OpenConsole("")

Procedure loop(*val)
  Global a.i = 0 
  Repeat 
    PrintN("loop 1 working")
    Delay(500)
  Until a = 1
EndProcedure 

Procedure loop2(*val)
  Global b.i = 0 
  Repeat 
    PrintN("HelloWorld")
    Delay (1000)
  Until b = 1
EndProcedure 
CreateThread(@loop(),1)
CreateThread(@loop2(),2) 

Repeat
Delay(1000)
ForEver

Re: problems in use threads in linux

Posted: Wed Sep 14, 2016 12:43 am
by Keya
and remember that Linux and OSX aren't as forgiving as Windows when it comes to doing GUI updates from threads :D Im guessing that means PrintN() too!? That was a rude shock to me at first because Windows really is amazingly forgiving in that sense, lol, but I'm glad because what i was doing in Windows was seemingly technically incorrect/not best practice. All GUI updates should be done by the main (message loop) thread, with worker threads telling the main thread what to do, typically using PostEvent with a value => #PB_Event_FirstCustomValue (this works smoothly on Windows, Linux, and OSX)

Code: Select all

EnableExplicit

Enumeration #PB_Event_FirstCustomValue
  #UpdateProgress
  #UpdateFinished
EndEnumeration

Procedure workerthread(WndId)
  Protected progress = 0
  Repeat
    Delay (20) ;simulate some work
    progress + 1
    PostEvent(#UpdateProgress,WndId,#PB_Ignore,#PB_Ignore,progress)  ;send update msg
  Until progress => 100
  PostEvent(#UpdateFinished,WndId,#PB_Ignore,#PB_Ignore,0)           ;send finish msg
EndProcedure

OpenWindow(0, 0, 0, 640, 80, "Worker-Thread GUI Demo", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ProgressBarGadget(1, 12, 30, 620, 20, 0, 100)
ButtonGadget(2, 1, 1, 50, 20, "Start")

Define Event
Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #UpdateProgress  ;worker thread has signaled the main thread to update the progress
      SetGadgetState(1, EventData())
    Case #UpdateFinished
      MessageRequester("Done","Finished!")
    Case  #PB_Event_Gadget
      If EventGadget() = 2   ;Start button
          CreateThread(@workerthread(), WindowID(0))
      EndIf
  EndSelect
Until Event = #PB_Event_CloseWindow

Re: problems in use threads in linux

Posted: Wed Sep 14, 2016 12:53 pm
by skinkairewalker
thanks answers everyone !! :D :D

so , how can i make multi windows communicate among themselves ? ^^

thanks by attention !!

Re: problems in use threads in linux

Posted: Wed Sep 14, 2016 1:40 pm
by ts-soft
skinkairewalker wrote:so , how can i make multi windows communicate among themselves ? ^^
You can't! You have to create all GUI in the mainthread.