CHAPTER SYNOPSIS
This chapter describes the ACE wrapper facades that encapsulate OS mul-tithreading mechanisms with portable C++ classes. We apply these ACE threading wrapper facades to illustrate a series of increasingly flexible and
enhancements to our networked logging service.
Overview
Many networked applications lend themselves naturally to multithreading because processing tasks can be separated from I/O tasks. Multithreading is particularly convenient in servers that manage connection-oriented or connectionless network associations for many clients simultaneously. To-day's increasingly powerful OS support for multithreading helps networked applications to:
• Leverage hardware advances, such as symmetric multiprocessing that enables true execution parallelism,
• Increase performance by overlapping computation and communica-tion,
• Improve response time for GUIs and network servers to ensure that time-sensitive tasks are scheduled as needed, and
185
186 9 The ACE Threading Wrapper Facades
• Simplify program structure by enabling applications to use intuitive synchronous programming mechanisms, rather than more complex asynchronous programming mechanisms.
This chapter describes the following ACE classes that networked applica-tions can use to spawn and manage one or more threads of control within a process:
ACE Class
Allows applications to create and manage the lifetime, synchronization, and properties of threads.
ACE A portable encapsulation of OS scheduling class fea-tures that's used in conjunction with the :
() wrapper method to control various properties of real-time threads.
ACE TSS Encapsulates storage mechanisms to allow objects that are "physically" private to a thread to be accessed as though they were "logically" global to a program.
These wrapper facades provide the following benefits:
• Ensure portability across heterogeneous OS platforms. Operating systems have diverse multithreading syntax and semantics, which ACE hides via its wrapper facades. For example, the ACE_Thread_
Manager class ensures threads behave the same regardless of the underlying OS threading mechanisms.
• Manage a group of threads as a cohesive collection. Concurrent networked applications often require multiple threads to start and end as a unit. The class provides a thread group capability that allows other threads to wait for an entire group of threads to exit before proceeding with their processing.
• Influence thread scheduling policies and priorities. Many net-worked applications require tight control over the priorities of their threads. Real-time and general-purpose operating systems often dif-fer in their scheduling capabilities, and multiplatform networked ap-plications need a way to manage the disparate scheduling properties portably. The ACE_Sched_Params class is designed to offer access to run-time scheduling information in a uniform, portable, and easy-to-use manner.
Section The Manager Class
• Efficiently use and manage thread-specific storage (TSS). TSS is a mechanism that enables multiple objects and methods to manage information that's to each thread. ACE's TSS wrapper facade class overcomes syntax differences, as well as capability differences, between platforms.
This chapter motivates and describes the capabilities of the ACE mul-tithreading classes. We present examples of each class to illustrate how they can simplify and optimize various aspects of our networked logging service.
9.2 The ACE Thread Manager Class
Motivation
Different operating systems use different APIs to create, manage, and syn-chronize the completion of threads. Today's multithreading mechanisms suffer from accidental complexities similar to those discussed in previous chapters. They also introduce the following two types of variability that make writing portable applications hard:
• OS threading APIs are often non-portable due to syntactic differences, even if they provide the same capabilities, for example, the Win32 CreateThread and the Pthreads pthread_
create functions provide similar thread creation capabilities, even though their APIs differ syntactically.
• facades that export a syntactically uniform C++
programming interface don't necessarily address semantic variations across OS multithreading mechanisms, for example, both Pthreads and threads support detached threads, whereas Win32 does not, and VxWorks supports only detached
One particularly vexing aspect of multithreaded applications is deter-mining how to cancel threads Some OS platforms provide native support for canceling threads, for example:
• Pthreads defines a powerful set of APIs that allow threads to be can-celed asynchronously or cooperatively via the pthread_cancel and pthread_testcancel () functions.
188 The ACE Threading Wrapper Facades
• also possible to cancel threads on some UNIX platforms via sig-nals, for example, via the Pthreads and threads
functions.
• The Win32 TerminateThread function offers asynchronous lation.
Unfortunately, the native OS thread mechanisms outlined above are nonportable and error For UNIX signals and the Win32 () function can stop a thread dead in its tracks, preventing it from releasing any resources it's holding. The Pthreads asyn-chronous thread cancelation mechanisms provide better support for clean-ing up resources held by a thread, but they are still hard to understand and program correctly, and aren't portable to non-Pthreads platforms.
Since it's tedious and error prone to address all of these portability issues in each application, ACE provides the class.
Class Capabilities
The class uses the Wrapper Facade pattern to guide the encapsulation of the syntactic and semantic variation among different OS multithreading APIs. This class provides the portable capa-bilities:
• Spawns one thread, or multiple threads at once, each running an application-designated function concurrently
• Alters the most common thread attributes, for example, scheduling priority and stack size, for each of the spawned threads
• Spawns and manages a set of threads as a cohesive collection, called * a thread group
• Manages the threads in an ACE_Task, which we present in [SH]
• Facilitates cooperative cancelation of and
• Waits for one or more threads to exit.
The interface of the class is shown in Figure and its key platform-independent methods are outlined in the following table:
Section 9.2 The Class 189
Method Description
spawn
wait ( )
cancel
Creates a new thread of control, passing it the function and function parameter to run as the thread's entry point.
Creates n new threads belonging to the same thread group.
Other threads can wait for this entire group of threads to exit.
Blocks until all threads in the thread manager have exited and reaps the exit status of threads.
Waits for a particular thread to exit and reaps its exit status.
Requests all threads managed by an ACE Thread Manager object to
Asks if the designated thread has been requested to stop.
Exits a thread and releases the thread's resources.
Closes down and releases resources for all managed threads.
A static method that returns a pointer to the ACE Thread Manager singleton.
ACE Thread Manager
grp_id_ :
+ spawn : args : void * = 0,
flags : long = |
id: ACE_thread_t * = 0, handle : priority : long =
: long = ACE_DEFAULT
grp_id : int = -1, task : ACE_Task_Base * = 0, handles : ACE_hthread_t [ ] = 0, stacks : void * [ stack_sizes : size_t [ ] = 0) : int
(timeout : const ACE_Time_Value * = 0) : int (id : status : void ** = 0) : int
= 0,
+ cancel_all (async_cancel : int int
+ testcancel (id : ) : int + exit (status : void * = 0, do_thread_exit + close ( ) : int
Figure The Class Diagram