CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    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++,

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Making a function thread safe

    Quote Originally Posted by aamir121a View Post
    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.

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Making a function thread safe

    Quote Originally Posted by aamir121a View Post
    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

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: Making a function thread safe

    Quote Originally Posted by monarch_dodra View Post
    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/

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Making a function thread safe

    Quote Originally Posted by nuzzle View Post
    Isn't this statement somewhat outdated in C++11?

    There are even books being written on the topic,

    http://www.manning.com/williams/
    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.

  6. #6
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Making a function thread safe

    thank you all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured