CSC/ECE 517 Fall 2009/wiki2 14 san: Difference between revisions
Line 6: | Line 6: | ||
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]. | 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]. | ||
[[Image:thread-unsafe. | [[Image:thread-unsafe.jpg|frame|Typical thread-unsafe scenario [4]]] | ||
Revision as of 03:11, 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].
Two important questions arise:
1. 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].
2. 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].
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