StephanTLavavej / mingw-distro

MinGW distro build scripts.
502 stars 55 forks source link

libstdc++ doesn't natively support multithreading on Windows #26

Closed adeelbaba closed 7 years ago

adeelbaba commented 7 years ago

I just downloaded the latest distribution from https://nuwen.net/mingw.html

But when I compile a simple program that uses std::mutex I get compile time errors.

g++ example4.cpp -o example4 example4.cpp:9:6: error: 'mutex' in namespace 'std' does not name a type std::mutex g_pages_mutex; ^~~~~ example4.cpp: In function 'void save_page(const string&)': example4.cpp:14:10: error: 'std::this_thread' has not been declared std::this_thread::sleep_for(std::chrono::seconds(2)); ^~~ example4.cpp:17:21: error: 'mutex' is not a member of 'std' std::lock_guard guard(g_pages_mutex); ^~~ example4.cpp:17:21: error: 'mutex' is not a member of 'std' example4.cpp:17:31: error: template argument 1 is invalid std::lock_guard guard(g_pages_mutex); ^ example4.cpp:17:39: error: 'g_pages_mutex' was not declared in this scope std::lock_guard guard(g_pages_mutex); ^~~~~ example4.cpp: In function 'int main()': example4.cpp:23:5: error: 'thread' is not a member of 'std' std::thread t1(save_page, "http://foo"); ^~~ example4.cpp:24:5: error: 'thread' is not a member of 'std' std::thread t2(save_page, "http://bar"); ^~~ example4.cpp:25:5: error: 't1' was not declared in this scope t1.join(); ^~ example4.cpp:26:5: error: 't2' was not declared in this scope t2.join(); ^~

The simple code I used is as follows:

include

include

include

include

include

include

std::map<std::string, std::string> g_pages; std::mutex g_pages_mutex;

void save_page(const std::string &url) { // simulate a long page fetch std::this_thread::sleep_for(std::chrono::seconds(2)); std::string result = "fake content";

std::lock_guard<std::mutex> guard(g_pages_mutex);
g_pages[url] = result;

}

int main() { std::thread t1(save_page, "http://foo"); std::thread t2(save_page, "http://bar"); t1.join(); t2.join();

// safe to access g_pages without lock now, as the threads are joined
for (const auto &pair : g_pages) {
    std::cout << pair.first << " => " << pair.second << '\n';
}

}

Any help would be much appreciated.

StephanTLavavej commented 7 years ago

GCC's libstdc++ doesn't have a Windows-native implementation of the multithreading headers, and I'm uninterested in building any Win32/Posix wrappers. I recommend using Boost.Thread with -DBOOST_THREAD_VERSION=4 to request Standard-conformant behavior.

StephanTLavavej commented 7 years ago

I'll reactivate this because it's an upstream issue that's causing me headaches (e.g. in Boost.Fiber and glbinding).

StephanTLavavej commented 7 years ago

mingw-w64's website says "Winpthreads has been merged into the main tarball as of 3.1.0." I may reluctantly consider figuring out how to build this.

StephanTLavavej commented 7 years ago

Although the underlying problem remains, I believe I can enable winpthreads and OpenMP in distro 15.2.