This is my first attempt to use the SPAWN command. Can it be used on Android? Is busy waiting like this my only option? Is it best reserved for compute-bound things like long calculations, so calls to I/O or graphics don't clobber each other? Am I more or less on the right track?
This command is not fully implemented and at the moment messes up the
program execution stack of the interpreter since all internal control
structures are accessed by two threads. Anyway, in a natively compiled
program this can work as expected.
It definitively does not work on WINDOWS and ATARI/TOS.
On Android maybe. It is realized with the fork() command internally (and this does not behave like threads, which you probably have in mind.) and I am not sure if an app is allowed to fork. I see some comments about this, it works, but the recommendation is: Don't do this. Just don't.
Also maybe if it is working, it might not do what you expect it to do: Each of the spawned procedures has their own variables, so they cannot share them while they are running.
Sorry.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
No problem! So don't use SPAWN in anything other than gcc/tcc compiled programs for Unix/Linux? At least in the interpreter under Android and Windows, SPAWN appears to behave just like GOSUB instead of making the interpreter go haywire. But all the idle cores on my Android and Windows devices! :'-(
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well, to make use of more Processor-cores, I had the idea to use OpenMP wherever it makes sense, but, in the end, I thought: who needs that? It is a big effort for little gain. The FORK kommand works at least. And one can split the most critical tasks in several sub-programs which can be excecuted (with SYSTEM) in parallel, if needed. Interprocess communications via files (/run/shm/) or so. To keep it easy. But If you really want, and can provide help, one could improve X11-basic step by step also in that direction.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I had a few more bad ideas regarding SPAWN and maybe extending AFTER and EVERY to work with threads. Maybe it should be possible to pass additional parameters to SPAWN that would then be passed to the spawned proc, and then when threads exit, a proc in the main program could be triggered, if only to set a flag or interrupt a PAUSE. So, something like this,
For i% = 0 To n%
Spawn Worker, i%, j%, k%, ...
Next i%
oops% = True
After Threads AllWorkersDone
Pause 60
If oops%
Print "Timeout, threads are still running"
Else
Print "All threads completed"
EndIf
...or maybe something like,
oops% = True
i% = n%
Every Thread OneWorkerDone
Do
Print i%; " threads remaining"
Exit If 0 = %i
oops% = True
Pause 60
Exit if oops%
Loop
If 0 = i%
Print "All threads completed"
Else If oops%
Print "Timeout, "; i%; " threads are still running"
Else
Print "Threads are hard, let's go shopping!"
EndIf
Procedure Worker(i%, j%, k%, ...)
! do stuff
Return
Procedure AllWorkersDone
oops% = False
Return
Procedure OneWorkerDone
Dec i%
oops% = False
Return
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is my first attempt to use the SPAWN command. Can it be used on Android? Is busy waiting like this my only option? Is it best reserved for compute-bound things like long calculations, so calls to I/O or graphics don't clobber each other? Am I more or less on the right track?
Thanks!
Well, as said in the manual:
This command is not fully implemented and at the moment messes up the
program execution stack of the interpreter since all internal control
structures are accessed by two threads. Anyway, in a natively compiled
program this can work as expected.
It definitively does not work on WINDOWS and ATARI/TOS.
On Android maybe. It is realized with the fork() command internally (and this does not behave like threads, which you probably have in mind.) and I am not sure if an app is allowed to fork. I see some comments about this, it works, but the recommendation is: Don't do this. Just don't.
Also maybe if it is working, it might not do what you expect it to do: Each of the spawned procedures has their own variables, so they cannot share them while they are running.
Sorry.
No problem! So don't use SPAWN in anything other than gcc/tcc compiled programs for Unix/Linux? At least in the interpreter under Android and Windows, SPAWN appears to behave just like GOSUB instead of making the interpreter go haywire. But all the idle cores on my Android and Windows devices! :'-(
Maybe FORK will be good enough, if it and the SHM commands work reasonably on every platform of interest, hopefully also on Android.
Umm, wut
results in, at least in Windows 10 64-bit Version 10.0.19045.3803,
Where is MAP documented? Can mapped files be (ab)used to implement shared memory?
Well, to make use of more Processor-cores, I had the idea to use OpenMP wherever it makes sense, but, in the end, I thought: who needs that? It is a big effort for little gain. The FORK kommand works at least. And one can split the most critical tasks in several sub-programs which can be excecuted (with SYSTEM) in parallel, if needed. Interprocess communications via files (/run/shm/) or so. To keep it easy. But If you really want, and can provide help, one could improve X11-basic step by step also in that direction.
Oh no, this is starting to look too much like MapReduce
I had a few more bad ideas regarding SPAWN and maybe extending AFTER and EVERY to work with threads. Maybe it should be possible to pass additional parameters to SPAWN that would then be passed to the spawned proc, and then when threads exit, a proc in the main program could be triggered, if only to set a flag or interrupt a PAUSE. So, something like this,
For i% = 0 To n%
Spawn Worker, i%, j%, k%, ...
Next i%
oops% = True
After Threads AllWorkersDone
Pause 60
If oops%
Print "Timeout, threads are still running"
Else
Print "All threads completed"
EndIf
...or maybe something like,
oops% = True
i% = n%
Every Thread OneWorkerDone
Do
Print i%; " threads remaining"
Exit If 0 = %i
oops% = True
Pause 60
Exit if oops%
Loop
If 0 = i%
Print "All threads completed"
Else If oops%
Print "Timeout, "; i%; " threads are still running"
Else
Print "Threads are hard, let's go shopping!"
EndIf
Procedure Worker(i%, j%, k%, ...)
! do stuff
Return
Procedure AllWorkersDone
oops% = False
Return
Procedure OneWorkerDone
Dec i%
oops% = False
Return