What does Threadsafe do?

Just starting out? Need help? Post your questions and find answers here.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

What does Threadsafe do?

Post 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?
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: What does Threadsafe do?

Post 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.
Last edited by spikey on Mon Sep 30, 2024 5:32 pm, edited 2 times in total.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: What does Threadsafe do?

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: What does Threadsafe do?

Post 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
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: What does Threadsafe do?

Post by Fred »

It mostly affect the string operations, and a lot of PB libs which requires threaded version (2DDrawing, HTTP etc.)
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: What does Threadsafe do?

Post 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.
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: What does Threadsafe do?

Post by Justin »

Hi, thanks for the responses so i assume locking mechanisms are still needed.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: What does Threadsafe do?

Post by Fred »

List and map are not threaded
Rinzwind
Enthusiast
Enthusiast
Posts: 679
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: What does Threadsafe do?

Post 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.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: What does Threadsafe do?

Post by skywalk »

Yup, I got burned by this early on. The current element was tricky with threads.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply