Page 1 of 2
Threads and Procedures
Posted: Fri Jul 29, 2022 1:28 am
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
Re: Threads and Procedures
Posted: Fri Jul 29, 2022 2:46 am
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?
Re: Threads and Procedures
Posted: Fri Jul 29, 2022 6:37 am
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)
Re: Threads and Procedures
Posted: Fri Jul 29, 2022 11:44 am
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
Re: Threads and Procedures
Posted: Fri Jul 29, 2022 12:26 pm
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
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 /
Re: Threads and Procedures
Posted: Fri Jul 29, 2022 12:44 pm
by juergenkulow
Threaded
Please send a running example.
Re: Threads and Procedures
Posted: Fri Jul 29, 2022 1:37 pm
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.
Re: Threads and Procedures
Posted: Fri Jul 29, 2022 2:45 pm
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().
Re: Threads and Procedures
Posted: Sat Jul 30, 2022 6:27 pm
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
Re: Threads and Procedures
Posted: Mon Aug 01, 2022 5:50 pm
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?
Re: Threads and Procedures
Posted: Mon Aug 01, 2022 5:56 pm
by jacdelad
Post a custom event into the main loop.
Re: Threads and Procedures
Posted: Mon Aug 01, 2022 8:05 pm
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.
Re: Threads and Procedures
Posted: Mon Aug 01, 2022 8:25 pm
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.
Re: Threads and Procedures
Posted: Mon Aug 01, 2022 8:58 pm
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.
Re: Threads and Procedures
Posted: Mon Aug 01, 2022 9:40 pm
by jacdelad
Hm, ok, I only use Windows. I thought using PostMessage would be inteded to be used with every platform...