We would like to build a community for Small Basic programmers of any age who like to code. Everyone from total beginner to guru is welcome. Click here to register and share your programming journey!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threading
#3
Hi,

Threading in Small Basic is tricky, mainly because all variables are global scope, i.e. variables are the same everywhere and if 2 or more threads are running at the same time changing the same variable chaos will ensue.  For example a loop counter "i" used on one thread could be overwritten mid loop by code running on a different thread also using a variablle called "i".

Multi-threading is used for several purposes:

1] To handle asyncronous events that can occur at any time - SB does this (all events are handled on asynchronous non-UI thread), with the limitations that it is best to perform the action initiated by the event in a 'game loop' running on the main UI (User Interface) thread.

2] To speed long calculations that are independenent (the results of one are not needed for another).  Usually this would mean running the same code on multiple threads - but in SB with variables having global scope this is not practical.

3] To perform slow calculation while leaving the main UI thread active.  If a slow calculation is performed on the UI-thread, then the UI appears unresponsive - we have all seen this.  So if a program needs some time consuming initialisation (e.g. downloading lots of resources) then this can actually be done in SB using a Timer which fires events asynchronously and do the downloading in the timer event.

So we can do most things apart from point 2, but we must also remember that the Primitive data type and SB arrays are inherently quite slow, so parallel calculations isn't a magic wand, better to carefully write the SB code more optimally, maybe using existing extension features (e.g. LDList) or write your own extension for an especially performance critical piece of code or move to another language if performance is critical (SB will never be fast!).

Example of LDCall.CallAsync HPMF426.000-0 - uses LitDev.dll and LDBasic.dll extension, note the different variable names in DoWork1, DoWork2, DoWork3 subroutines

If there are use cases I have missed or want more details on above then please reply.
[-] The following 1 user Likes litdev's post:
  • jrmrhrb00
Reply


Messages In This Thread
Threading - by jrmrhrb00 - 01-09-2025, 03:27 PM
RE: Threading - by AbsoluteBeginner - 01-09-2025, 07:12 PM
RE: Threading - by litdev - 01-10-2025, 11:40 AM
RE: Threading - by jrmrhrb00 - 01-10-2025, 06:58 PM
RE: Threading - by z-s - 01-11-2025, 02:33 AM
RE: Threading - by litdev - 01-11-2025, 10:30 AM
RE: Threading - by jrmrhrb00 - 01-11-2025, 06:25 PM
RE: Threading - by litdev - 01-12-2025, 08:15 AM
RE: Threading - by jrmrhrb00 - 01-12-2025, 02:32 PM
RE: Threading - by litdev - 01-12-2025, 10:48 PM
RE: Threading - by jrmrhrb00 - 01-13-2025, 07:51 PM

Forum Jump:


Users browsing this thread: 4 Guest(s)