Thread: Thread-Safe Functions

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    14

    Thread-Safe Functions

    Hello,

    I was wondering how to make malloc() thread safe. Could I just use a mutex everytime I use malloc() in my code example

    Code:
    pthread_mutex_lock(&malloc_mutex);
       // Malloc Code here
    pthread_mutex_unlock(&malloc_mutex);
    And if that's the case could I make any function thread safe that way?

    Thanks,
    scioner
    Last edited by scioner; 04-17-2008 at 09:58 AM. Reason: Missing stuff

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure. The only problem is that some implementations already do locking (such as Visual Studio), in which case you'd be locking twice. But otherwise, that's how you would want to do it. Or you may use critical sections which are faster.
    EDIT: On Windows.
    Last edited by Elysia; 04-17-2008 at 10:55 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Or you may use critical sections which are faster
    Which depends on the conflict probability
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    malloc() is thread safe under a POSIX sytem supporting threads.
    http://www.opengroup.org/onlinepubs/...l#tag_02_09_01

    >> could I make any function thread safe that way?
    In the most general sense, yes.

    gg

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by scioner View Post
    Hello,

    I was wondering how to make malloc() thread safe. Could I just use a mutex everytime I use malloc() in my code
    I would definitely not recommend wrapping every call to malloc() in a lock. If you have a thread-unsafe malloc(), you should write a thread safe function (e.g. threadsafe_malloc()) which does the locking/unlocking for you. And of course similar functions for realloc() and free(). Doing it explicitly everywhere would be redundant, and open the possibility of forgetting.

    And if that's the case could I make any function thread safe that way?
    Simply adding locks does not necessarily make a function thread safe. You have to consider deadlock conditions as well. Also, think about what might happen (if anything) if a signal is delivered at any particular point.

    Most platforms capable of threading have a native thread safe malloc(). You should use it, if available. It probably does optimizations which give better performance than just wrapping everything in locks.

    Failing that, you may be able to write a custom allocator which is thread safe without requiring locks. But a global lock around all calls to malloc() is probably the least preferred solution.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    14

    good

    wow I was overwhelmed by the responses thank you very much, very quick and to the point all the answers.


    I wrote a library with 3 functions

    log_write() -> Writes to log file
    log_make_date() -> Generates the current time and date
    log_make_stamp() -> Creates Time Stamp

    Now log_make_date() makes use of gettimeofday() and localtime().

    I am using this small library in a multi-threaded application and I understand that gettimeofday() is not thread-safe. I need to make this function thread-safe or find an thread-safe implementation, any help would be greatly appreciated.

    Thanks in advance
    Last edited by scioner; 04-17-2008 at 11:31 PM.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    14
    Never Mind I was missinformed by a post I now find out that gettimeofday is thread safe because it doesn't required static data and localtime() well there is a thread safe version localtime_r() thanks anyways. Sorry for posting without researching further

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  2. are winsock functions thread safe?
    By *DEAD* in forum Networking/Device Communication
    Replies: 2
    Last Post: 12-15-2007, 10:37 AM
  3. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  4. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM
  5. Replies: 12
    Last Post: 05-17-2003, 05:58 AM