我正在Windows上安装mingw-w64,有两个选项:win32线程和posix线程.我知道win32线程和pthreads之间的区别是什么,但我不明白这两个选项之间的区别.我怀疑如果我选择posix线程,它将阻止我调用WinTI函数,如CreateThread.
看来这个选项指定某些程序或库将使用哪个线程API,但是通过什么?通过GCC,libstdc ++还是别的什么?
我发现了这个: 在windows的gcc端口中thread_posixs和thread_win32之间的区别是什么?
简而言之,对于这个版本的mingw,threads-posix版本将使用posix API并允许使用std :: thread,而threads-win32将使用win32 API,并禁用std :: thread部分标准.
好的,如果我将选择win32线程,那么std :: thread将不可用,但仍将使用win32线程.但用什么?
我正在编写一个简单的C++程序来演示锁的使用.我正在使用codeblocks和gnu gcc编译器.
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
int x = 0; // shared variable
void synchronized_procedure()
{
static std::mutex m;
m.lock();
x = x + 1;
if (x < 5)
{
cout<<"hello";
}
m.unlock();
}
int main()
{
synchronized_procedure();
x=x+2;
cout<<"x is"<<x;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:mutex in namespace std does not name a type.
为什么我收到此错误?编译器是否支持使用锁?
在尝试编译以下代码时
#include <thread>
#include <iostream>
void foo() { std::cout << "foo\n"; }
int main()
{
std::thread t(foo);
t.join();
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
C:\Test>g++ -g -Wall -lpthread -std=c++0x
main.cpp
main.cpp: In function 'int main()':
main.cpp:12:2: error: 'thread' is not a member of 'std'
main.cpp:12:14: error: expected ';' before 't'
main.cpp:13:2: error: 't' has not been declared
Run Code Online (Sandbox Code Playgroud)
如何使用C++ 11实验并发功能?我有MinGW GCC 4.5.1(TDM)
编辑: BTW,Visual Studio 2012执行良好的代码示例.