jeudi 1 janvier 2015

Can clang's std::string be made thread aware

While working with clang's thread sanitizer we noticed that std::string's copy-on-write technique isn't thread safe. We reduced the bug we were seeing to this code:



void test3() {
std::unique_ptr<std::thread> thread;

{
auto output = make_shared<string>();
std::string str = "test";
thread.reset(new std::thread([str, output]() { *output += str; }));
// The str string now goes out of scope but due to COW
// the captured string may not have the copy of the content yet.
}

thread->join();
}


When compiled with thread sanitizer enabled:



clang++ -stdlib=libc++ -std=c++11 -O0 -g -fsanitize=thread -lpthread -o test main.cpp


or



clang++ -std=c++11 -O0 -g -fsanitize=thread -lpthread -o test main.cpp


And when run multiple times, it eventually produces this warning:



WARNING: ThreadSanitizer: data race (pid=30829)
Write of size 8 at 0x7d0c0000bef8 by thread T62:
#0 operator delete(void*) <null>:0
...

Previous write of size 1 at 0x7d0c0000befd by thread T5:
#0 std::__1::char_traits<char>::assign(char&, char const&) string:639
...


Can this be work arounded without changing the code (e.g. by passing some flags to the compiler) or is this a know bug?


Thanks for looking.


Aucun commentaire:

Enregistrer un commentaire