FACTOID # 57: In 2002, every 1000 Swedes made a bus.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

FACTS & STATISTICS    Simple view

  1. Select countries to view: (hold down Control key and click to select several)

     

     

    Compare:

     

     

  1. Select fact or statistic: (* = graphable)

     

     

     

  2. (OPTIONAL) Compare to statistic: (both need to be graphable)

     

     

     

  3. View result as:

     

       
(OR) SEARCH ALL encyclopedia, stats & forums:   

Encyclopedia > Thread safe

Thread-safety is a computer programming concept applicable to multi-threaded programs. A piece of code is thread-safe if it is reentrant or protected from multiple simultaneous execution by some form of mutual exclusion.


Thread-safety is a key challenge in multi-threaded programming. It was once only a concern of the operating system programmer but has of late become a commonplace issue to be tackled by the everyday programmer. In a multi-threaded program, several threads execute simultaneously in a shared address space. Every thread has access to virtually all the memory of every other thread. Thus the flow of control and the sequence of accesses to data often have little relation to what would be reasonably expected by looking at the text of the program. This violates the principle of least astonishment. Thread-safety is a property aimed for so as to minimize surprising behaviour by re-establishing some of the correspondences between the actual flow of control and the text of the program.


The requirement for thread-safety highlights the inherent tension in multi-threaded programming: 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.


It is not easy to determine if a piece of code is thread-safe or not. However, there are several indicators that suggest the need for careful examination to see if it is unsafe:

A subroutine that only uses variables from the stack, depends only on the arguments passed in, and calls other subroutines with similar properties (a so-called "pure function" in C compiler circles) is reentrant, and thus thread-safe.


As seen in the definition, there are a few ways to achieve thread-safety:

  • reentrancy: Basically, writing code in such a way as to avoid sharing of data across threads
  • mutual exclusion: Access to shared data is serialized using mechanisms that ensure only one thread is accessing the shared data at any time. If a piece of code accesses multiple shared pieces of data, there needs to be an enormous amount of care in using mutual exclusion mechanisms -- problems include race conditions, deadlocks, livelocks, starvation, and various other ills enumerated in many operating systems textbooks.

A commonly used idiom combines both approaches:

  • make changes to a private copy of the data, and finally, atomically update the shared data from the private copy. Thus, most of the code would be close to re-entrant, and the amount of time spent serialized would be small.

The concept of exception safety is closely related, since it again deals with (synchronous) flows of control not directly correlated to the text of a program. Several of the idioms used are similar.


Example

If you are writing a network program, like a web browser, you need to have many threads of execution, i.e many different pieces of code running at the same time to perform different tasks like

  1. Graphic User Interaface GUI
  2. Network I/O
  3. Disk I/O

These threads allow the user to cancel a page that is loading, while interacting with the browser, when it is fetching a file from the Internet. The same functions, if done with a single-threaded browser, will make you wait; actions are serially done, rather than in parallel. This means you cannot do two things at a time, like reading help pages, and downloading files, in a single threaded application.


See also

External links

  • Short description (http://whatis.techtarget.com/definition/0,,sid9_gci331590,00.html)
  • Writing Reentrant and Thread-Safe Code (http://davinci01.man.ac.uk/aix433/aixprggd/genprogc/writing_reentrant_thread_safe_code.htm)
  • Article "Thread-safe webapps using Spring (http://javalobby.org/articles/thread-safe/index.jsp)" by Steven Devijver
  • Thread-safe design (http://www.javaworld.com/javaworld/javaqa/1999-04/01-threadsafe.html)
  • Article "Design for thread safety (http://www.javaworld.com/javaworld/jw-08-1998/jw-08-techniques.html)" by Bill Venners
  • Article "Write thread-safe servlets (http://www.javaworld.com/javaworld/jw-07-2004/jw-0712-threadsafe.html)" by Phillip Bridgham
  • Article "Smart Pointer Thread Safety (http://jelovic.com/articles/smart_pointer_thread_safety.htm)" by Dejan Jelovic
  • Citations from CiteSeer (http://citeseer.org/cs?q=thread+safety)

  Results from FactBites:
 
Designing for Thread Safety (795 words)
Thread safety simply means that the fields of an object or class always maintain a valid state, as observed by other objects and classes, even when used concurrently by multiple threads.
Once you introduce multiple threads, however, the JVM may interrupt the thread executing one method while the object's instance variables are still in a temporarily invalid state.
Because all threads share the same heap, and the heap is where all instance variables are stored, multiple threads can attempt to use the same object's instance variables concurrently.
Thread-safe fuctions (2336 words)
However, if more than one thread is attempting to 'strcat()' to the _same_ string (the same physical location in memory) at the same time, the resultant string would be undefined.
I was only referring to thread safety wrt data that is not under the programmer's control (that is, possible global/static data manipulation inside the library function implementation).
However, if more than 1 thread is attempting a 'strcat()' to the _same_ memory location, the results are undefined unless that memory is protected against concurrent access through a mutex.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.