这是我尝试实现一个C++事件。什么是同步这个事件实现的最佳方法
class Event{
typedef std::tr1::function<void(int&)> CallbackFunction;
std::list<CallbackFunction> m_handlers;
template<class M>
void AddHandler(M& thisPtr, void typename (M::*callback)(int&))
{
CallbackFunction bound = std::tr1::bind(callback, &thisPtr, _1);
m_handlers.push_back(bound);
}
void operator()(int& eventArg)
{
iterate over list...
(*iter)(eventArg);
}}
这里的麻烦是线程安全。如果AddHandler
和operator()
被调用的同时事情可能会中断。
什么是最好的同步方式?使用互斥锁可能会导致性能下降。我想知道在这种情况下boost :: signals或C#事件幕后会发生什么。
他们每秒打电话多久?为什么这个相同的对象需要对所有线程都可用?难道他们不能只发送一个线程接收到的互斥信息吗? – 2012-01-12 00:02:34