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. 23 1921
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
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.
"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.
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.
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.
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.
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.
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.
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
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.
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
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
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.
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)
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
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.
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!
"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.
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)
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".
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
"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,
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alice |
last post by:
Hello
I have four multithread windows applications(vb.net) interacting and running on the same machine(windows 2000 with .net framework 1.0). All of them start a new thread each time Filewatcher's...
|
by: zbcong |
last post by:
Hello:
I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
|
by: zhebincong |
last post by:
Hello:
I write a multithread c# socket server,it is a winform application,there is
a richtextbox control and button,when the button is click,the server begin
to listen the socket port,waiting...
|
by: fred |
last post by:
I need some help in trying to understand how to make myCollection (inherited
from CollectionBase) multithread safe.
Taking my implementation of the Add Sub and a readonly property Item.
Public...
|
by: jmartin |
last post by:
Hi,
I have made a multithread version of a program (load a file into
database), and with two processors I get the double of time in the
multithread than in the process (unithread) version.
I...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
| |