popen thread safe?

This is a discussion on popen thread safe? within the Linux Programming forums, part of the Platform Specific Boards category; Is popen safe to use with pthreads? I've tried to google it, but I found no straight answer. And in ...

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    popen thread safe?

    Is popen safe to use with pthreads?

    I've tried to google it, but I found no straight answer.
    And in forums, some people claim it's not because it forks, while others say it's considered threadsafe(without giving a link to a trustable site), and other argues its safe because it forks and then immediately execs.

    So what I hope is, does anyone here know for sure if it's safe to use popen with pthreads?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Drogin View Post
    Is popen safe to use with pthreads?

    I've tried to google it, but I found no straight answer.
    And in forums, some people claim it's not because it forks, while others say it's considered threadsafe(without giving a link to a trustable site), and other argues its safe because it forks and then immediately execs.

    So what I hope is, does anyone here know for sure if it's safe to use popen with pthreads?
    You're right, there is no clear answer out there. Amazing.

    However, I really doubt there's anything inherently problematic about using popen() in a program that has multiple threads. It may not be properly re-entrant, in which case you should serialize calls to popen()/pclose() by protecting it with a mutex.

    Yet another line item in my growing list of why the POSIX API really isn't as great as I used to think it was.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,608
    You probably won't have any issues on Linux, but this definitely doesn't hold true to all *nixes, and so isn't portable at all.
    I've run pthreads from forks in Linux with no ill effects, but the same code completely broke on OBSD.

    I agree, POSIX has a couple retarded shortcomings. But what OS' API doesn't?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    It's not in the list of "async-signal-safe" functions (though execl and fork are):
    http://pubs.opengroup.org/onlinepubs...ag_15_04_03_03
    "Async-signal-safe" implies "thread-safe".

    It's not in the list of "functions that synchronize memory" (though fork is):
    http://pubs.opengroup.org/onlinepubs...html#tag_04_11
    Functions that synchronize imply "thread-safe".

    But here is what we really need:
    http://pubs.opengroup.org/onlinepubs...l#tag_15_09_01
    All functions defined by this volume of POSIX.1-2008 shall be thread-safe, except that the following functions need not be thread-safe.
    popen is not in that list.

    So you can call any Posix function in an application that uses threads. You just have to be aware that the functions in that list may not be "thread-safe" - like strtok. But if only one thread is using those functions, then there's nothing to worry about.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. thread safe in Dispose method
    By George2 in forum C# Programming
    Replies: 3
    Last Post: 05-15-2008, 07:32 AM
  2. thread safe in my code to manipulate List?
    By George2 in forum C# Programming
    Replies: 8
    Last Post: 05-01-2008, 06:57 AM
  3. are winsock functions thread safe?
    By *DEAD* in forum Networking/Device Communication
    Replies: 2
    Last Post: 12-15-2007, 09:37 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21