From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,4753a27d8ceddc44 X-Google-Attributes: gid109fba,public X-Google-Thread: f678f,4753a27d8ceddc44 X-Google-Attributes: gidf678f,public X-Google-ArrivalTime: 1993-02-16 14:29:20 PST Xref: sparky comp.lang.c++:21129 comp.unix.osf.misc:110 Newsgroups: comp.lang.c++,comp.unix.osf.misc Path: sparky!uunet!rational.com!thor!rmartin From: rmartin@thor.Rational.COM (Bob Martin) Subject: Re: Thread-safe libraries for C++? Message-ID: Sender: news@rational.com Organization: Rational References: <1993Feb12.184345.19220@walter.bellcore.com> Date: Tue, 16 Feb 1993 22:29:20 GMT 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++