50 likes | 201 Views
E81 CSE 532S: Advanced Multi-Paradigm Software Development. The Active Object Pattern. Chris Gill Department of Computer Science and Engineering Washington University, St. Louis cdgill@cse.wustl.edu. Active Object Pattern. Active Object. Problem
E N D
E81 CSE 532S: Advanced Multi-Paradigm Software Development The Active Object Pattern Chris Gill Department of Computer Science and Engineering Washington University, St. Louis cdgill@cse.wustl.edu
Active Object Pattern Active Object • Problem • Concurrent access risks races or deadlock, blocking, etc. • Solution: “activate” the object • I.e., launch thread “inside” it • Decouples object method execution from invocation • Helps prevent deadlock • Helps increase concurrency • Implementation concerns • Ensuring thread safety • Increasing concurrency • Code and concurrency interact queue requesting thread worker thread
Details: Retrieving Results Active Object Active Object • Completion of work is asynchronous • Blocking the request thread becomes a variant of the Monitor Object pattern • Can combine Active Object and ACT patterns • Encapsulate general completion rendezvous points as “Futures” • Can poll for result, or just block until it’s ready • Or, can post on requesting thread’s queue if it’s an active object as well request queue requesting thread worker thread result queue
Details: Managing Queue Access Active Object • Need to synchronize access to queue methods • Particularly mutator methods • Want classic supplier-consumer behavior • Supplier sleeps when queue is full • Consumer sleeps when queue is empty • Need to manage carefully • Use condition variables • Increase concurrency opportunity • Maintain thread safety • Use high/low water marks • Avoid “silly window syndrome” queue requesting thread worker thread
Design and Implementation Concerns • Key trade-off for lock-based data structures • Ensuring thread safety (no meaningful race conditions) • Increasing concurrency opportunities (deadlock == none) • Scope of serialization matters • How long is a lock held, and over how much data? • Minimize protected regions, use different locks if possible • I.e., “right mutex is locked … held for … minimum … time” • Data structure and concurrency semantics interact • Queue interface motivates use of condition variables • Note that notify_all() may be needed for exception safety! • Move expensive copying/allocation outside locked regions • Associating a lock per item also reduces lock granularity • Decoupling items is then essential (hash vs. tree vs. array example) • Using a readers/writer lock also may be helpful in some cases