Threads and Procedures

Just starting out? Need help? Post your questions and find answers here.
vwidmer
Enthusiast
Enthusiast
Posts: 286
Joined: Mon Jan 20, 2014 6:32 pm

Threads and Procedures

Post by vwidmer »

Sorry, if this is a.. its right there in the manual question, but threads drive me crazy, so hope someone can help me explain a little.

If I have a program like

Code: Select all

Procedure doSOMETHING(DATA$)
	***code to dosomething**
	ProcedureReturn DATA$
EndProcedure[

Procedure getURL(URL$)
	***code to get url**
	ProcedureReturn doSOMETHING(DATA$)
EndProcedure[

Procedure YourProcedure(*Value)
Repeat
	getURL(Value)
	Delay(1000)    
  ForEver
EndProcedure

  CreateThread(@YourProcedure(), "SOMEURL")
  CreateThread(@YourProcedure(), "SOMEOTHERURL")
I know the above code is incomplete.

My problem is that it sometimes seems one of the threads will overwrite the other when its running one of the procedures. How can I stop that from happening, but still allow them to run independently and not have to wait?

Hope someone understands what I am asking, and I didn't overcomplicate it.

Thanks
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
User avatar
jacdelad
Addict
Addict
Posts: 1992
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Threads and Procedures

Post by jacdelad »

Hi vwidmer,
I don't know if I get the task right and your example has a lot of errors. Try this:

Code: Select all

Procedure.s doSOMETHING(Data$)
  ;***code To dosomething**
  ProcedureReturn Data$
EndProcedure

Procedure.s getURL(URL$)
  ;***code To get url**
  ProcedureReturn doSOMETHING(URL$)
EndProcedure

Procedure YourProcedure(*Value)
  Repeat
    Debug getURL(PeekS(*Value))
    Delay(1000)    
  ForEver
EndProcedure

CreateThread(@YourProcedure(), @"SOMEURL")
CreateThread(@YourProcedure(), @"SOMEOTHERURL")
Repeat
  Delay(1)
ForEver
- Don't give a string as a parameter to a thread-procedure, use the address of the string.
- "YourProcedure" receives "*Value", but utilizes "Value", which is a different variable. You can prevent this by using "EnableExplicit". The same applies to the "getURL"-function: one variable "URL$" (which contains the string) and one "DATA$" (which is empty).
- "getURL" needs to peek (see point one on this list).
- Procedures that return strings must be declared as "Procedure.s".
- You don't wait at the end, so the program is terminated. I added a simple forever-loop, so please don't compile it into a program and abort it via IDE.

Is this helpful?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Threads and Procedures

Post by STARGÅTE »

Some additionals from my side:
If a threaded procedure should run independently, all resources have to pass as a real copy to the thread (procedure).
This means, also variable strings have to be copied, instead of just using @String, if the Thread wants to read this string and at the same time the main program changes this string after starting the thread.
For example (main program)

Code: Select all

CreateThread(@AnyProcedure(), UTF8(MyString))
and the Thread:

Code: Select all

Procedure AnyProcedure(*StringPointer)
  LocalString = PeekS(*StringPointer, -1, #PB_UTF8)
  FreeMemory(*StringPointer)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Olli
Addict
Addict
Posts: 1199
Joined: Wed May 27, 2020 12:26 pm

Re: Threads and Procedures

Post by Olli »

I am absolutely agree with stargate. A common data block must be shared between several threads. We can also imagine the threads are as any bus. The time is explicite, with several period scales.

Code: Select all

Structure townCenter
   *TheTimes
   flashInfos.s{256}
   *TrainHours
   *busHours
EndStructure

Procedure myAction(*this.townCenter)
   With *this
      If \TheTimes ; whatever
         ; ...
      EndIf  ; whatever
   EndWith
EndProcedure
vwidmer
Enthusiast
Enthusiast
Posts: 286
Joined: Mon Jan 20, 2014 6:32 pm

Re: Threads and Procedures

Post by vwidmer »

Sorry, I think my question got confusing. I am not having problem passing the thread procedure.

My question is on the "doSOMETHING" and "getURL" procedures.

I will try again an example

so once the threads are running.

They call getURL procedure

if I have in that procedure

like 2

Code: Select all

Debug URL$
Debug URL$
Most of the time they will be the same, but occasionally, I get it showing one from each procedure.
Like the getURL procedure is being run together with the 2 threads rather than running independently.

So I am expecting or thinking it works something like this:

- - - - - - - - / getURL --- doSOMETHING
Thread1 /
Thread2 \
- - - - - - - - \ getURL --- doSOMETHING


But It seems like its sometimes doing like

Thread1 \
- - - - -- - - > getURL --- doSOMETHING
Thread2 /
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: Threads and Procedures

Post by juergenkulow »

Threaded
Please send a running example.
vwidmer
Enthusiast
Enthusiast
Posts: 286
Joined: Mon Jan 20, 2014 6:32 pm

Re: Threads and Procedures

Post by vwidmer »

I seen this example in the help already.

I am asking if procedures called from multiple threads overlap and how to prevent it allowing all the threads to run.
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Threads and Procedures

Post by skywalk »

You need to slow down and run the most basic threaded examples from mk-soft, infratec, TI-994A and others.
My suggestion is focus on Semaphores. Mutexes are limited in scope.
Of course your threads will run independently but with Semaphores, you can control their behavior with the granularity of your code.
SignalSemaphore() / WaitSemaphore().
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Threads and Procedures

Post by mk-soft »

1. Use always compiler option thread-safe.
2. Do never change GUI objects from thread (crash)
3. Exiting Threads before end program.
4. Use not KillThread for exiting thread (Only if thread hangs)

Examples: Mini Thread Control
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: Threads and Procedures

Post by AZJIO »

mk-soft wrote: Sat Jul 30, 2022 6:27 pm 2. Do never change GUI objects from thread (crash)
Linux = crash
Windows = not crash

And how then to change the status bar during data processing?
User avatar
jacdelad
Addict
Addict
Posts: 1992
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Threads and Procedures

Post by jacdelad »

Post a custom event into the main loop.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: Threads and Procedures

Post by AZJIO »

jacdelad wrote: Mon Aug 01, 2022 5:56 pm Post a custom event into the main loop.
Does not work

Works if the event has ended. If you have a long processing time and during the process you need to display information to the user at what stage the process is, then only the last line will enter the status line, perhaps all the rest will be in the queue and will be inserted in the last microsecond. That is, the user cannot monitor the process, for example, "files are being scanned", then "MD5 calculation", then "MD5 comparison", then "Finish". Instead, nothing will be output, and it will output "Done" at the end.
User avatar
jacdelad
Addict
Addict
Posts: 1992
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Threads and Procedures

Post by jacdelad »

Erm...a quick example would be helpful. I post events over events in my threads and it works fine. Also this is the ways it shall be done.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: Threads and Procedures

Post by AZJIO »

Try the program: "Search duplicates" https://www.purebasic.fr/english/viewtopic.php?t=79382
Use source: "Search_duplicates (Linux) En.pb"
In the "Compare" procedure, uncomment the lines "StatusBarText(#StatusBar, ...".

Perform a search, it is not necessary to do a delete, you will simply get a list of duplicates without changing anything in your file system, but at the same time, see if you get a status bar output for intermediate steps. Another option made using thread (Search_duplicates +Thread (Windows) En.pb) outputs as expected. There is nothing special, sequential actions, they do not interfere with the output in any way, they are simply executed under one button press event and there will be no output until the event ends.
User avatar
jacdelad
Addict
Addict
Posts: 1992
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Threads and Procedures

Post by jacdelad »

Hm, ok, I only use Windows. I thought using PostMessage would be inteded to be used with every platform...
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply