C++
'std::thread' Is Not a Member of 'std' Fix
std::thread is a C++11 library class declared in the header
Written November 27, 2026 · Tested with GCC 13.3.0 · 9 min read
- The error: the include is missing
- The same error through a neighboring header: <mutex> is not enough
- The error: the mode is before C++11
- The fix
Written November 27, 2026
Tested with GCC 13.3.0
What this guide is
std::thread is a class that represents a single thread of execution, available since C++11 and defined in the header <thread>. The same header also declares the std::this_thread namespace — get_id, yield, sleep_for, and sleep_until — and, since C++20, std::jthread. A thread starts running the function passed to its constructor as soon as the object is constructed, and the program later waits for it with join() or lets it run independently with detach().
Because the class lives in one specific header and one specific language mode, 'thread' is not a member of 'std' has exactly two common causes, and GCC's notes under the error tell them apart:
- The
#include <thread>line is missing. GCC adds a note naming the header:'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'? - The file is compiled in C++98 or C++03 mode, with the include present. GCC adds a different note —
'std::thread' is only available from C++11 onwards— plus an#errorfrom inside the header itself.
A third question travels with this error — whether -pthread is still required — and on a current Linux toolchain the answer is no, for a specific reason covered below.
The error: the include is missing
This program uses std::thread but includes only <iostream>:
#include <iostream>
void work() { std::cout << "worker ran\n"; }
int main() {
std::thread t(work);
t.join();
return 0;
}
$ g++ -std=c++11 noinclude.cpp -o noinclude_prog
noinclude.cpp: In function ‘int main()’:
noinclude.cpp:4:10: error: ‘thread’ is not a member of ‘std’
4 | std::thread t(work);
| ^~~~~~
noinclude.cpp:2:1: note: ‘std::thread’ is defined in header ‘<thread>’; did you forget to ‘#include <thread>’?
1 | #include <iostream>
+++ |+#include <thread>
2 | void work() { std::cout << "worker ran\n"; }
noinclude.cpp:5:5: error: ‘t’ was not declared in this scope; did you mean ‘tm’?
5 | t.join();
| ^
| tm
The default compilation mode prints the identical error, so this shape is not a mode problem. The second error about t is a consequence, not a separate defect: because std::thread was never declared, the variable t was never created, and both errors disappear together when the include is added.
The same error through a neighboring header: <mutex> is not enough
Thread code often includes <mutex> for the locks the worker uses, and that include feels close enough. It is not. This program differs from the one above only in the added <mutex> line, and it fails the same way:
#include <iostream>
#include <mutex>
void work() { std::cout << "worker ran\n"; }
int main() {
std::thread t(work);
t.join();
return 0;
}
$ g++ -std=c++11 mutexonly.cpp -o mutexonly_prog
mutexonly.cpp: In function ‘int main()’:
mutexonly.cpp:5:10: error: ‘thread’ is not a member of ‘std’
5 | std::thread t(work);
| ^~~~~~
mutexonly.cpp:3:1: note: ‘std::thread’ is defined in header ‘<thread>’; did you forget to ‘#include <thread>’?
2 | #include <mutex>
+++ |+#include <thread>
3 | void work() { std::cout << "worker ran\n"; }
mutexonly.cpp:6:5: error: ‘t’ was not declared in this scope; did you mean ‘tm’?
6 | t.join();
| ^
| tm
No standard header is required to declare std::thread on behalf of <thread>. If a program compiles on one machine because some header happens to pull <thread> in as a side effect, that is an accident of that library version, not a rule to build on. Include <thread> in every file that names std::thread, std::this_thread, or std::thread::hardware_concurrency.
The error: the mode is before C++11
The second shape keeps the include and drops the mode. The libstdc++ manual lists <thread> among the C++ 2011 library headers, available in C++11 compilation mode — -std=c++11 or -std=gnu++11 — and warns that including these headers in C++98/03 mode may result in compilation errors. With GCC 13.3.0 the result is exact: an #error from inside the header, then the same 'thread' is not a member of 'std' message with a different note:
$ g++ -std=c++98 good.cpp -o good_prog
In file included from /usr/include/c++/13/thread:37,
from good.cpp:2:
/usr/include/c++/13/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
32 | #error This file requires compiler and library support \
| ^~~~~
good.cpp: In function ‘int main()’:
good.cpp:5:10: error: ‘thread’ is not a member of ‘std’
5 | std::thread t(work);
| ^~~~~~
good.cpp:5:10: note: ‘std::thread’ is only available from C++11 onwards
good.cpp:6:5: error: ‘t’ was not declared in this scope; did you mean ‘tm’?
6 | t.join();
| ^
| tm
good.cpp:7:36: error: ‘std::thread’ has not been declared
7 | std::cout << "cores: " << std::thread::hardware_concurrency() << "\n";
| ^~~~~~
Compiling with -std=c++03 prints the identical output. No include change fixes this shape, because the include is already there — the header refuses the mode. The fix is the flag: -std=c++11 at minimum, or any newer mode.
The fix
Add the header and compile in C++11 mode or newer:
#include <iostream>
#include <thread>
void work() { std::cout << "worker ran\n"; }
int main() {
std::thread t(work);
t.join();
std::cout << "cores: " << std::thread::hardware_concurrency() << "\n";
std::cout << "main done\n";
return 0;
}
$ g++ -std=c++11 good.cpp -o good_prog
$ ./good_prog
worker ran
cores: 32
main done
The same program also compiles and runs with -std=c++17, and with no -std flag at all — GCC 13 defaults to C++17 mode (__cplusplus 201703L), which is why the missing-include error appears in default mode too: the mode was never the problem there, only the include was. std::thread::hardware_concurrency() is a static member of the class, so it needs the class declaration just as much as constructing a thread does; naming it without <thread> fails with 'std::thread' has not been declared, a wording variant of the same missing include.
Do you still need -pthread?
On older Linux systems, advice for any thread error was to add -pthread to the compile and link commands, and build files still carry the flag for that reason. On a current system it is not what fixes this error, and it is not needed for the program to run correctly either. The fixed program above was compiled with no -pthread flag, in C++11 mode and in C++17 mode, and ran correctly both times:
$ g++ -std=c++11 good.cpp -o good_prog
$ ./good_prog
worker ran
cores: 32
main done
The reason is a library change underneath the compiler: glibc 2.34 merged libpthread into libc, along with libdl, libutil, and libanl, in order to support safe use of threading at any time by a process. With the thread functions inside libc itself — the system here runs glibc 2.39 — there is no separate library left for -pthread to add at link time, and the resulting binary's ldd output lists libc and libstdc++ (with libgcc_s and libm) and no libpthread line. Keeping -pthread in a build is harmless and stays the portable choice for build files that also target older systems, but when 'thread' is not a member of 'std' is the error in front of you, adding it changes nothing: that error is about a declaration, and -pthread declares nothing.
The neighboring error: std::this_thread has not been declared
Code that only sleeps or yields, without creating a thread, hits a different wording of the same defect:
#include <chrono>
#include <iostream>
int main() {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::cout << "slept\n";
return 0;
}
$ g++ -std=c++11 thisthread.cpp -o thisthread_prog
thisthread.cpp: In function ‘int main()’:
thisthread.cpp:4:10: error: ‘std::this_thread’ has not been declared
4 | std::this_thread::sleep_for(std::chrono::milliseconds(1));
| ^~~~~~~~~~~
std::this_thread is a namespace, not a member of std in the class sense, so GCC words the error differently — but the cause and the fix are the same: this_thread is declared in <thread>, so add that include. <chrono> declares the duration types, not the sleep functions. With <thread> added, the program compiles in C++11 mode and runs:
$ g++ -std=c++11 fixed2.cpp -o fixed2_prog
$ ./fixed2_prog
slept
The exact rule
std::thread,std::this_thread, andstd::thread::hardware_concurrencyare declared by#include <thread>, in C++11 mode or newer, and nowhere else.<iostream>and<mutex>do not declare them.'thread' is not a member of 'std'with the notedid you forget to '#include <thread>'?means the include is missing. The same message with the noteonly available from C++11 onwards, plus an#errorfromc++0x_warning.h, means the mode is C++98 or C++03.- The follow-on error
't' was not declared in this scopeis a consequence of the first error, not a second defect. Fix the declaration and it disappears. - On glibc 2.34 or newer,
-pthreadis not required for astd::threadprogram to compile, link, or run correctly. It also never fixes a missing declaration.
The fixes, ranked
- First, read GCC's note under the error. It names the cause directly: a missing
<thread>include, or a pre-C++11 mode. - Second, add
#include <thread>to every file that namesstd::thread,std::this_thread, orhardware_concurrency— do not rely on another header pulling it in. - Third, if the
#errorabout the ISO C++ 2011 standard appears, raise the mode:-std=c++11at minimum, or-std=gnu++11, or any newer standard mode. - Fourth, leave
-pthreadalone while fixing this error. On current glibc it is not needed, and on any system it cannot supply a missing declaration.
Which fix for which cause
#include <thread>missing: the message is'thread' is not a member of 'std'— add#include <thread>.- Only
<mutex>included: same message, same note — add#include <thread>. - Mode is C++98 or C++03: an
#errorplus the same message — compile with-std=c++11or newer. std::this_threadwithout<thread>: the message is'std::this_thread' has not been declared— add#include <thread>.hardware_concurrencywithout<thread>: the message is'std::thread' has not been declared— add#include <thread>.- No
-pthreadon glibc 2.39: no error, and the program runs correctly — no change needed.
Checklist
- Read the note under the error before changing anything.
did you forget to '#include <thread>'?andonly available from C++11 onwardspoint at two different fixes. - Check the include list of the file that names
std::thread, not just the project. The include must be in that file, or in a header that file includes directly by design. - Check the mode on the compile command:
-std=c++98and-std=c++03fail even with the include present;-std=c++11is the floor. - Do not chase the
't' was not declared in this scopeerror separately. It follows the failedstd::threaddeclaration and clears with it. - If the code uses only
std::this_thread::sleep_fororsleep_until, it still needs<thread>;<chrono>supplies only the duration arguments. - After the fix, confirm the program runs, not just that it compiles: the fixed program here prints
worker ran, a core count fromhardware_concurrency, andmain done, in that order. - Call
join()ordetach()on every thread you construct. A thread object destroyed while still joinable ends the program — that is a runtime rule of the class, separate from the declaration error this guide covers.
Related guides
- C++23 std::expected Error-Handling Pipelines — another standard-library feature whose availability turns on the header and the language mode, not on the compiler's age alone.
- C++20 Modules with CMake 3.28 and Ninja — setting up a current GCC toolchain and build, the environment in which the C++11 and later library headers are available.
- C++ Expected Unqualified-Id Fix — a neighboring front-end error where the compiler's wording points at the wrong half of the defect.
Sources
- cppreference,
std::thread— forstd::threadbeing a class that represents a single thread of execution since C++11, for threads beginning execution on construction, and forhardware_concurrencyreturning the number of concurrent threads supported. - cppreference, Standard library header
<thread>(C++11) — for<thread>declaringstd::thread,std::jthread, and thestd::this_threadfunctionsget_id,yield,sleep_for, andsleep_until. - GCC, libstdc++ Manual — Headers — for
<thread>being listed among the C++ 2011 library headers, available in C++11 compilation mode (-std=c++11or-std=gnu++11), with including these headers in C++98/03 mode possibly resulting in compilation errors. - glibc Wiki, Release 2.34 — for libpthread, libdl, libutil, and libanl being merged into libc in glibc 2.34, to support safe use of threading at any time by a process.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.