Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

TBB mutex not safe in Windows shared DLL?

I have heard that the mutex object simply uses Critical Sections in Win32. This is very fast for a process, but isn't safe in a DLL that is used by multiple processes.

Is this true?
0 Kudos
1 Reply
Valued Contributor II

Yes, TBB::mutex object uses Enter|LeaveCriticalSection in the Windows implementation and pthread_mutex_lock|unlock otherwise. Each of these are meant to operate within a process, avoiding the overhead incurred for interprocess communication.

So what about shared DLLs? While the "text" of a shared DLL (the instructions and constants parts) are shared among processes, the "data" part is not. Each belongs to the respective process. In fact, you have to go to some extremesto share memory between multiple instantiations of a DLL. It's possible and there certainly are DLLs out there that use shared memory to moderate communications between multiple processes, but this is not the norm.

So, if you use tbb::mutex in a DLL that only uses parent process data, you should not have to worry about thread safety issues using tbb::mutex. But if you're DLL is moderating between multiple processes, the data structures involved will need stronger (slower) protections.

0 Kudos