Last active
May 2, 2018 21:19
-
-
Save jamesob/fe9a872051a88b2025b1aa37bfa98605 to your computer and use it in GitHub Desktop.
stack overflow with thread_local/mutex primitives usage on win32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vagrant@vagrant-ubuntu-trusty-64:~/bitcoin$ x86_64-w64-mingw32-g++ --static -std=c++11 fuuuuu.cpp | |
vagrant@vagrant-ubuntu-trusty-64:~/bitcoin$ ./a.exe | |
err:seh:setup_exception stack overflow 2976 bytes in thread 0055 eip 00007f68d258a8c1 esp 0000000000740a60 stack 0x740000-0x742000-0x940000 | |
^Cerr:ntdll:RtlpWaitForCriticalSection section 0x100a8 "heap.c: main process heap section" wait timed out in thread 005b, blocked by 0055, retrying (60 sec) | |
err:ntdll:RtlpWaitForCriticalSection section 0x100a8 "heap.c: main process heap section" wait timed out in thread 0064, blocked by 0055, retrying (60 sec) | |
err:ntdll:RtlpWaitForCriticalSection section 0x100a8 "heap.c: main process heap section" wait timed out in thread 0096, blocked by 0055, retrying (60 sec) | |
err:ntdll:RtlpWaitForCriticalSection section 0x7f68d27e1e20 "loader.c: loader_section" wait timed out in thread 002b, blocked by 0064, retrying (60 sec) | |
^C^C^C^Cerr:ntdll:RtlpWaitForCriticalSection section 0x7f68d27e1e20 "loader.c: loader_section" wait timed out in thread 005d, blocked by 0064, retrying (60 sec) | |
err:ntdll:RtlpWaitForCriticalSection section 0x7f68d27e1e20 "loader.c: loader_section" wait timed out in thread 0039, blocked by 0064, retrying (60 sec) | |
err:ntdll:RtlpWaitForCriticalSection section 0x7f68d27e1e20 "loader.c: loader_section" wait timed out in thread 0009, blocked by 0064, retrying (60 sec) | |
err:ntdll:RtlpWaitForCriticalSection section 0x7f68d27e1e20 "loader.c: loader_section" wait timed out in thread 003d, blocked by 0064, retrying (60 sec) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ diff works.cpp fuuuuu.cpp | |
15c15 | |
< static std::string this_id = std::to_string(uid++); | |
--- | |
> static thread_local std::string this_id = std::to_string(uid++); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <map> | |
#include <thread> | |
#include <iostream> | |
#include <string> | |
#include <atomic> | |
#include <iostream> | |
class Registry | |
{ | |
public: | |
void DoThing() { | |
static std::atomic<int> uid{0}; | |
static thread_local std::string this_id = std::to_string(uid++); | |
themap[this_id]++; | |
} | |
private: | |
std::map<std::string, int> themap; | |
}; | |
int main() | |
{ | |
std::vector<std::thread> threads; | |
Registry reg; | |
auto ThreadFunc = [&]() { | |
reg.DoThing(); | |
}; | |
for (int i = 0; i < 100; ++i) { | |
threads.push_back(std::thread(ThreadFunc)); | |
} | |
for (std::thread& t : threads) t.join(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <map> | |
#include <thread> | |
#include <iostream> | |
#include <string> | |
#include <atomic> | |
#include <iostream> | |
class Registry | |
{ | |
public: | |
void DoThing() { | |
static std::atomic<int> uid{0}; | |
// | |
// MISSING thread_local HERE | |
// | |
static std::string this_id = std::to_string(uid++); | |
themap[this_id]++; | |
} | |
private: | |
std::map<std::string, int> themap; | |
}; | |
int main() | |
{ | |
std::vector<std::thread> threads; | |
Registry reg; | |
auto ThreadFunc = [&]() { | |
reg.DoThing(); | |
}; | |
for (int i = 0; i < 100; ++i) { | |
threads.push_back(std::thread(ThreadFunc)); | |
} | |
for (std::thread& t : threads) t.join(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment