Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Thread safety
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== In the following piece of [[Java (programming language)|Java]] code, the Java keyword [[list of Java keywords#synchronized|synchronized]] makes the method thread-safe: <syntaxhighlight lang="java"> class Counter { private int i = 0; public synchronized void inc() { i++; } } </syntaxhighlight> In the [[C (programming language)|C programming language]], each thread has its own stack. However, a [[static variable]] is not kept on the stack; all threads share simultaneous access to it. If multiple threads overlap while running the same function, it is possible that a static variable might be changed by one thread while another is midway through checking it. This difficult-to-diagnose [[logic error]], which may compile and run properly most of the time, is called a [[race condition#Software|race condition]]. One common way to avoid this is to use another shared variable as a [[lock (computer science)|"lock" or "mutex"]] (from '''mut'''ual '''ex'''clusion). In the following piece of C code, the function is thread-safe, but not reentrant: <span class="anchor" id="mutexexample"></span> <syntaxhighlight lang="c"> # include <pthread.h> int increment_counter () { static int counter = 0; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // only allow one thread to increment at a time pthread_mutex_lock(&mutex); ++counter; // store value before any other threads increment it further int result = counter; pthread_mutex_unlock(&mutex); return result; } </syntaxhighlight> In the above, <code>increment_counter</code> can be called by different threads without any problem since a mutex is used to synchronize all access to the shared <code>counter</code> variable. But if the function is used in a reentrant interrupt handler and a second interrupt arises while the mutex is locked, the second routine will hang forever. As interrupt servicing can disable other interrupts, the whole system could suffer. The same function can be implemented to be both thread-safe and reentrant using the lock-free [[linearizability|atomics]] in [[C++11]]: <syntaxhighlight lang="cpp"> # include <atomic> int increment_counter () { static std::atomic<int> counter(0); // increment is guaranteed to be done atomically int result = ++counter; return result; } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)