CriticalSection or a simple boolean property of the App?

Hi all!

In a webApp 1, I use a timer that will launch a thread every minute (on the server, not on webpage). This thread will launch a succession of other threads and CURL objects to connect to the external services API.

I need the timer not to restart the action until the threads of the previous action have finished. Do I have to use a CriticalSection? (e.g. as a property of the App object) Or can the threads write/read a simple boolean property of the App? In order to test if the thread succession has finished its work.

At each launch of the timer action:

  • If the App.inProgress property of the App is FALSE, then the process is launched:
  • The App.inProgress property of the App is set to TRUE.
  • A thread is launched. A job is done with the API. Once it is done: if the thread detects that there is no need to launch other threads, then App.inProgress=FALSE. Otherwise, another thread is launched. A job is done with the API. Once it is done: if the thread detects that there is no need to launch other threads, then App.inProgress=FALSE. Otherwise, another thread is launched, etc.

It really depends on what you need. If you use the boolean, executions of the thread will be skipped. For example, let’s say you have 3 executions of the timer. On the first, nothing is happening, so it’s time to start the thread. On the second, the thread is still running so the boolean is true, and no thread is started. On the third, the thread has already completed, the boolean is false, and a new thread is started.

If you use a critical section, you’ll always get a new thread and they will wait for each other. This could have a cascading effect if the timer consistently fires before the thread finishes.

I’d imagine you want to first scenario.

1 Like

Thank you very much Thom! So accessing an boolean app property via multiple threads is not a problem? The criticalSection is only required if it is to access an object or an external resource?

No problem accessing the boolean. Even with multiple threads, Xojo still runs only one line of code at a time. The need for critical sections is rare.

1 Like