Multithreading Through a Wrapper DLL

mooseware_ca
mooseware_ca used Ask the Experts™
on
I've got a question about thread safe programming.  I'm a business application developer and don't really understand all of the implications of thread-safe programming.  I've got a customer who is using a third-party DLL which is thread-safe/multithreaded and stateful.  You can open many independent sessions in this DLL from many different calling programs and each session gets an integer handle that you use as part of subsequent calls to the API.  The DLL keeps all of these concurrent sessions on multiple threads separate and everything works great.

So here is my problem/issue/question.  My customer wants to have a wrapper written around certain parts of the API of this third-party DLL.  The point of the wrapper is just to reconfigure the function arguments.  The DLL uses standard C calling conventions and relies heavily on C structs.  The DLL is not COM or anything, just straight-forward static C functions.  My customer wants to access the API using a language which can't handle structs properly.  Therefore, they want a wrapper DLL that rejigs the structs as independent function arguments using basic data types.  This all seems pretty simple, just create a new DLL that wraps the third-party DLL with some static functions that do the translations to and from structs and basic types.  

What I don't know is this:  Will there be any threading complications if I build this simple DLL with straight forward static wrapper functions?  Do I need to do anything to avoid problems in a multi-threaded application?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
jkr
Top Expert 2012

Commented:
>>Will there be any threading complications if I build this simple DLL with
>>straight forward static wrapper functions?

As long as you don't use glogal storage or similar mechnisms where multiple threads could interfere with each other and create race conditions, you are on the safe side. Take a look at http://en.wikipedia.org/wiki/Thread_safety#Identification
trinitrotolueneDirector - Software Engineering

Commented:
you should be serializing access to these static functions whenever you call them.

Something like

--enter_critical_section
  call static_fn1
--leave critical section

check for performance issues
jkr
Top Expert 2012

Commented:
>>you should be serializing access to these static functions whenever you call
>>them.

That's the point - this is not necessary if you don't use global or shared data as well as the multithreaded version of the runtime libraries.

Author

Commented:
From what I understand, the third-party DLL definitely has data shared across multiple threads and even processes, I think.  Nevertheless, all of the ugliness involved in that is handled within the third-party DLL.  If the wrapper DLL just takes the function arguments, reformats them to/from structs, passes them on to the third-party DLL and then returns that DLL's result to the caller, there is no memory to worry about other than local variables whose memory is released when the wrapper function exits.  This seems pretty straight-forward to me but I've got people whispering in my other ear that it's going to be ugly/hard.  It really is this simple, isn't it?
Top Expert 2012
Commented:
>>the third-party DLL definitely has data shared across multiple threads and
>>even processes, I think.

Then it also has means to ensure that these don't hamper each other, and since you are only wrapping the calls, you won't interfere with these mechanisms.

>> It really is this simple, isn't it?

If it's only local variables you deal with - yes it is. Local variables are allocated on the stack, and since each thread has its own stack, this is guaranteed to be safe - thread-safe, literally (pardon the pun ;o)

Author

Commented:
Thanks!  I really appreciate the help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial