Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Thread safety
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Thread safety
(section)
Add topic