473,564 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Listing the multi-threading unsafe parts of C

I need a list of multithreading unsafe C (C99) functions/features.
comp.programmin g.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
2 3252
Juuso Hukkanen wrote:
I need a list of multithreading unsafe C (C99) functions/features.
comp.programmin g.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
Juuso Hukkanen <ju***********@ tele3d.net> writes:
I need a list of multithreading unsafe C (C99) functions/features.
comp.programmi ng.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1443
by: Oli | last post by:
Hi I have 2 textboxes - "from" & "to". Assume I enter number 1 in "from" and 10 in "to" - I want the ASP to return: 1,2,3,4,5,6,7,8,9,10 and insert this into an Access database over 10 rows into the 'number' field. Anyone got any ideas how to do this?
9
1995
by: Håkon Helgesen | last post by:
Hello! I am using php to get a result from a MySql Query, but the listing is too long to have in one column. Can i use CSS to get my result in several column? How? PS. This is the very first time I'm using css so be gentle (C: cheers
10
2453
by: Chris | last post by:
Hi, Not sure if this is the right forum, but hopefully someone can help me. I am creating something for our intranet and i want to list the files and folders of a directory, i found some code to do this. The only problem is that it lists the asp file used to for example if i go to: "http://myserver/listing.asp" In the file listing will...
1
6277
by: httpmart | last post by:
On my website I routinely make use of anchors such as: <a name="shipping">SHIPPING</a> and a link to them such as: <a href="#shipping">Go to Shipping information</a> These work fine on IE.
19
3298
by: SU News Server | last post by:
I've struggled with this for quite a while and I'm am just not sure what is going on. I have the following code import os def buildList( directory='/Users/mkonrad' ) dirs = listing = os.listdir(directory)
3
2035
by: David Jacques | last post by:
I am trying to get a list of all files of a certain extension type on disk to do some processing in a loop. The code needs to be portable to UNIX, so I need to use plain c functionality. Does anyone know of a way to do this ? Any URLs, code snippets, etc ?
2
3366
by: Tom | last post by:
I need to get a directory listing through http. If I put the directory path in the browser address bar such as http://somewebpage.com/subdir I get the listing of the directory. Of course this is returned to my client in html. To get the directory listing from this I would need to do some intersting parsing. ftp is not enabled and so I can't do...
1
1491
by: Johnny Jörgensen | last post by:
I've got a serious problem. I've got Visual Studio 2005 installed, and of course I'm using the Pretty Listing formatting function. When I start up VS, everything is fine, but after a while (which can be one minute or one hour) the Pretty Listing starts acting weird. For instance, if I start writing "for...", it changes it to f(o) r" or...
5
4397
by: eyden | last post by:
Phew, I have a problem that is really bugging me. We have a system of naming our partnames which we want to add into a database. Our partnames are composed of 1-7 numbers or letters where each number has a meaning. You can think of it as a multi-dimensional array and I've started to build a database with four tables ...
5
3479
by: jain236 | last post by:
HI every body, i am always getting the following error while parsing a directory . i am reading a directory by doing ls and trying to find out the name,type,size, mtime and mode of files from the directory my @valeu =@{$Files}; foreach my $file (parse_dir(\@valeu)) { @Files = $ftp->ls('-lR');
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.