Coming from C# / Java programming, creating new threads in programs tends to add a significant amount of overhead (for example 1MB per thread in C#). I was curious what kind of overhead do C++11 threads introduce.
Asked
Active
Viewed 268 times
2
-
3Virtual memory is not normally considered a scarce resource. – David Schwartz Apr 27 '14 at 03:50
-
@DavidSchwartz Under 32 bit it is. In fact, long-running processes can (and do) die from address space starvation when their dynamic memory becomes fragmented. – danielschemmel Apr 27 '14 at 03:55
2 Answers
8
C++ offers a fairly thin wrapper on top of the underlying implementation, leading to no significant additional overhead. In fact, you can even get a handle to the underlying OS thread, which will be a __gthread_t
, which is a pthread handle for g++ and a WINAPI thread handle for Visual C++.
However, threads do have intrinsic overhead, because they need to be scheduled by the OS, contain a stack and so forth.
An analysis by Mark Russinovich goes through the limits of thread creation under Windows. These limits are of course caused by the thread overhead and give:
- A thread requires about 1 MB of virtual address space (default linker setting)
- 4-16 KB of initial commit size
- 12-48 KB of nonpageable memory

danielschemmel
- 10,885
- 1
- 36
- 58
-
Also, it should be noted that the thin wrapper of std::thread looks alot like boost::thread, which for some machines is just a wrapper of pthreads. – It'sPete Apr 27 '14 at 03:57
-
1@It'sPete In fact, std::thread is such a thin wrapper for pthreads that for both gcc and clang add -lpthread is required for using it ;) – danielschemmel Apr 27 '14 at 03:59
3
Already solved here:
How much overhead is there when creating a thread?
Cliff Notes: this will be system dependent and the best way to know is to benchmark on your target system.