-
November 24th, 2011, 01:08 AM
#1
Making a function thread safe
I'm trying to write a DLL which exports several C like functions, I was wondering how can I make functions exported by this DLL thread safe, for example in language like Java there is a keyword called synchronisation or synchronise which makes the function exported by the class thread safe.
I was wondering how is there something similar to be done in C or C++,
-
November 24th, 2011, 02:24 AM
#2
Re: Making a function thread safe
Originally Posted by aamir121a
I'm trying to write a DLL which exports several C like functions, I was wondering how can I make functions exported by this DLL thread safe, for example in language like Java there is a keyword called synchronisation or synchronise which makes the function exported by the class thread safe.
I was wondering how is there something similar to be done in C or C++,
C++ is not thread aware and does not provide ANY functionality for threading.
A rule of thumb, if you aren't using any explicitly shared data on a shared heap:
*Code WITH static variables are thread UNSAFE
*Code WITHOUT static variables are thread SAFE
A lot of modern C++ libraries are usually thread safe, as they tend to avoid using statics. Unfortunately, a lot of C libraries use loads of statics to "optimize" things like return by value or error handling. These are unsafe.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
November 24th, 2011, 03:34 AM
#3
Re: Making a function thread safe
Originally Posted by aamir121a
I was wondering how is there something similar to be done in C or C++,
I think the recommended way of handling thread-safety in Windows DLL:s is to use Thread Local Storage,
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
-
November 24th, 2011, 03:38 AM
#4
Re: Making a function thread safe
Originally Posted by monarch_dodra
C++ is not thread aware and does not provide ANY functionality for threading.
Isn't this statement somewhat outdated in C++11?
There are even books being written on the topic,
http://www.manning.com/williams/
-
November 24th, 2011, 05:10 AM
#5
Re: Making a function thread safe
Originally Posted by nuzzle
Yes, as of C++11, threading support has been added. However, before that, threads were not part of the core language.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
November 24th, 2011, 05:29 PM
#6
Re: Making a function thread safe
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|