什么是升压:障碍,如何使用这种方法提升的。 你能给我一个明确的例子,因为我发现下面的例子:
bool wait()
{
boost::mutex::scoped_lock lock(m_mutex);
unsigned int gen = m_generation;
if (--m_count == 0)
{
m_generation++;
m_count = m_threshold;
m_cond.notify_all();
return true;
}
while (gen == m_generation)
m_cond.wait(lock);
return false;
}
另外,在上述的代码:m_cond.notify_all();是进入其他等待线程? 你能告诉我清楚地约屏障功能? 谢谢。
notify_all,通知等待线程。
阻挡是一个简单的概念。 也称为集合,它是多个线程之间的同步点。 该屏障被配置为线程特定数量(N),并作为线程到达屏障,他们必须等待,直到所有n个线程已经到达。 一旦第n个线程已经到达屏障,所有等待的线程可以继续,并且阻挡复位。
简单的例子。 的电流的值时将3个线程调用上屏障等待功能仅outputed。
#include <boost/thread.hpp>
#include <boost/thread/barrier.hpp>
#include <boost/bind.hpp>
#include <boost/atomic.hpp>
boost::mutex io_mutex;
void thread_fun(boost::barrier& cur_barier, boost::atomic<int>& current)
{
++current;
cur_barier.wait();
boost::lock_guard<boost::mutex> locker(io_mutex);
std::cout << current << std::endl;
}
int main()
{
boost::barrier bar(3);
boost::atomic<int> current(0);
boost::thread thr1(boost::bind(&thread_fun, boost::ref(bar), boost::ref(current)));
boost::thread thr2(boost::bind(&thread_fun, boost::ref(bar), boost::ref(current)));
boost::thread thr3(boost::bind(&thread_fun, boost::ref(bar), boost::ref(current)));
thr1.join();
thr2.join();
thr3.join();
}