This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/55471] New: c++ mutex does not work as expected
- From: "gustavo at atc dot ugr.es" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Mon, 26 Nov 2012 18:31:22 +0000
- Subject: [Bug c++/55471] New: c++ mutex does not work as expected
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55471
Bug #: 55471
Summary: c++ mutex does not work as expected
Classification: Unclassified
Product: gcc
Version: 4.7.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: gustavo@atc.ugr.es
Created attachment 28780
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=28780
source code with the bug
In a small test with a mutex it does not block all threads.
The bug is triggered by the place the mutex is declared:
* When the mutex is declared global it does not work.
* When it is a local variable it does work.#include <iostream>
#include <mutex>
#include <thread>
std::mutex m; // this program fails when m is a global variable
int main()
{
const unsigned N = 10;
// std::mutex m; // with m here everything is allright
for(unsigned i = 0; i < N; ++i)
new std::thread([&]()
{
while(true)
{
m.lock();
std::cout << "[" << std::this_thread::get_id() << "]: ";
for(unsigned i = 0; i < 10; ++i)
std::cout << i;
std::cout << std::endl;
m.unlock();
}
});
std::this_thread::sleep_for(std::chrono::seconds(1));
}