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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 77: Line 77:


====Read/Write Lock Pattern====
====Read/Write Lock Pattern====
This pattern allows concurrent read access to an object but requires exclusive access for write operations [11]. This means that when an object is being written to, the object access will be blocked for all other read operations.




Line 90: Line 91:
[9] http://www.cs.uu.nl/docs/vakken/no/active_object_pattern.pdf <br />
[9] http://www.cs.uu.nl/docs/vakken/no/active_object_pattern.pdf <br />
[10] http://en.wikipedia.org/wiki/Balking_pattern <br />
[10] http://en.wikipedia.org/wiki/Balking_pattern <br />
[11] http://www.mindspring.com/~mgrand/pattern_synopses.htm#Balking
[11] http://www.mindspring.com/~mgrand/pattern_synopses.htm

Revision as of 07:07, 9 October 2009

Thread-Safe Programming and Concurrency Patterns

Introduction to Thread-Safe Programming

In computer programming, thread-safe describes a program portion or routine that can be called from multiple programming threads without unwanted interaction between the threads [1]. A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads and in particular, it must satisfy the need for multiple threads to access the same shared data, and the need for a shared piece of data to be accessed by only one thread at any given time [2].

Here's an illustration of a thread unsafe scenario. Suppose that your application creates several threads, each of which makes a call to the same library routine such that this library routine accesses/modifies a global structure or location in memory then as each thread calls this routine it is possible that they may try to modify this global structure/memory location at the same time [4].

Typical thread-unsafe scenario [4]


Two important questions arise:

  • What are the consequences of running a thread-unsafe program? Thread programming errors may lead to unpredictable results like data errors possibly followed by fatal application exceptions or endless blocked threads (race-conditions and deadlocks) and these errors are especially difficult to find but easy to introduce by inexperienced programmers [3].
  • How do we ensure that a particular multi-threaded code is thread safe? Some ways to unsure thread safety are that concurrent threads use synchronized algorithms that cooperate with each other and to confine the address of a shared object to one thread whenever an unasynchronized algorithm is active [1].

Here's an example of some thread safe code. In C language, local variables are dynamically allocated on the stack. Therefore, any function that does not use static data or other shared resources is trivially thread-safe, as in the following example [5]:

/* thread-safe function */
int diff(int x, int y)
{
       int delta;
       delta = y - x;
       if (delta < 0)
               delta = -delta;
       return delta;
}


Concurrency Patterns

Definitions

  • Design Patterns
    In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design and not a finished design that can be transformed directly into code [6]. It is a description or template for how to solve a problem that can be used in many different situations and typically shows relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved [6].
  • Concurrency Pattern
    To ensure the above discussed thread safe programming in a multi-threaded programming paradigm where the focus is on the interactions between tasks that execute in parallel, we need to design applications using design patterns called concurrency patterns [7].

Types of Concurrency Patterns

Active Object

The Active Object pattern decouples method execution from method invocation in order to simplify synchronized access to an object that resides in its own thread of control so that one or more independent threads can access data modeled as a single object [8]. This pattern is commonly used in distributed systems requiring multi-threaded servers and in client applications, such as windowing systems and network browsers which employ active objects to simplify concurrent, asynchronous network operations [8].

This pattern consists of certain important components and describes the interactions between these components [8]: The Proxy component provides interface for clients with publicly accessible methods. The Scheduler component schedules requests to decide which request will be executed next. There is another interface which defines the method request on an active object. There also exists a list of pending requests from clients along with the implementation of the active object method.

Class Diagram for Active Object [9]

An example of the Scheduler component in Java [9]:

public class Scheduler
{
  private List act_list = new ArrayList();
  private synchronized IMethodRequest get()
  public synchronized void insert(IMethodRequest mr)
  private class Dispatcher extends Thread
}


Balking Pattern

The balking pattern only executes an action on an object when the object is in a particular state so if we are trying to access an object in a particular state which differs from the expected state, the object will "balk".

For example, in Java, an IllegalStateException might be thrown in this case [10]. To avoid such an error from occurring, the object can attempt to acquire write locks which will time out if the object is not in the right state. Also, if an object’s method is called when the object is not in an appropriate state to execute that method, have the method return without doing anything [11].

An example of the Scheduler component in Java [10]:

public class Example{
   private boolean jobInProgress = false; 
   public void job(){
       synchronized(this){
          if(jobInProgress){
              return;
          }
          jobInProgress = true;
       }
   }
}


Read/Write Lock Pattern

This pattern allows concurrent read access to an object but requires exclusive access for write operations [11]. This means that when an object is being written to, the object access will be blocked for all other read operations.


References

[1] http://searchcio-midmarket.techtarget.com/sDefinition/0,,sid183_gci331590,00.html
[2] http://en.wikipedia.org/wiki/Thread_safety
[3] http://www.multicoreinfo.com/2009/03/thread-safe/
[4] http://www.cdac.in/html/events/beta-test/PEEP-2008-web-page/peep-2008-handson-webpage-mode-1/pthreads-peep-2008/pthreads-peep-2008-overview.html
[5] http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.aix.genprogc/doc/genprogc/writing_reentrant_thread_safe_code.htm
[6] http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29
[7] http://en.wikipedia.org/wiki/Concurrency_pattern
[8] http://www.cs.wustl.edu/~schmidt/PDF/Act-Obj.pdf
[9] http://www.cs.uu.nl/docs/vakken/no/active_object_pattern.pdf
[10] http://en.wikipedia.org/wiki/Balking_pattern
[11] http://www.mindspring.com/~mgrand/pattern_synopses.htm