467,135 Members | 1,199 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,135 developers. It's quick & easy.

What is multithread safety

Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.

Thanks.

Sincerely,
Shalini.
Nov 14 '05 #1
  • viewed: 1663
Share:
23 Replies
In article <28**************************@posting.google.com >,
Shalini Joshi <sh*******@yahoo.com> wrote:
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.


This refers to code that is still correct if multiple threads are running
it concurrently.
This is beyond the scope of the C language and therefore not appropriate
for comp.lang.c .

Googling for `thread safety' turns up a few sensible hits (how hard did
you actually try to `find relevant information on the web'?).

comp.programming.threads is probably the best place to ask questions.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Unnecessary casts are the tool of evil (and of C++ programmers, but I
repeat myself).
--Richard Bos in comp.lang.c
Nov 14 '05 #2
On Wed, 27 Oct 2004 07:22:17 -0700, Shalini Joshi wrote:
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.


Fundamentally, it's simply off-topic here, as C doesn't have threading
support. However, the idea is simple enough: ask yourself what would
happen if two different threads accessed or modified the same piece of
data in an unpredictable manner. As an example:

/* file.c */
static char workbuff[32];

void myfunc( char *s )
{
/* do something with workbuff */
strcpy( s, workbuff );
}
If there's only one thread, myfunc will operate correctly. But suppose
there are two threads, and half way through "do something", the second
thread calls myfunc. The second thread "does something" which overwrites
whatever is in workbuff, so when control goes back to the first function,
the buffer is not what it should be. Thus the function is not thread safe.

To make it thread safe, you could do many things. Allocate workbuff on
the fly. Use thread-local storage. Use an automatic buffer. Create a
lock so that two processes can never modify the buffer at the same time.

What you can't do is allow more than one thread to modify the _same_
variable without some form of protection.
Nov 14 '05 #3

"Shalini Joshi" <sh*******@yahoo.com> wrote
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.

To all intents and purposes, thread safety in C means avoiding global and
static variables.
Nov 14 '05 #4
Malcolm wrote:
"Shalini Joshi" <sh*******@yahoo.com> wrote
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.


To all intents and purposes, thread safety in C means avoiding global and
static variables.


That does not guarantee thread safety. A thread safe ISO C program will
not use pointers except in certain ways, will not use dynamically
allocated memory except in certain ways and will not use, as you said,
variables with extent that does not follow scope.

I think that might be enough...

--
Thomas.
Nov 14 '05 #5
On Wed, 27 Oct 2004 21:45:45 +0100, Malcolm wrote:

"Shalini Joshi" <sh*******@yahoo.com> wrote
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.

To all intents and purposes, thread safety in C means avoiding global and
static variables.

You mean there is no program where multiple threads access non global
data ?
For all intents and purposes, your comment is completely nonsense.

Nov 14 '05 #6
On Wed, 27 Oct 2004 23:40:50 +0200, Nils O. Selåsdal wrote:
On Wed, 27 Oct 2004 21:45:45 +0100, Malcolm wrote:

"Shalini Joshi" <sh*******@yahoo.com> wrote
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.

To all intents and purposes, thread safety in C means avoiding global and
static variables.

You mean there is no program where multiple threads access non global
data ?
For all intents and purposes, your comment is completely nonsense.

Well, atleast for things outside standard C which DO support
multi threading.

Nov 14 '05 #7
Shalini Joshi wrote:
Hi, I was trying to find out
what exactly is meant by multi-thread safe code/functions etc.
I couldn't find relevant information on the web
and would appreciate any useful links/information on this topic.


Code is "thread safe"
1.) if multiple threads cannot modify shared program state variables or
2.) shared program state variables are protected by "mutual exclusion".

The term "thread safe" actually refers to the first property.
Codes (C functions/programs) which rely on mutual exclusion,
either explicitly or implicitly,
are more properly described as *threaded* functions/programs.
Nov 14 '05 #8
Nils O. Selåsdal wrote:
Malcolm wrote:
Shalini Joshi wrote:
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.
To all intents and purposes, thread safety in C means
avoiding global and static variables.


You mean
there is no program where multiple threads access non global data?


No.
He means that there are no thread safe *functions*
which access non global data.
Functions which access global data must actually be threaded --
they must use a thread library which guarantees "mutual exclusion"
while they are modifying the global variables
and/or they must use threaded library functions
which use mutual exclusion when modifying global variables.
For all intents and purposes, your comment is completely nonsense.

Nov 14 '05 #9
E. Robert Tisdale wrote:
Nils O. Selåsdal wrote:

Malcolm wrote:

Shalini Joshi wrote:
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.

To all intents and purposes, thread safety in C means
avoiding global and static variables.


You mean
there is no program where multiple threads access non global data?


No.
He means that there are no thread safe *functions*
which access non global data.


I doubt that's what he means. For example, it
would imply that a thread-safe function could not
access its own arguments or its own `auto' and
`register' variables.

--
Er*********@sun.com
Nov 14 '05 #10
Thanks a lot Kelsey, Dave for explaining it. I actually came across
the term while reading up on how to write secure functions in C - and
wondered what it meant. I am sorry i should have searched harder on
the web ( was actually searching for "multithread safe functions" so
now I know why i was getting Java-related stuff mostly).

Regards,

Shalini.
Nov 14 '05 #11
On Wed, 27 Oct 2004 21:53:24 UTC, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote:
Nils O. Selåsdal wrote:
Malcolm wrote:
Shalini Joshi wrote:

Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.

To all intents and purposes, thread safety in C means
avoiding global and static variables.


You mean
there is no program where multiple threads access non global data?


No.
He means that there are no thread safe *functions*
which access non global data.
Functions which access global data must actually be threaded --
they must use a thread library which guarantees "mutual exclusion"
while they are modifying the global variables
and/or they must use threaded library functions
which use mutual exclusion when modifying global variables.
For all intents and purposes, your comment is completely nonsense.

I'm writing multithreaded applications and libraries since 1988. No,
not the flaky linux threading. Real, kernel based threading. I have
written thousends of functions handling global data, handling static
data, sending pointers around from thread to thread, receiving
pointers in multiwindow, multiproces, multithreaded applications
flawless.

Having multithreading that handles threads better than linux handles
processes makes it much easier to control the flow as you would know
that any of your thread can be interrupted by another thread even
while it is insinde a single C instruction.

Having an OS that gives you many varied APIs to get that under control
makes it really easy. Have the chance to block thread change for
limited time in a single process, variant types of semaphores and
other technics to control the program flow makes it easy to spread new
threads, block access to variables, block access to code, to devices,
to shared memory and so on makes interthread and interprocess
communication easy.

Letting the OS handle not only process but thread scheduling gives you
more control about threads as pthreads ever can reach. Sure, you needs
a thread aware standard C library - but any C compiler can deliver it.
No need to waste one single second on that other than to define the
right linker flags for that. Select the right blocking mechanism and
you can acces global and static variables and even use nonreentrant
functions in your multithreaded environment.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation

Nov 14 '05 #12
On Wed, 27 Oct 2004 21:31:38 UTC, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote:
Shalini Joshi wrote:
Hi, I was trying to find out
what exactly is meant by multi-thread safe code/functions etc.
I couldn't find relevant information on the web
and would appreciate any useful links/information on this topic.
Code is "thread safe"
1.) if multiple threads cannot modify shared program state variables or


Wrong on OSes that are capapable of preemtive multithreading on single
and multi CPU computers.
2.) shared program state variables are protected by "mutual exclusion".
Wrong too.
The term "thread safe" actually refers to the first property.
Codes (C functions/programs) which rely on mutual exclusion,
either explicitly or implicitly,
are more properly described as *threaded* functions/programs.


Not true.

Having an OS that does handle multithreading on kernel level, having
the right methods of controlling threads - and semaphores are an
relative seldom needed way on that. Half hearted pthreads are only a
dumb compromise between multiprocessing, and real preemtive
multithreading.

I've written multithreaded applications and libraries without any use
of semaphores but handling lots of global and shared data using other,
quite simpler methods to coordinate access to them.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation

Nov 14 '05 #13
Eric Sosman wrote:
E. Robert Tisdale wrote:
Nils O. Selåsdal wrote:
Malcolm wrote:

Shalini Joshi wrote:

>Hi, I was trying to find out what exactly is meant by multi-thread
>safe code/functions etc. I couldn't find relevant information on the
>web and would appreciate any useful links/information on this topic.

To all intents and purposes, thread safety in C means
avoiding global and static variables.

You mean
there is no program where multiple threads access non global data?
No.
He means that there are no thread safe *functions*
which access non global data.


Should be:

which access global data.

I doubt that's what he means. For example, it
would imply that a thread-safe function could not
access its own arguments or its own `auto' and
`register' variables.

Nov 14 '05 #14
In article <cl**********@news6.svr.pol.co.uk>, ma*****@55bank.freeserve.co.uk
says...

"Shalini Joshi" <sh*******@yahoo.com> wrote
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.

To all intents and purposes, thread safety in C means avoiding global and
static variables.


Not hardly. Hence not answering OT questions being a good idea.

--
Randy Howard (2reply remove FOOBAR)

Nov 14 '05 #15
In <cl**********@news6.svr.pol.co.uk> "Malcolm" <ma*****@55bank.freeserve.co.uk> writes:

"Shalini Joshi" <sh*******@yahoo.com> wrote
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.

To all intents and purposes, thread safety in C means avoiding global and
static variables.


It also means avoiding *any* library function:

4 The functions in the standard library are not guaranteed to be
reentrant and may modify objects with static storage duration.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #16
Randy Howard wrote:
malcolm wrote:
Shalini Joshi wrote:
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.
To all intents and purposes,
thread safety in C means avoiding global and static variables.


Not hardly.


Wrong!
Hence not answering OT questions being a good idea.


Correct.
Nov 14 '05 #17
Dan Pop wrote:
"Malcolm" <ma*****@55bank.freeserve.co.uk> writes:
"Shalini Joshi" <sh*******@yahoo.com> wrote

Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.

To all intents and purposes, thread safety in C means avoiding global
and static variables.


It also means avoiding *any* library function:

4 The functions in the standard library are not guaranteed to be
reentrant and may modify objects with static storage duration.


Qualify that. function of which you do not have the full source
(right down to the hardware) and the capability of evaluating.

Of course higher quality libraries and systems guard their critical
sections. This allows you to treat things that call them as if
they were re-entrant.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #18

"Dan Pop" <Da*****@cern.ch> wrote
To all intents and purposes, thread safety in C means avoiding global and ^^^^^^^^^^^^^^^^^^^^^static variables.


It also means avoiding *any* library function:

4 The functions in the standard library are not guaranteed to be
reentrant and may modify objects with static storage duration.

There are other issues. The basic problem is that two threads might access
the same variable at the same time, for instance a global integer may be
half written and then accessed by the second thread, causing a garbage read.
Or the algorithm could rely on the value remaining constant for the duration
of the function, when in fact it changes.
However you can also have contention for hardware resources, and I can
easily believe that standard library functions are not guaranteed to be
reentrant (some, like strtok(), are difficult to write in a thread-safe
manner). So I am not giving a complete guide to implementing thread-safe C,
just explaining the basic issue.
Nov 14 '05 #19
In article <cl**********@nntp1.jpl.nasa.gov>, E.**************@jpl.nasa.gov
says...
Randy Howard wrote:
malcolm wrote:
Shalini Joshi wrote:

Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.

To all intents and purposes,
thread safety in C means avoiding global and static variables.


Not hardly.


Wrong!


ERT, If you think that's all there is to doing it properly, you've got
even more to learn about that than you do about C.

--
Randy Howard (2reply remove FOOBAR)

Nov 14 '05 #20
Randy Howard wrote:
ERT, If you think that's all there is to doing it properly,
you've got even more to learn about that than you do about C.


You have confused "thread safe code" with "[multi]threaded code".
Nov 14 '05 #21
On 28 Oct 2004 17:03:49 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <cl**********@news6.svr.pol.co.uk> "Malcolm" <ma*****@55bank.freeserve.co.uk> writes:

"Shalini Joshi" <sh*******@yahoo.com> wrote
Hi, I was trying to find out what exactly is meant by multi-thread
safe code/functions etc. I couldn't find relevant information on the
web and would appreciate any useful links/information on this topic.

To all intents and purposes, thread safety in C means avoiding global and
static variables.


It also means avoiding *any* library function:

4 The functions in the standard library are not guaranteed to be
reentrant and may modify objects with static storage duration.

Not guaranteed by the standard (which of course doesn't guarantee
anything about multi-threaded code), but may be guaranteed by the
implementation.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #22
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<cl**********@nntp1.jpl.nasa.gov>...
Randy Howard wrote:
ERT, If you think that's all there is to doing it properly,
you've got even more to learn about that than you do about C.


You have confused "thread safe code" with "[multi]threaded code".


Now I'm curious. What practical difference is there
between my single-threaded (but thread-safe) library
function and the multithreaded (and thread-safe)
calling code (other than the obvious "one calls the
other") with regard specifically to "thread-safety".
goose,
Nov 14 '05 #23

"goose" <ru**@webmail.co.za> wrote

Now I'm curious. What practical difference is there
between my single-threaded (but thread-safe) library
function and the multithreaded (and thread-safe)
calling code (other than the obvious "one calls the
other") with regard specifically to "thread-safety".

It is extremely easy to write a thread-safe function in C. Simply avoid
globals, static variables, pointers which may have been passed to other
threads and, of course, calling non-thread-safe suboutines.

eg

int threadsafe(int x, int y)
{
return x + y;
}

int notthreadsafe(void)
{
static int count = 0;
return count++;
}

The problem is that it seldom any use to have two threads in a program that
do not communicate with each other at all. Somehow information has to get
from thread1 to thread2, and this opens up a whole set of complications,
off-topic for this ng.
Nov 14 '05 #24

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Alice | last post: by
2 posts views Thread by zhebincong | last post: by
6 posts views Thread by jmartin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.