Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
From: rmartin@thor.Rational.COM (Bob Martin)
Subject: Re: Thread-safe libraries for C++?
Date: Tue, 16 Feb 1993 22:29:20 GMT	[thread overview]
Message-ID: <rmartin.729901760@thor> (raw)
In-Reply-To: 1993Feb12.184345.19220@walter.bellcore.com

doel@bbq.uucp (Mike Doel) writes:


|In order to use threads, you must use
|"thread safe" libraries.  Apparently, the standard C libraries aren't thread
|safe.  To counteract this, "jacket" routines have been developed which make
|the standard Unix libraries thread safe (what does that mean?).

First, the definition of "thread safe".  In an operating system that
supports multiprocessing, a thread is most often a process.  It is an
execution context managed by the operating system.  The operating
system can suspend the execution of one thread and start or continue
the execution of another at any arbitrary time.  Thus, if your code is
running in one thread, it may get arbitrarily interrupted while the
code from someone elses program runs.  

This has severe implications for the management of any resources that
the two threads share in common.  For example, if both threads
attempted to output messages to cout, the characters within the
messages could be interleaved as the operating system alternately
suspended and continued the two threads.

A thread safe library is one in which the operations are guaranteed to
remain sane and safe in the presence of multiple threads of execution.

|My question is, has anything been done to make C++ libraries safe?  

Some C++ class libraries are thread safe.  The Booch Components are
one example.  I am not directly familiar with any thread-safe versions
of the standard C or C++ libraries.

|If not,
|can we still develop C++ applications in a DCE threaded environment?  I
|talked to someone at Transarc (who has a DCE implementation for SunOS 4.1.2)
|and he suggested we do something like this:

|	//obtain the "mutex", however that's done

|	cout << "Now it's safe to do output"

|	//release the mutex

A mutex is a semaphore.  It is a flag which says "I have it, you cant
use it!".  You sieze the mutex before you mess with a shared resource.
You release it when you are done.  If you can't sieze the mutex, then
someone else must have it, so you wait until they release it.   Most
operating systems provide mutex tools in one form or another.

I assume that your correspondent was not talking about any particular
mutex, but one that you created for yourself, and that all your
threads used.

|This strikes me as terribly kludgy.  Obtaining and release the mutex should
|be done by the stream class, not the user of it.

That would be nice.  Perhaps you should derive a mutex using subclass
of ios....  

--
Robert Martin       | Design Consulting    | Training courses offered:
R. C. M. Consulting | rmartin@rational.com |  Object Oriented Analysis
2080 Cranbrook Rd.  | Tel: (708) 918-1004  |  Object Oriented Design
Green Oaks, Il 60048| Fax: (708) 918-1023  |  C++


  parent reply	other threads:[~1993-02-16 22:29 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-02-12 18:43 Thread-safe libraries for C++? Mike Doel
1993-02-12 12:22 ` Jon C. R. Bennett
1993-02-13 18:17   ` Pete Becker
1993-02-14 22:26     ` Andrew Walduck
1993-02-12 20:40 ` Michael J. Grier
1993-02-16 17:54   ` Jim Robinson
1993-02-16 23:14     ` Michael J. Grier
1993-02-18  2:51     ` Michael Joosten
1993-02-18 22:05     ` Greg Brail
1993-02-16 22:29 ` Bob Martin [this message]
1993-02-17 18:15   ` John Nagle
1993-02-18 14:37     ` Andrew Condon
1993-02-22 20:22       ` Greg Brail
1993-02-16 22:30 ` Greg Brail
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox