CSC/ECE 517 Fall 2009/wiki2 14 conc patterns: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 25: Line 25:
   static Singleton *instance_;
   static Singleton *instance_;
  };
  };
// ...
// ...
Singleton::instance ()->method ();
Singleton::instance ()->method ();
// ...
// ...

Revision as of 05:28, 9 October 2009

Thread-Safe Programming and Concurrency Patterns

Introduction

Within a particular realm, design patterns represent solutions to the problems that are encountered during the development phase of software development life cycle(SDLC). These patterns help in reusing of successful software architectures and designs. There are various different patterns as shown in adjacent figure.

Classification of Design Patterns

1. Double-checked locking

The Double-checked locking design pattern is used when an application has one or more critical sections of code that must execute sequentially; when multiple threads can potentially attempt to execute the critical section simultaneously; when the critical section is executed just once and when acquiring a lock on every access to the critical section causes excessive overhead.

Many familiar design patterns such as Singleton that work well for sequential programs contain subtle assumptions that do not apply in the context of concurrency. Consider the following implementation of the Singleton pattern:

class Singleton
{
public:
 static Singleton *instance (void)
 {
  if (instance_ == 0)
   // Critical section.
   instance_ = new Singleton;
  return instance_;
 }
 void method (void);
 // Other methods and members omitted.
private:
 static Singleton *instance_;
};
// ...
Singleton::instance ()->method ();
// ...