Repository navigation
TutorialIntermediate
Dmitry Koplyarov edited this page Feb 10, 2016
·
14 revisions
- Connecting from another thread
- Guarding an external state with a signal mutex
- The external state and the populators
The default specialization of signal template is thread-safe.
You may invoke a signal and connect to it from different threads without any problems if you have no external state to guard.
class Timer
{
std::atomic<bool> _alive;
std::thread _thread;
public:
wigwag::signal<void()> OnTick;
Timer()
: _alive(true), _thread(&Timer::ThreadFunc, this)
{ }
~Timer()
{
_alive = false;
_thread.join();
}
private:
void ThreadFunc()
{
while (_alive)
{
std::this_thread::sleep(std::chrono::seconds(1));
OnTick();
}
}
};
// ...
{
Timer timer;
wigwag::token t = timer.OnTick.connect([] { std::cout << "tick" << std::endl; });
std::this_thread::sleep(std::chrono::milliseconds(3500));
}Output:
tick
tick
tick
However, usually you do have a state associated with a signal.
In such a case, if you want to modify the state and invoke the signal atomically, you should use the signal::lock_primitive() getter.
In this sample code, the signal internal mutex is locked when the associated state is modified and the signal is invoked and also when the associated state is obtained.
class Timer
{
std::atomic<bool> _alive;
int _tickCount;
std::thread _thread;
public:
wigwag::signal<void(int)> OnTick;
Timer()
: _alive(true), _thread(&Timer::ThreadFunc, this)
{ }
~Timer()
{
_alive = false;
_thread.join();
}
int GetTickCount() const
{
std::lock_guard<std::recursive_mutex> l(OnTick.lock_primitive());
return _tickCount;
}
private:
void ThreadFunc()
{
while (_alive)
{
std::this_thread::sleep(std::chrono::seconds(1));
std::lock_guard<std::recursive_mutex> l(OnTick.lock_primitive());
++_tickCount;
OnTick(_tickCount);
}
}
};
// ...
{
Timer timer;
std::cout << "ticks before connect: " << timer.GetTickCount() << std::end;
wigwag::token t = timer.OnTick.connect([] (int n) { std::cout << "tick " << n << std::endl; });
std::this_thread::sleep(std::chrono::milliseconds(3500));
}Output:
ticks before connect: 0
tick 1
tick 2
tick 3
class Timer
{
std::atomic<bool> _alive;
int _tickCount;
std::thread _thread;
public:
wigwag::signal<void(int)> OnTick;
Timer()
: _alive(true), _thread(&Timer::ThreadFunc, this),
OnTick(std::bind(&Timer::OnTickPopulator, this, std::placeholders::_1))
{ }
~Timer()
{
_alive = false;
_thread.join();
}
private:
// This function is invoked when a handler is being connected to a signal
void OnTickPopulator(const std::function<void(int)>& handler) const
{
// The signal mutex is already locked here!
handler(_tickCount);
}
void ThreadFunc()
{
while (_alive)
{
std::this_thread::sleep(std::chrono::seconds(1));
std::lock_guard<std::recursive_mutex> l(OnTick.lock_primitive());
++_tickCount;
OnTick(_tickCount);
}
}
};
// ...
{
Timer timer;
wigwag::token t = timer.OnTick.connect([] (int n) { std::cout << "tick " << n << std::endl; });
std::this_thread::sleep(std::chrono::milliseconds(3500));
}Output:
tick 0
tick 1
tick 2
tick 3
You may use a labmda function as a populator:
Timer()
: _alive(true), _thread(&Timer::ThreadFunc, this),
OnTick([&](const std::function<void(int)>& h) { h(_tick); })
{ }Or a lambda function with auto argument if you use C++14:
Timer()
: _alive(true), _thread(&Timer::ThreadFunc, this),
OnTick([&](auto&& h) { h(_tick); })
{ }