Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
Public Member Functions | List of all members
pw::ThreadChecker Class Reference

Public Member Functions

 ThreadChecker (Thread::id id)
 
void lock ()
 
void unlock ()
 

Detailed Description

A BaseLockable class that stores the id of a thread and verifies that all lock() calls happen on that thread if PW_THREAD_CHECKER_RUNTIME_ASSERT_ENABLED config is set.

Its purpose is to provide a check that data that is meant to only be accessed from a single thread is always accessed from that thread. This is useful on data that isn't synchronized using regular sync primitives. For example, this could be used on data that is always used on an async dispatcher to ensure all data access happens on that dispatcher thread.

In addition to providing an optional runtime check, this class can be used with static thread safety analysis to ensure that resources are accessed in a context that is checked.

class MyClass {
public:
MyClass() : thread_checker_(pw::this_thread::get_id()) {}
void Foo() {
std::lock_guard checker(thread_checker_);
resource_ = 0;
}
private:
pw::ThreadChecker thread_checker_;
int resource_ PW_GUARDED_BY(thread_checker_);
};
Definition: checker.h:51
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27

The documentation for this class was generated from the following file: