File tree 2 files changed +24
-4
lines changed
2 files changed +24
-4
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,12 @@ std::map<std::thread::id, std::string> threadId2NameMap_;
38
38
39
39
} // unnamed namespace
40
40
41
+
42
+ std::mutex& NamedThread::getMutex ()
43
+ {
44
+ return mutex_;
45
+ }
46
+
41
47
NamedThread::NamedThread (const std::string& name)
42
48
: name_(name)
43
49
{
Original file line number Diff line number Diff line change 20
20
#ifndef OPENZIM_LIBZIM_NAMEDTHREAD_H
21
21
#define OPENZIM_LIBZIM_NAMEDTHREAD_H
22
22
23
+ #include < mutex>
23
24
#include < string>
24
25
#include < thread>
25
26
@@ -34,11 +35,18 @@ class LIBZIM_PRIVATE_API NamedThread
34
35
explicit NamedThread (const std::string& name);
35
36
36
37
public:
37
- template <class F , class ... Args >
38
- NamedThread (const std::string& name, F&& f, Args&&... args )
38
+ template <class F >
39
+ NamedThread (const std::string& name, F&& f)
39
40
: NamedThread(name)
40
41
{
41
- thread_ = std::thread (std::forward<F>(f), std::forward<Args>(args)...);
42
+ // Ensure that f starts executing after the assignment to
43
+ // the thread_ data member has completed (so that any possible
44
+ // calls to NamedThread::getCurrentThreadName() from inside f()
45
+ // read the correct value of thread id).
46
+ std::mutex& mutex = getMutex ();
47
+ std::lock_guard<std::mutex> lock (mutex);
48
+
49
+ thread_ = std::thread ([f, &mutex]() { mutex.lock (); mutex.unlock (); f (); });
42
50
}
43
51
44
52
~NamedThread ();
@@ -50,7 +58,13 @@ class LIBZIM_PRIVATE_API NamedThread
50
58
51
59
static std::string getCurrentThreadName ();
52
60
53
- private:
61
+ private: // functions
62
+ // This is a workaround for a bug in our build system that prevents
63
+ // LIBZIM_PRIVATE_API and/or LIBZIM_API classes from having static data
64
+ // members
65
+ static std::mutex& getMutex ();
66
+
67
+ private: // data
54
68
const std::string name_;
55
69
std::thread thread_;
56
70
};
You can’t perform that action at this time.
0 commit comments