Is it safe to pass arguments by reference into a stdthread function?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Passing arguments by reference into a std::thread function can be safe, but only if lifetime and synchronization are both correct. The language gives you the mechanism, not the guarantees. In real code, the question is not “can I pass by reference,” but “will the referenced object still exist, and will concurrent access be race-free.”
std::thread Copies Arguments by Default
By default, std::thread stores copies of the arguments you pass to it. That protects you from some lifetime bugs, but it also means the thread function does not automatically modify the original object.
value in main remains unchanged because the thread receives a copy.
Pass by Reference Explicitly with std::ref
If you want the thread to operate on the original object, use std::ref.
This is safe only because value remains alive until after join().
The Two Real Safety Conditions
Passing by reference is safe only when both of these are true:
- The referenced object outlives all thread access.
- Access is synchronized if at least one thread writes.
If either condition fails, the program has undefined behavior.
Lifetime Safety
The most common bug is passing a reference to an object that goes out of scope before the worker finishes. This is especially dangerous with detached threads.
Unsafe idea:
- create local variable.
- launch detached thread with
std::ref(local). - return from the function.
The thread then holds a dangling reference. In most codebases, detaching threads with referenced stack objects is an architectural mistake.
Synchronization Safety
Even if lifetime is correct, simultaneous access can still be unsafe. If multiple threads read and write the same referenced object, you need a mutex or atomic design.
Without the mutex, this code has a data race.
Passing by const Reference
If the thread only needs read access, use std::cref and a const reference parameter. This does not remove lifetime requirements, but it reduces accidental mutation.
When Reference Passing Is the Wrong Tool
Sometimes ownership transfer is clearer than shared reference access. For example, if a worker should exclusively own a resource, pass a std::unique_ptr with std::move instead of sharing by reference.
Reference passing is best when:
- one thread owns the object lifetime.
- sharing is intentional.
- synchronization policy is clear.
Common Pitfalls
- Forgetting that
std::threadcopies arguments unlessstd::refis used. - Passing references to stack objects that go out of scope too early.
- Assuming reference passing is safe without locks or atomics.
- Detaching threads that still depend on referenced objects.
- Using shared references when ownership transfer would be simpler.
Summary
- Passing by reference into
std::threadcan be safe, but only under strict conditions. - Use
std::reforstd::crefwhen you truly need reference semantics. - Ensure the referenced object outlives the worker thread.
- Synchronize shared mutable access to avoid data races.
- Prefer clearer ownership models when shared reference access is unnecessary.
Related reading
- Is it safe to return from function before all stdfutures are finished?
- Is it safe to use async with external js files?
- Is it smart to replace boostthread and boostmutex with c11 equivalents?
- Is it thread-safe when using tf.Session in inference service?
- Is Meyers' implementation of the Singleton pattern thread safe?
- Is stdmutex sequentially consistent?
- Is it true that async should not be used for high-CPU tasks?
- Is iterating ConcurrentHashMap values thread safe?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.