Add Thread Guard for debugging simple threading issues (#4254)
* Add ThreadGuard class * Use ThreadGuard when accessing a ChannelView's messageSnapshot
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <thread>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
// Debug-class which asserts if guard of the same object has been called from different threads
|
||||
struct ThreadGuard {
|
||||
#ifndef NDEBUG
|
||||
std::mutex mutex;
|
||||
std::optional<std::thread::id> threadID;
|
||||
#endif
|
||||
|
||||
inline void guard()
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::unique_lock lock(this->mutex);
|
||||
|
||||
auto currentThreadID = std::this_thread::get_id();
|
||||
|
||||
if (!this->threadID.has_value())
|
||||
{
|
||||
this->threadID = currentThreadID;
|
||||
return;
|
||||
}
|
||||
|
||||
assert(this->threadID == currentThreadID);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
Reference in New Issue
Block a user