You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add thread_local only when CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS is defined #3044
Our use case is maybe a little uncommon: we are building Catch2 as a DLL (using a local patch that adds the necessary __declspec(dllexport) annotations). On Windows, thread_local is incompatible with building DLLs, and results in error C2492 ("data with thread storage duration may not have dll interface").
Since we are already patching Catch, it's easy to add another patch that removes the thread_local again, and because we are not opting in to CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS, this seems like a safe change. But it would be nice if we didn't have to do that. I'm thinking of a change similar to this (obviously incomplete):
diff --git a/src/catch2/internal/catch_message_info.cpp b/src/catch2/internal/catch_message_info.cpp
index bc887abf..732c36e8 100644
--- a/src/catch2/internal/catch_message_info.cpp+++ b/src/catch2/internal/catch_message_info.cpp@@ -21,6 +21,6 @@ namespace Catch {
// Messages are owned by their individual threads, so the counter should be thread-local as well.
// Alternative consideration: atomic, so threads don't share IDs and things are easier to debug.
- thread_local unsigned int MessageInfo::globalCount = 0;+ CATCH_THREAD_LOCAL unsigned int MessageInfo::globalCount = 0;
} // end namespace Catch
diff --git a/src/catch2/internal/catch_message_info.hpp b/src/catch2/internal/catch_message_info.hpp
index 7597f80d..bb8b2b81 100644
--- a/src/catch2/internal/catch_message_info.hpp+++ b/src/catch2/internal/catch_message_info.hpp@@ -38,7 +38,7 @@ namespace Catch {
return sequence < other.sequence;
}
private:
- static thread_local unsigned int globalCount;+ static CATCH_THREAD_LOCAL unsigned int globalCount;
};
} // end namespace Catch
diff --git a/src/catch2/internal/catch_thread_support.hpp b/src/catch2/internal/catch_thread_support.hpp
index c15980b7..beec711c 100644
--- a/src/catch2/internal/catch_thread_support.hpp+++ b/src/catch2/internal/catch_thread_support.hpp@@ -13,6 +13,10 @@
#if defined( CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS )
# include <atomic>
# include <mutex>
++# define CATCH_THREAD_LOCAL thread_local+#else+# define CATCH_THREAD_LOCAL
#endif
#include <catch2/catch_totals.hpp>
Cool. Would you prefer to do the change yourself, or do you want me to make a PR? I'd prefer the former, because you probably know better what to watch out for, but I could give it a try if you prefer.
On Windows, thread_local is incompatible with building DLLs
I was a little surprised to read this, since I have done it before without problems. I don't know if you left out the details for simplicity, but the actual incompatibility is that a thread_local variable cannot be exported as part of the DLL interface. It's perfectly fine to use thread_local variables otherwise (source).
@cschreib Yes, I know that, and sorry that the issue description may be misleading. But as the description says, we are patching Catch2 to add __declspec(dllexport) where needed, and one such place where we add it is the MessageInfo class. And that class contains a thread_local, hence the problem.
It's not a problem for "ordinary" users (who don't patch Catch2), so it's not an urgent issue to fix.
Activity
horenmar commentedon Nov 6, 2025
Sure, why not.
stk-ableton commentedon Nov 6, 2025
Cool. Would you prefer to do the change yourself, or do you want me to make a PR? I'd prefer the former, because you probably know better what to watch out for, but I could give it a try if you prefer.
horenmar commentedon Nov 7, 2025
I can get to it eventually. If you want it done faster, you have to open a PR 😃
stk-ableton commentedon Nov 7, 2025
Awesome. We're not in a hurry, our patch works. 😄
cschreib commentedon Nov 16, 2025
I was a little surprised to read this, since I have done it before without problems. I don't know if you left out the details for simplicity, but the actual incompatibility is that a
thread_localvariable cannot be exported as part of the DLL interface. It's perfectly fine to usethread_localvariables otherwise (source).stk-ableton commentedon Nov 16, 2025
@cschreib Yes, I know that, and sorry that the issue description may be misleading. But as the description says, we are patching Catch2 to add
__declspec(dllexport)where needed, and one such place where we add it is theMessageInfoclass. And that class contains athread_local, hence the problem.It's not a problem for "ordinary" users (who don't patch Catch2), so it's not an urgent issue to fix.
Only use thread_local in builds with thread safety enabled