Listing the multi-threading unsafe parts of C

Discussion in 'C Programming' started by Juuso Hukkanen, May 16, 2006.

  1. I need a list of multithreading unsafe C (C99) functions/features.
    comp.programming.threads provided an initial list of C:ish functions,
    with following ANSI C functions:

    asctime, gmtime, localtime, ctime, tmpnam, strtok

    http://www.lambdacs.com/cpt/FAQ.html#Q150

    However, extra Googling hinted rand() and srand(), also being
    unsuitable for multi-threading - opinions? And what is the status of
    the FILE struct? Is it commonly defined as unsigned char, allowing
    only 255 open file descriptors? - As claimed by:

    http://linuxgazette.net/issue15/mthread.html

    or does C99 say something else or otherwise guarantee the FILE
    struct's (and of all functions utilizing it) suitability for more
    general multithreading.

    <OT>I need this list for a project intending to build another (easiest
    & most powerful) programming language, which two page definition
    defines multi-threadability to be included.
    http://www.tele3d.com/t3d/language.pdf
    </OT>


    Juuso Hukkanen
    (to reply by e-mail set addresses month and year to correct)
    www.tele3d.com
     
    Juuso Hukkanen, May 16, 2006
    #1
    1. Advertising

  2. Juuso Hukkanen wrote:
    > I need a list of multithreading unsafe C (C99) functions/features.
    > comp.programming.threads provided an initial list of C:ish functions,
    > with following ANSI C functions:
    >
    > asctime, gmtime, localtime, ctime, tmpnam, strtok
    >
    > http://www.lambdacs.com/cpt/FAQ.html#Q150
    >
    > However, extra Googling hinted rand() and srand(), also being
    > unsuitable for multi-threading - opinions?

    If you're making a multi-threaded application that uses srand() and
    rand() then it will no longer be deterministic if you call rand() from
    more than one thread. I.e. normally when I run

    int main(void) {
    srand(1);
    printf("%i\n"), rand());
    printf("%i\n"), rand());
    printf("%i\n"), rand());
    return 0;
    }

    I get the exact same output each time, which is useful e.g. validating
    or reproducing results.

    If I've got a multi-threaded application then even though I use the
    exact same seed each time the threads run at the whims of the scheduler.
    If I have 2 or more threads running the following code there is no way
    of being sure that even if the seed doesn't change the results will be
    the same each time.

    int val=0;
    for (unsigned int count=0; count < 100; ++count) {
    val += rand();
    }
    printf("%i\n", val);

    There is a version of rand called rand_r() that will give you
    predictable behvaviour provided you use a different seedp with each
    thread. (And correct locking where threads exchange data).

    The rand(3) manpage includes some discussion on this.

    Alan
     
    Alan Woodland, May 16, 2006
    #2
    1. Advertising

  3. Juuso Hukkanen <> writes:

    >I need a list of multithreading unsafe C (C99) functions/features.
    >comp.programming.threads provided an initial list of C:ish functions,
    >with following ANSI C functions:


    >asctime, gmtime, localtime, ctime, tmpnam, strtok


    You will need to look at the standards defining the pthreads
    and related. The C standard itself says nothing about threads.

    In a number of cases the thread unsafe functions have been made safe
    by using "thread local storage".

    >http://www.lambdacs.com/cpt/FAQ.html#Q150


    >However, extra Googling hinted rand() and srand(), also being
    >unsuitable for multi-threading - opinions? And what is the status of
    >the FILE struct? Is it commonly defined as unsigned char, allowing
    >only 255 open file descriptors? - As claimed by:


    >http://linuxgazette.net/issue15/mthread.html


    There's no such inherit limitation. The members of the FILE structure
    do not need to be visible (the size, however, is; that is a mistake
    in the standard).

    ABIs defined a long time ago often only catered for fds 0-255
    (32 bit SVr4/Solaris) or even just 0-127 (SunOS 4.x and earlier, by
    defining FILE->_file as a "char").

    When 64 bit ABIs were defined for Solaris, two changes were made:
    - the FILE structure was increased in size (128 bytes vs 16 bytes)
    - non of the fields were directly referencable by code.
    (fileno(fp) works, but FILE->_file does not)

    On the "threads" subject, the fixed size of the structure also created
    all kinds of issues with where to put the flockfile() lock, etc.
    So 32 bit stdio userland became fairly convoluted.
    (In since recent weeks, 32 bit Solaris allows applications to use
    32 bit file descriptors in the current development releases)

    >or does C99 say something else or otherwise guarantee the FILE
    >struct's (and of all functions utilizing it) suitability for more
    >general multithreading.


    C99 doesn't mention threading at all.

    ><OT>I need this list for a project intending to build another (easiest
    >& most powerful) programming language, which two page definition
    >defines multi-threadability to be included.
    >http://www.tele3d.com/t3d/language.pdf
    ></OT>


    Start reading manual pages of the C functions; you'll find a list
    of them in the C standard and then start reading the www.opengroup.org
    documents or, e.g., the Solaris manual pages (at docs.sun.com)

    Casper
    --
    Expressed in this posting are my opinions. They are in no way related
    to opinions held by my employer, Sun Microsystems.
    Statements on Sun products included here are not gospel and may
    be fiction rather than truth.
     
    Casper H.S. Dik, May 16, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rockdale
    Replies:
    3
    Views:
    3,301
    rockdale
    Nov 3, 2006
  2. Replies:
    3
    Views:
    617
    Keith Thompson
    Mar 31, 2007
  3. Replies:
    1
    Views:
    966
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Apr 12, 2007
  4. kizk
    Replies:
    0
    Views:
    641
  5. fniles
    Replies:
    4
    Views:
    863
    fniles
    Nov 29, 2005
Loading...

Share This Page