CriticalSection and Threads

I’d just like to confirm hot I think critical sections work in threads.

Is it for locking an external resource, such as a file, or is it for accessing data within the thread itself?

I have a main thread function which appends the data into a thread for processing.

Do I have to create a critical section around adding the data and reading the data or not? Both seem to work

Without a criticalsection while true if>-1 dim thisdata as string = me.process(thisdata) end if me.sleep(1000) wend

Main Thread Code

With a criticalsection me.crit = new criticalsection while true if>-1 and me.crit.tryenter then dim thisdata as string = me.process(thisdata) end if me.sleep(1000) wend

Main Thread Code




I would say either.

The general rules is that a resource should be protected when multiple threads access it and one of the threads could be manipulating it. If you know that a resource will never be manipulated then there should be no reason to protect it.

Your example using a critical section is what I would probably use (not knowing more about your app). NOTE. You have a missing Leave command in your If condition.

The reason why both are working is probably down to your data access pattern. Your main thread is only appending to the array so the fact that the other thread is removing from the array is safe. Here are a couple of situations that could be unsafe without protection:
a) Both threads remove from the array.
b) The main thread inserts at the head and your other thread removes from the head. In this scenario there could potentially be a context switch between ‘dim thisdata as string =’ and ‘’. This could mean that another element was inserted between those two commands meaning that you remove the wrong index.

This is where Xojo’s co-operative threading wins over pre-emptive threading. If this was a pre-emptive thread you would have to be a lot more careful about protecting resources as both threads could be accessing the resource at the very same time.

Finally, remember that critical sections are blocking actions so you need to use them carefully to prevent your application slowing down due to it constantly waiting for access to the resource.

It was a pseudo code example (I’ve added the leave though). There’s really only 1 thread running, but the actions it performs may take a long time

The documentation does not states if Xojo framework is thread safe: it’s not clearly reported if classes or data structures can be used concurrently between threads.
For example nothing can be assumed on what happens on arrays or dictionaries when multiple threads add/remove elements.
Another point not clearly documented is when a thread switch take place: it’s only reported that a co-operative scheduling is used.
This does not state if this scheduling is done only when executing user code or can happens also in the framework.
On plugins nothing can be assumed.

As a rule of thumb, to be safe anything shared between threads must be protected.