I try to write unit tests or my Visual C++ Win64 native static library. I'm using VC++ 2017.
The tests fail, when I create additional threads in my test and use complex thread_local variables, like in this example:
In my lib:
#include <unordered_map>
#include<string>
class TlsObject
{
public:
static void clear_map() {
mymap.clear();
}
private:
thread_local static std::unordered_map<std::string, std::string> mymap;
};
...
thread_local std::unordered_map<std::string, std::string> TlsObject::mymap;
Here is the unit test code:
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../TLSUnittest/TlsObject.h"
#include <future>
#include <string>
#include <unordered_map>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace SomeUnittest
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1) {
TlsObject::clear_map(); // works fine here!!
auto future = std::async(std::launch::async, []() {
TlsObject::clear_map(); // throws exception here!!
return 0; });
future.wait();
}
};
}
It seems as if the constructor of the std::unordered_map was not called before entering the lamda code. Therefore my code produces a segmentation fault. This finally causes the unit test to fail.
The same code as in the unit test works fine when I link it to a console .exe.
What could be the reason for the unit test failing, while the .exe has no problem?
Aucun commentaire:
Enregistrer un commentaire