'std::mutex' Is Not a Member of 'std' Fix
Written November 28, 2026 · Tested with GCC 13.3.0 · 12 min read
- The error: the include is missing
- Neighboring headers: some fail, some only appear to work
- The fix
- The guarded counter: four threads, one mutex
Written November 28, 2026
Tested with GCC 13.3.0
What this guide is
std::mutex is a class — not a class template — defined in the header <mutex>. It is a synchronization primitive that protects shared data from being accessed by several threads at once: a thread owns the mutex from a successful lock or try_lock call until it calls unlock, ownership is exclusive and non-recursive, and the class is neither copyable nor movable.
Because the class lives in one specific header, 'mutex' is not a member of 'std' has one common cause:
- The
#include <mutex>line is missing. For this error GCC does name the header: its note says'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?, and following that note is the whole fix.
Some headers that sit near mutex code do not rescue the program, and a few rescue it only by accident. Each one is separated below.
The error: the include is missing
This program declares a std::mutex, locks it, prints, and unlocks it, but includes only <iostream>:
#include <iostream>
int main() {
std::mutex m;
m.lock();
std::cout << 1 << "\n";
m.unlock();
return 0;
}
$ g++ -std=c++20 a_noinclude.cpp -o a_prog
a_noinclude.cpp: In function 'int main()':
a_noinclude.cpp:3:10: error: 'mutex' is not a member of 'std'
3 | std::mutex m;
| ^~~~~
a_noinclude.cpp:2:1: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
1 | #include <iostream>
+++ |+#include <mutex>
2 | int main() {
a_noinclude.cpp:4:5: error: 'm' was not declared in this scope; did you mean 'tm'?
4 | m.lock();
| ^
| tm
The first error is the defect. The 'm' was not declared in this scope error that follows is a knock-on effect: the declaration of m failed, so every later use of m fails too. Fix the include and both errors go.
Neighboring headers: some fail, some only appear to work
Swapping <iostream> for another nearby header changes nothing for most of them. Compiled one at a time as the only header besides <iostream>, each of these fails with the same first error, 'mutex' is not a member of 'std':
<vector><string><algorithm><utility>
Three other headers behave differently on this library, and the difference matters. This program includes only <thread> besides <iostream>:
#include <iostream>
#include <thread>
int main() {
std::mutex m;
m.lock();
std::cout << 1 << "\n";
m.unlock();
return 0;
}
$ g++ -std=c++20 e_threadonly.cpp -o e_prog
$ ./e_prog
1
It compiles and prints 1. The same happens with <memory> and with <condition_variable> as the only extra header: on this libstdc++, each of those headers pulls <mutex> in transitively, so the name is declared even though the program never includes <mutex> itself. That is a property of this library version, not a rule of the language — no header except <mutex> is required to declare std::mutex, and another library, or another version of this one, may stop pulling it in. A program that uses std::mutex includes <mutex>, whether or not it happens to compile without it. std::thread itself is declared by <thread>; the mutex name arriving alongside it is the accident, not the arrangement.
The fix
Add the header:
#include <iostream>
#include <mutex>
int main() {
std::mutex m;
m.lock();
std::cout << 1 << "\n";
m.unlock();
return 0;
}
$ g++ -std=c++20 a_fixed.cpp -o a_fixed_prog
$ ./a_fixed_prog
1
One include, <mutex>, declares the class. In real code the mutex is usually owned through a wrapper rather than locked and unlocked by hand, and the wrappers live in the same header — the next section uses one, and the section after it shows what happens to all three wrappers when the header is missing.
The guarded counter: four threads, one mutex
std::lock_guard is a class template in <mutex> that owns a mutex for the duration of a scoped block: it locks on construction and unlocks on destruction. Guarding every increment with it makes the total exact — four threads, 250 increments each, one shared counter:
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
int main() {
std::mutex m;
int counter = 0;
std::vector<std::thread> workers;
for (int t = 0; t < 4; ++t) {
workers.emplace_back([&]() {
for (int i = 0; i < 250; ++i) {
std::lock_guard<std::mutex> guard(m);
++counter;
}
});
}
for (auto& w : workers) w.join();
std::cout << counter << "\n";
return 0;
}
$ g++ -std=c++20 b_fixed.cpp -o b_prog
$ ./b_prog
1000
Every increment happens while the incrementing thread owns the mutex, so no increment is lost: the program prints 1000, every run.
The neighboring errors: the lock wrappers fail too
The wrappers are declared by the same header, so a missing <mutex> takes them down with the mutex itself. This program uses std::lock_guard without the header:
#include <iostream>
int main() {
std::mutex m;
std::lock_guard<std::mutex> guard(m);
std::cout << 1 << "\n";
return 0;
}
$ g++ -std=c++20 c_lockguard.cpp -o c_prog
c_lockguard.cpp: In function 'int main()':
c_lockguard.cpp:3:10: error: 'mutex' is not a member of 'std'
3 | std::mutex m;
| ^~~~~
c_lockguard.cpp:2:1: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
1 | #include <iostream>
+++ |+#include <mutex>
2 | int main() {
c_lockguard.cpp:4:10: error: 'lock_guard' is not a member of 'std'
4 | std::lock_guard<std::mutex> guard(m);
| ^~~~~~~~~~
c_lockguard.cpp:4:10: note: 'std::lock_guard' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
c_lockguard.cpp:4:26: error: 'mutex' is not a member of 'std'
4 | std::lock_guard<std::mutex> guard(m);
| ^~~~~
c_lockguard.cpp:4:26: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
c_lockguard.cpp:4:39: error: 'm' was not declared in this scope; did you mean 'tm'?
4 | std::lock_guard<std::mutex> guard(m);
| ^
| tm
c_lockguard.cpp:4:33: error: 'guard' was not declared in this scope
4 | std::lock_guard<std::mutex> guard(m);
| ^~~~~
The mutex error surfaces first, because the mutex is declared on the earlier line, and then lock_guard gets its own error with its own note naming the same header. std::unique_lock — the movable wrapper that allows deferred locking and works with condition variables — behaves the same way:
#include <iostream>
int main() {
std::mutex m;
std::unique_lock<std::mutex> lock(m);
std::cout << 1 << "\n";
return 0;
}
$ g++ -std=c++20 d_unique.cpp -o d_prog
d_unique.cpp: In function 'int main()':
d_unique.cpp:3:10: error: 'mutex' is not a member of 'std'
3 | std::mutex m;
| ^~~~~
d_unique.cpp:2:1: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
1 | #include <iostream>
+++ |+#include <mutex>
2 | int main() {
d_unique.cpp:4:10: error: 'unique_lock' is not a member of 'std'
4 | std::unique_lock<std::mutex> lock(m);
| ^~~~~~~~~~~
d_unique.cpp:4:10: note: 'std::unique_lock' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
d_unique.cpp:4:27: error: 'mutex' is not a member of 'std'
4 | std::unique_lock<std::mutex> lock(m);
| ^~~~~
d_unique.cpp:4:27: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
d_unique.cpp:4:39: error: 'm' was not declared in this scope; did you mean 'tm'?
4 | std::unique_lock<std::mutex> lock(m);
| ^
| tm
d_unique.cpp:4:34: error: 'lock' was not declared in this scope; did you mean 'clock'?
4 | std::unique_lock<std::mutex> lock(m);
| ^~~~
| clock
And std::scoped_lock — the C++17 wrapper that owns one or more mutexes for a scoped block, using a deadlock-avoidance algorithm when several are given — fails the same way again, its own error after the mutex error:
#include <iostream>
int main() {
std::mutex m;
std::scoped_lock lock(m);
std::cout << 1 << "\n";
return 0;
}
$ g++ -std=c++20 d2_scoped.cpp -o d2_prog
d2_scoped.cpp: In function 'int main()':
d2_scoped.cpp:3:10: error: 'mutex' is not a member of 'std'
3 | std::mutex m;
| ^~~~~
d2_scoped.cpp:2:1: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
1 | #include <iostream>
+++ |+#include <mutex>
2 | int main() {
d2_scoped.cpp:4:10: error: 'scoped_lock' is not a member of 'std'
4 | std::scoped_lock lock(m);
| ^~~~~~~~~~~
d2_scoped.cpp:4:10: note: 'std::scoped_lock' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
One added include declares all four names: the mutex and its three wrappers.
The neighboring mutexes: recursive and timed
The other mutex classes in the header fail the same way when it is missing. std::recursive_mutex, which the same thread may lock more than once, gets its own wording of the error with a note naming <mutex>:
#include <iostream>
int main() {
std::recursive_mutex m;
m.lock();
m.lock();
std::cout << 2 << "\n";
m.unlock();
m.unlock();
return 0;
}
$ g++ -std=c++20 f_recursive.cpp -o f_prog
f_recursive.cpp: In function 'int main()':
f_recursive.cpp:3:10: error: 'recursive_mutex' is not a member of 'std'
3 | std::recursive_mutex m;
| ^~~~~~~~~~~~~~~
f_recursive.cpp:2:1: note: 'std::recursive_mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
1 | #include <iostream>
+++ |+#include <mutex>
2 | int main() {
f_recursive.cpp:4:5: error: 'm' was not declared in this scope; did you mean 'tm'?
4 | m.lock();
| ^
| tm
So does std::timed_mutex, which adds locking attempts with a timeout:
#include <iostream>
int main() {
std::timed_mutex m;
bool got = m.try_lock();
std::cout << got << "\n";
if (got) m.unlock();
return 0;
}
$ g++ -std=c++20 f2_timed.cpp -o f2_prog
f2_timed.cpp: In function 'int main()':
f2_timed.cpp:3:10: error: 'timed_mutex' is not a member of 'std'
3 | std::timed_mutex m;
| ^~~~~~~~~~~
f2_timed.cpp:2:1: note: 'std::timed_mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
1 | #include <iostream>
+++ |+#include <mutex>
2 | int main() {
f2_timed.cpp:4:16: error: 'm' was not declared in this scope; did you mean 'tm'?
4 | bool got = m.try_lock();
| ^
| tm
With the header added, both classes and a scoped lock over a plain mutex work in one program — the recursive mutex takes two locks from the same thread and prints 2, the timed mutex reports a successful try_lock as 1, and the scoped block prints 3:
#include <iostream>
#include <mutex>
int main() {
std::recursive_mutex r;
r.lock();
r.lock();
std::cout << 2 << "\n";
r.unlock();
r.unlock();
std::timed_mutex t;
bool got = t.try_lock();
std::cout << got << "\n";
if (got) t.unlock();
std::mutex m;
{
std::scoped_lock lock(m);
std::cout << 3 << "\n";
}
return 0;
}
$ g++ -std=c++20 f_fixed.cpp -o f_fixed_prog
$ ./f_fixed_prog
2
1
3
A different wording: the name without its qualifier
Including <mutex> is not the only half of the name. This program includes the header but drops the std:: qualifier:
#include <iostream>
#include <mutex>
int main() {
mutex m;
m.lock();
std::cout << 1 << "\n";
m.unlock();
return 0;
}
$ g++ -std=c++20 g_unqualified.cpp -o g_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:4:5: error: 'mutex' was not declared in this scope; did you mean 'std::mutex'?
4 | mutex m;
| ^~~~~
| std::mutex
In file included from /usr/include/c++/13/mutex:45,
from g_unqualified.cpp:2:
/usr/include/c++/13/bits/std_mutex.h:96:9: note: 'std::mutex' declared here
96 | class mutex : private __mutex_base
| ^~~~~
g_unqualified.cpp:5:5: error: 'm' was not declared in this scope; did you mean 'tm'?
5 | m.lock();
| ^
| tm
That wording — was not declared in this scope — is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::mutex; adding includes does not bring the name out of namespace std.
The exact rule
std::mutexis declared by#include <mutex>, and nowhere else by rule.<iostream>,<vector>,<string>,<algorithm>, and<utility>do not declare it.'mutex' is not a member of 'std'means the<mutex>include is missing, and GCC's note names the header directly:'std::mutex' is defined in header '<mutex>'. Follow the note.<thread>,<memory>, and<condition_variable>happen to declarestd::mutexon this libstdc++ because they pull<mutex>in transitively. That is not a guarantee — a file that usesstd::mutexincludes<mutex>itself.std::lock_guard,std::unique_lock, andstd::scoped_lockare declared by the same#include <mutex>: in the programs shown here, which include only<iostream>beside the use, each gets its ownis not a member of 'std'error with a note naming<mutex>, after the mutex's own error, when that include is missing.std::recursive_mutexandstd::timed_mutexare declared by the same header too, and fail with the same shape of error when it is missing.std::threadis declared by#include <thread>, not by<mutex>: the two headers declare different names that are often used together.'mutex' was not declared in this scopefor an unqualifiedmutexuse is a different defect: qualify the name asstd::mutex— the include alone does not bring the name out of namespacestd.- The
'm' was not declared in this scopeerrors that follow a failed declaration are knock-on effects of the missing declaration, not separate defects.
The fixes, ranked
- First, check the include list of the file that names
std::mutex. If<mutex>is not there, add it — that is the fix in the common case, and it is the fix GCC's own note names. - Second, do not chase the follow-on errors. Fix the include and recompile; the undeclared-
merrors are consequences, not causes. - Third, if the program compiles only because
<thread>,<memory>, or<condition_variable>is present, add<mutex>anyway — the transitive include is this library's accident, not the program's arrangement. - Fourth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::mutexas well. - Fifth, if the failing name is a wrapper or a neighboring mutex —
lock_guard,unique_lock,scoped_lock,recursive_mutex,timed_mutex— the same added include fixes it: every one of those names shares the header.
Which fix for which cause
#include <mutex>missing,std::mutexused with<iostream>present: the message is'mutex' is not a member of 'std'— add#include <mutex>.- Only
<vector>,<string>,<algorithm>, or<utility>included: same message, same note — add#include <mutex>. - Only
<thread>,<memory>, or<condition_variable>included: the program compiles on this library, by transitive include — add#include <mutex>anyway so it compiles by rule. std::lock_guard,std::unique_lock, orstd::scoped_lockused without<mutex>: each name gets its ownis not a member of 'std'error with a note naming<mutex>— add#include <mutex>.std::recursive_mutexorstd::timed_mutexused without<mutex>: the message names that class, with a note naming<mutex>— add#include <mutex>.mutexused without thestd::qualifier,<mutex>included: the message is'mutex' was not declared in this scope— qualify the name asstd::mutex.
Checklist
- Check the include list of the file that names the mutex, not just the project. The include must be in that file, or in a header that file includes directly by design.
- Read the note under the error. For this error GCC names the header —
'std::mutex' is defined in header '<mutex>'— so take the fix from the note instead of guessing. - Do not rely on a neighboring header pulling
std::mutexin.<thread>,<memory>, and<condition_variable>do on this libstdc++, and no header is required to;<vector>,<string>,<algorithm>, and<utility>do not. - One header per name the file uses:
std::threadcomes from<thread>andstd::vectorfrom<vector>, even in a program whose subject is the mutex. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
1for the direct lock and unlock,1000for the guarded counter, and2,1, and3across the recursive, timed, and scoped sequence. - Prefer a wrapper over locking by hand where the scope allows it:
std::lock_guardunlocks when the scope ends, including when the scope is left early.
Related guides
- 'std::thread' Is Not a Member of 'std' Fix — the same missing-include shape for the thread class in
<thread>, the header that only appears to rescue the mutex on this library. - 'std::set' Is Not a Member of 'std' Fix — the same missing-include shape for the sorted container in
<set>, whose note likewise names its header. - Fix C++ Deadlock with std::scoped_lock — Two Mutexes, Opposite Order, One Hang — what the scoped lock from this header is for once it compiles: taking two mutexes at once without a deadlock.
Sources
- cppreference,
std::mutex— forstd::mutexbeing a class (since C++11), a synchronization primitive protecting shared data from simultaneous access by multiple threads, offering exclusive, non-recursive ownership throughlock,try_lock, andunlock, being neither copyable nor movable, and usually being managed throughstd::lock_guard,std::unique_lock, orstd::scoped_lockrather than accessed directly. - cppreference, Standard library header
<mutex>— for<mutex>declaringstd::mutex,std::recursive_mutex,std::timed_mutex, andstd::recursive_timed_mutexas classes, andstd::lock_guard,std::unique_lock, andstd::scoped_lockas class templates. - cppreference,
std::lock_guard— forstd::lock_guardbeing a class template (since C++11) that owns a mutex for the duration of a scoped block, locking on construction and releasing the mutex when the guard is destructed, and being non-copyable. - cppreference,
std::unique_lock— forstd::unique_lockbeing a class template (since C++11), a general-purpose mutex ownership wrapper allowing deferred locking, time-constrained locking attempts, transfer of lock ownership, and use with condition variables, and being movable but not copyable. - cppreference,
std::scoped_lock— forstd::scoped_lockbeing a class template (since C++17) that owns zero or more mutexes for the duration of a scoped block, using a deadlock-avoidance algorithm when several mutexes are given, and being non-copyable.
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.