Page 1 of 1

What does Threadsafe do?

Posted: Mon Sep 30, 2024 1:48 pm
by Justin
What kind of safety provides this compiler option?
If for example a thread is iterating a list or accesing an allocated memory area can another thread acces that list or memory?, or are mutex, semaphores still need it?

Re: What does Threadsafe do?

Posted: Mon Sep 30, 2024 2:09 pm
by spikey
The default libraries aren't thread aware and functions may cause crashes or problems in threaded programs. They may be slightly faster than the threaded ones because they don't need to set up and manage synchronisation devices, and don't need to wait etc. If you are writing a single threaded program it makes sense to take advantage of this, so that's why it's an option.

This option tells the compiler to replace the single threaded functions with versions which do use synchronisation devices internally and so won't crash a threaded program or cause a synchronisation fault.

Your code must still provide the necessary synchronisation on its own activities to ensure that it doesn't cause synchronisation problems though. So yes, you will still need mutexes and semaphores where appropriate.

Re: What does Threadsafe do?

Posted: Mon Sep 30, 2024 3:01 pm
by skywalk
It would be good to know if this actually applies to the underlying libs.
For example, does the sqlite3 lib get compiled with different threadsafe settings?
That would speed up database operations if the app was truly single threaded.

Re: What does Threadsafe do?

Posted: Mon Sep 30, 2024 5:37 pm
by spikey
skywalk wrote: Mon Sep 30, 2024 3:01 pm Does the sqlite3 lib get compiled with different threadsafe settings?
Apparently not. I guess I should have qualified my remark and said PB libraries.

Code: Select all

PRAGMA compile_options;
...
THREADSAFE=1

Re: What does Threadsafe do?

Posted: Mon Sep 30, 2024 6:59 pm
by Fred
It mostly affect the string operations, and a lot of PB libs which requires threaded version (2DDrawing, HTTP etc.)

Re: What does Threadsafe do?

Posted: Mon Sep 30, 2024 8:25 pm
by idle
This could get a bit more attention in the documentation.
I still don't know if a list or map is 100% threadsafe and just treat them as if they're not.

Re: What does Threadsafe do?

Posted: Mon Sep 30, 2024 9:53 pm
by Justin
Hi, thanks for the responses so i assume locking mechanisms are still needed.

Re: What does Threadsafe do?

Posted: Mon Sep 30, 2024 10:53 pm
by Fred
List and map are not threaded

Re: What does Threadsafe do?

Posted: Tue Oct 01, 2024 3:38 pm
by Rinzwind
Fred wrote: Mon Sep 30, 2024 10:53 pm List and map are not threaded
But they could, at least for pure concurrent read access.

Re: What does Threadsafe do?

Posted: Tue Oct 01, 2024 4:23 pm
by skywalk
Yup, I got burned by this early on. The current element was tricky with threads.