Listing the multi-threading unsafe parts of C

J

Juuso Hukkanen

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
 
A

Alan Woodland

Juuso said:
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
 
C

Casper H.S. Dik

Juuso Hukkanen said:
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".
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:

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top