Skip to content

Instantly share code, notes, and snippets.

@jamesob
Last active May 2, 2018 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesob/fe9a872051a88b2025b1aa37bfa98605 to your computer and use it in GitHub Desktop.
Save jamesob/fe9a872051a88b2025b1aa37bfa98605 to your computer and use it in GitHub Desktop.
stack overflow with thread_local/mutex primitives usage on win32
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)
$ 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++);
#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();
}
#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