How to pass arguments to a thread?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Passing arguments to threads is fundamental for concurrent programming. Every major language has its own mechanism: Python uses the args and kwargs parameters of threading.Thread, Java passes data through constructors or lambda captures, C# uses ParameterizedThreadStart or lambdas, C++ uses std::thread constructor arguments, and Go uses goroutine function parameters. The key concern across all languages is thread safety — shared mutable data must be protected to avoid race conditions.
Python
args must be a tuple. A single argument requires a trailing comma: args=(value,). Forgetting the comma passes the value as a non-tuple iterable.
Python with a Shared Mutable Argument
When threads share a mutable argument (like a list), protect writes with a Lock to prevent race conditions.
Java
Lambda-captured variables must be effectively final in Java. If you need to modify them, use AtomicInteger or an array wrapper.
C#
Lambdas capture variables by reference in C#. If the variable changes before the thread runs, the thread sees the new value. Copy to a local variable for safety.
C++
std::thread copies arguments by default. Use std::ref() to pass by reference, and protect shared data with std::mutex.
Go
Goroutines take function arguments directly. Arguments are evaluated at the go statement, so they are safe from the loop variable capture problem as long as you pass them as parameters.
Go: Loop Variable Capture
Common Pitfalls
- Python single-arg tuple:
args=(value)is not a tuple — it is justvaluein parentheses. Useargs=(value,)with a trailing comma for a single argument. - Java lambda variable must be final: Lambdas in Java can only capture effectively final variables. If you need mutable state, use
AtomicReference,AtomicInteger, or pass an object. - C# closure over loop variable:
for (int i = 0; i < 5; i++) new Thread(() => Console.Write(i)).Start()may print5five times. Copyito a local:var local = i; - C++ dangling reference: Passing a local variable by
std::refto a detached thread causes undefined behavior if the variable goes out of scope before the thread finishes. Either join the thread or copy the data. - Go loop variable capture: Goroutines with closures capture the loop variable by reference. Pass it as a function argument to get a per-iteration copy.
Summary
- Python:
threading.Thread(target=fn, args=(a, b), kwargs={"key": val}) - Java: pass via constructor (Runnable) or lambda capture (effectively final only)
- C#: lambda capture or
ParameterizedThreadStart, preferTask.Runfor modern code - C++:
std::thread(fn, arg1, arg2)copies by default, usestd::ref()for references - Go: pass arguments directly to the goroutine function to avoid closure capture bugs
- Always protect shared mutable data with locks, mutexes, or atomic types
Related reading
- How to pass async variable in template action function?
- How to pass body into aiohttp get request?
- How to pass parameters to ThreadStart method in Thread?
- How to pass parameters to ThreadStart method in Thread?
- How to pass Task results to other Tasks not using continuations
- How to pass the UI Dispatcher to the ViewModel
- How to pause / sleep thread or process in Android?
- How to Pause and Resume the Task which runs Asynchronously
.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.