problems in use threads in linux

Just starting out? Need help? Post your questions and find answers here.
User avatar
skinkairewalker
Addict
Addict
Posts: 810
Joined: Fri Dec 04, 2015 9:26 pm

problems in use threads in linux

Post by skinkairewalker »

hi !
i am using ubuntu 16.06 , i have problems with threads when i use repeat until ...

Linux works with thread ?
infratec
Always Here
Always Here
Posts: 7713
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: problems in use threads in linux

Post by infratec »

Of course :!:
User avatar
skinkairewalker
Addict
Addict
Posts: 810
Joined: Fri Dec 04, 2015 9:26 pm

Re: problems in use threads in linux

Post 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 !!!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: problems in use threads in linux

Post 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
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
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: problems in use threads in linux

Post 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
User avatar
skinkairewalker
Addict
Addict
Posts: 810
Joined: Fri Dec 04, 2015 9:26 pm

Re: problems in use threads in linux

Post by skinkairewalker »

thanks answers everyone !! :D :D

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

thanks by attention !!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: problems in use threads in linux

Post 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.
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
Post Reply