互斥锁会在 mingw 中停止程序,但不会在 linux 中停止
Mutex lock halts program in mingw but not on linux
我正在尝试使用 mingw-w64 将原型移植到 windows,但我对它如何处理线程感到困惑。以下代码在我锁定互斥量后停止。
#include <iostream>
#include <mutex>
int main() {
std::mutex mu;
std::unique_lock<std::mutex> lock(mu);
std::cout<<"starting"<<std::endl;
mu.lock();
std::cout<<"end"<<std::endl;
return 0;
}
输出应该是
starting
end
但它只打印开始然后挂起。
我在 linux 上尝试了相同的程序,它工作正常。
如果有帮助,这是我的 g++ 版本
Using built-in specs.
COLLECT_GCC=C:\msys64\mingw64\bin\g++.exe
COLLECT_LTO_WRAPPER=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.2.1/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-8-20181214/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-bootstrap --with-arch=x86-64 --with-tune=generic --enable-languages=ada,c,lto,c++,objc,obj-c++,fortran --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts=yes --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --disable-isl-version-check --enable-lto --enable-libgomp --disable-multilib --enable-checking=release --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev1, Built by MSYS2 project' --with-bugurl=https://sourceforge.net/projects/msys2 --with-gnu-as --with-gnu-ld
Thread model: posix
gcc version 8.2.1 20181214 (Rev1, Built by MSYS2 project)
您无法锁定已锁定的互斥量。它产生一个具有未定义行为的解除锁。
http://www.cplusplus.com/reference/mutex/mutex/lock/
If the mutex is currently locked by the same thread calling this function, it produces a deadlock (with undefined behavior). See recursive_mutex for a mutex type that allows multiple locks from the same thread.
为了能够多次锁定互斥量,您应该使用 std::recursive_mutex
而不是 std::mutex
。
另请注意,您应该将每个 lock()
调用与 unlock()
调用配对。
我正在尝试使用 mingw-w64 将原型移植到 windows,但我对它如何处理线程感到困惑。以下代码在我锁定互斥量后停止。
#include <iostream>
#include <mutex>
int main() {
std::mutex mu;
std::unique_lock<std::mutex> lock(mu);
std::cout<<"starting"<<std::endl;
mu.lock();
std::cout<<"end"<<std::endl;
return 0;
}
输出应该是
starting
end
但它只打印开始然后挂起。
我在 linux 上尝试了相同的程序,它工作正常。
如果有帮助,这是我的 g++ 版本
Using built-in specs.
COLLECT_GCC=C:\msys64\mingw64\bin\g++.exe
COLLECT_LTO_WRAPPER=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.2.1/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-8-20181214/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-bootstrap --with-arch=x86-64 --with-tune=generic --enable-languages=ada,c,lto,c++,objc,obj-c++,fortran --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts=yes --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --disable-isl-version-check --enable-lto --enable-libgomp --disable-multilib --enable-checking=release --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev1, Built by MSYS2 project' --with-bugurl=https://sourceforge.net/projects/msys2 --with-gnu-as --with-gnu-ld
Thread model: posix
gcc version 8.2.1 20181214 (Rev1, Built by MSYS2 project)
您无法锁定已锁定的互斥量。它产生一个具有未定义行为的解除锁。
http://www.cplusplus.com/reference/mutex/mutex/lock/
If the mutex is currently locked by the same thread calling this function, it produces a deadlock (with undefined behavior). See recursive_mutex for a mutex type that allows multiple locks from the same thread.
为了能够多次锁定互斥量,您应该使用 std::recursive_mutex
而不是 std::mutex
。
另请注意,您应该将每个 lock()
调用与 unlock()
调用配对。