LockMutex() and Threadsafety

Everything else that doesn't fall into one of the other PB categories.
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

LockMutex() and Threadsafety

Post by eriansa »

Perhaps a stupid question, but is LockMutex() threadsafe by itself?
Do I need to compile with ThreadSafety ON?

On my comp (single-core/ threadSafety OFF) my code works as expected, but I am a bit worried if the same applies for multi-cores.
harkon
Enthusiast
Enthusiast
Posts: 217
Joined: Wed Nov 23, 2005 5:48 pm

Post by harkon »

The number of cores should not matter to the application as that is all handled by the OS. If it works on a single core then it should work on a multicore machine.

As a rule though, and someone should correct me if I'm wrong, if you are programming multiple threads, and you must be otherwise LockMutex() would have no point, then you should enable threadsafe as a compiler option.

From the PB help
PureBasic has a special compiler setting to create threadsafe executables. (/THREAD commandline switch or "create threadsafe executable" in the IDE compiler options). Without this mode, certain functions (and also the string access) are faster, but not save to use in threads. It is still possible to create threads without this mode but it is not recommended, as even something simple like a local string access can be dangerous and needs to be protected. Enabling this option makes these things save inside threads, but comes at the price of some speed. The decision on wether or not to use threads should therefore done with care, and the threadmode should only be used when there is a real need for it.
This is the practice I've followed so far anyway.
Missed it by that much!!
HK
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

especially if using a lot of different PB Lib functions inside threads, the option is required for threadsafety in the PB code. As mentioned above, when threadsafe is on the functions are a bit slower so they are left somewhat unsafe by design for performance.

Mutexs etc are for your code and syncing access to your data.

Threadsafe is for PB's code and PB's data.

Atleast that's how I understand it
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Post by eriansa »

pdwyer wrote:Mutexs etc are for your code and syncing access to your data.

Threadsafe is for PB's code and PB's data.
Sure, I understand that.
But the question remains : is LockMutex() threadsafe by itself (without that compiler-option "Threadsafe") OR should I use the Win-Api counterparts which are by nature threadsafe. (SignalObjectAndWait/WaitForSingleObject)

I do not want to use ThreadSafe ON... :wink:
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

The question seems a redundant one to me; PB's mutex objects are simply (in the case of Windows) critical sections used to protect shared resources within a single application. As such this is a Windows construct and not a PB one and Windows, being a multi-tasking OS, is thereadsafe by default! :)

Use CreateMutex() with or without the threadsafe switch. Of course you would likely only use this in a multi-threaded application anyhow and you are then strongly advised to set the threadsafe switch in these circumstances. :)
I may look like a mule, but I'm not a complete ass.
freak
PureBasic Team
PureBasic Team
Posts: 5962
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

> But the question remains : is LockMutex() threadsafe by itself

Yes it is, the whole thread lib is threadsafe by default.
But as others said, you have to know what you are doing when using threads without the threadsafe option ;)
quidquid Latine dictum sit altum videtur
Post Reply