By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 986 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

Listing the multi-threading unsafe parts of C

P: n/a
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

May 16 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
May 16 '06 #2

P: n/a
Juuso Hukkanen <ju***********@tele3d.net> 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.
May 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.