When to Use Spin Locks and How They Work in Multithreaded Code
This article explains the concept of spin locks, compares them with mutexes, shows how to use the pthread API and C++ atomic_flag to implement them, outlines suitable scenarios, highlights CPU‑usage pitfalls, and provides practical code examples.
What is a spin lock?
A spin lock is a synchronization primitive in which a thread repeatedly checks (spins) for a lock to become available instead of sleeping. It is useful when the expected wait time is very short, because the thread can acquire the lock immediately once it is released.
pthread spin‑lock API (Linux)
pthread_spinlock_t spin;
pthread_spin_init(&spin, 0); // initialize (0 = process‑shared)
pthread_spin_lock(&spin); // acquire (spins until the lock is free)
/* critical section */
pthread_spin_unlock(&spin); // release
pthread_spin_destroy(&spin); // destroyComplete pthread example
#include <pthread.h>
#include <stdio.h>
int shared_data = 0;
pthread_spinlock_t spinlock;
void* increment_data(void* arg) {
pthread_spin_lock(&spinlock);
shared_data++;
printf("Thread %d: shared_data = %d
", *(int*)arg, shared_data);
pthread_spin_unlock(&spinlock);
return NULL;
}
int main() {
pthread_t threads[5];
int thread_ids[5] = {0,1,2,3,4};
pthread_spin_init(&spinlock, 0);
for (int i = 0; i < 5; ++i) {
pthread_create(&threads[i], NULL, increment_data, &thread_ids[i]);
}
for (int i = 0; i < 5; ++i) {
pthread_join(threads[i], NULL);
}
pthread_spin_destroy(&spinlock);
return 0;
}Each thread increments shared_data while holding the spin lock, guaranteeing exclusive access.
When to choose a spin lock
Very short critical sections : Updating a small variable or performing a quick read/write where the overhead of putting a thread to sleep would dominate.
Multi‑core systems : One core can spin while other cores continue useful work, improving overall throughput.
Kernel‑level or low‑latency paths : Situations where a context switch is too costly.
If the protected code may run for a noticeable amount of time, a mutex (or other blocking lock) is preferable.
CPU consumption and mitigation
Spinning keeps the CPU busy; prolonged holding of a spin lock can cause high CPU usage. A common mitigation is to limit the number of spin attempts and fall back to a blocking lock.
int try_spinlock_with_limit(pthread_spinlock_t *lock, int max_attempts) {
int attempt = 0;
while (attempt < max_attempts) {
if (pthread_spin_trylock(lock) == 0) {
return 0; // acquired
}
++attempt;
}
return -1; // give up after max_attempts
}Pros and cons
Advantages
Fast acquisition – no context‑switch overhead.
Works well on multi‑core CPUs where a spinning thread does not block others.
Disadvantages
Consumes CPU cycles while waiting.
Only suitable for short‑duration waits; long waits waste resources.
C++ spin lock using std::atomic_flag
Standard C++ does not provide a spin‑lock type, but a simple spin lock can be built with std::atomic_flag:
#include <atomic>
#include <thread>
class SpinLock {
private:
std::atomic_flag flag = ATOMIC_FLAG_INIT;
public:
void lock() {
while (flag.test_and_set(std::memory_order_acquire)) {
// busy‑wait
}
}
void unlock() {
flag.clear(std::memory_order_release);
}
};Usage example:
SpinLock spinlock;
void critical_section() {
spinlock.lock();
// ... critical code ...
spinlock.unlock();
}Conclusion
Spin locks provide low‑latency mutual exclusion for very short critical sections, especially on multi‑core systems or in kernel code. They avoid the overhead of sleeping and waking threads but can waste CPU if the lock is held too long. Use them only when the protected section is guaranteed to be brief, and consider a fallback (e.g., pthread_spin_trylock with a limit) for longer waits.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
