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
|
|
0
|
|
|
|
Reply
|
juuso_12_2003 (33)
|
5/16/2006 1:32:55 PM |
|
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
|
|
0
|
|
|
|
Reply
|
ajw05 (92)
|
5/16/2006 3:36:46 PM
|
|
Juuso Hukkanen <juuso_12_2003@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.
|
|
0
|
|
|
|
Reply
|
Casper.Dik (623)
|
5/16/2006 4:29:47 PM
|
|
|
2 Replies
37 Views
(page loaded in 0.055 seconds)
Similiar Articles: Loki's Factory Methods and Multithreading - comp.lang.c++ ...... the GUARD_RETURN stuff, instead of using the thread-unsafe ... > That code encapsulates both multi-threaded and single ... lock over the whole operation, even if only part ... setting timer - comp.unix.programmer... Thanks Joe and Loic. > > Did I understand right that 'printf' is unsafe function ... That's the same with multi-threading. close() is thread-safe, but if you close an fd that ... Part Numbering Systems for CAD management. - comp.cad.solidworks ...Here is the current list of part ... kelln...@cbd.net> wrote: > Part numbering schemes can be like a religion. Dangerous ... Consider the list in this thread with three ... Application hangs - comp.sys.hp.hpuxThought our application is single threaded, we have critical sections since, this code is a part of a shared ... Do you know if this process ever became multi-threaded before ... Simple code encryption (xor) problem - comp.lang.asm.x86 ...The second DB would *not* be part of Payload. >Don't worry if no one knows. ... This sort of code is also frequently multi-threaded, running on as many CPUs as you have ... Sockets in gfortran? - comp.lang.fortranThe tough part is getting started with such a programming task, but with these ... wanted a way to communicate between my GUI written in C++ (using Qt, and multi-threaded ... Practice of using fork() - comp.unix.programmer:) If you need a high-performance multi-threaded malloc, hoard is out there. ... is not part of the program in the sense of the executable itself, it is part of ... Calculation and verification of IBAN checksums - comp.lang.awk ...So I repost in this thread two postings from a couple ... # # Consider the IBAN generation part of this module for ... # * Requires Unix tool bc(1) for multi-precision arithmetic. Misuses of RTTI - comp.lang.c++.moderated... tedious, manual proofing and is done for scientific purposes only or as part of ... You would think that, for example, supporting multi-threading on a modern multi-core ... NASM - Official Recursive Macro Support! - comp.lang.asm.x86 ...... revamp of the NASM preprocessor, now available as part ... if. * %comment/%endcomment: Comment block for long/multi ... questions/comments/concerns, please reply to this thread. Listing the multi-threading unsafe parts of C - C Language - C+ ...C/C++ :: C Language :: Listing the multi-threading unsafe parts of C Threading in C# - Part 2 - Basic SynchronizationPart 2: Basic Synchronization. Synchronization Essentials. So far ... other words, we would have to lock exactly as with our thread-unsafe collection classes (making the List ... 7/6/2012 4:09:32 PM
|