简体   繁体   English

boost :: thread生产者使用者

[英]boost::thread producer consumer

I am new to boost::thread I am making a producer consumer with a Monitor. 我刚开始使用boost::thread我正在使用Monitor使生产者成为消费者。 This is how I've coded it so far. 到目前为止,这就是我编码的方式。

//{ Declarations in header
private:
  boost::condition_variable    _condition;
  boost::mutex                 _mutex;
  std::deque<RawMatrix*>       _queue;
  boost::detail::atomic_count  _count;
//}

void MatrixMonitor::deposit(RawMatrix* rawMatrix){
    boost::unique_lock<boost::mutex> lock(_mutex);
    _condition.wait(lock, boost::bind(std::less_equal<int>(), boost::ref(_count), max));
    _queue.push_back(rawMatrix);
    ++_count;
    _condition.notify_one();
}

RawMatrix* MatrixMonitor::withdraw(){
    boost::unique_lock<boost::mutex> lock(_mutex);
    _condition.wait(lock, boost::bind(std::greater_equal<int>(), boost::ref(_count), min));
    RawMatrix* elem = _queue.front();
    _queue.pop_front();
    --_count;
    _condition.notify_one();
    return elem;
}

Is that okay ? 这样可以吗 ? and one thing I can not understand is how would I design the Producer and Consumer now ? 我不明白的一件事是,我现在将如何设计生产者和消费者? So far I've done 到目前为止,我已经完成了

void MatrixProducer::produce(){
    boost::mutex::scoped_lock lock(_mutex);
    RawMatrix* matrix = rawMatrix();
    _monitor->deposit(matrix);
}
RawMatrix* MatrixProducer::rawMatrix(){/*Generates and returns a matrix*/}

But how can/should I run the produce() in some interval. 但是,如何/应该在一定间隔内运行produce() and I don't know What I need to write in consumer. 而且我不知道我该如何写给消费者。 and who will have the ownership of this Producer, Consumer and Monitor ? 谁将拥有此生产者,消费者和监控者的所有权?

Is that okay ? 这样可以吗 ?

  1. You shouldn't use one condition variable for two distinct predicates. 您不应将一个条件变量用于两个不同的谓词。 Use one condition variable for the queue-full condition and one for the queue-empty condition, or you will end up with missing updates. 对队列已满条件使用一个条件变量,对队列空条件使用一个条件变量,否则最终将缺少更新。

  2. In your produce() function, you shouldn't lock a second mutex, if this is not necessary. 在您的Produce()函数中,如果不需要,则不应锁定第二个互斥锁。 If it's a necessary predicate to call rawMatrix(), you could at least release the mutex before calling deposit() to not lock two mutex. 如果调用rawMatrix()是必要的谓词,则可以至少在调用deposit()之前释放互斥锁以不锁定两个互斥锁。 Every time you lock more than one mutex, you must be aware of possible dead locks. 每次锁定多个互斥锁时,必须注意可能的死锁。 One way to avoid deadlocks is to lock mutex always in the very same order (a so called lock hierarchy). 避免死锁的一种方法是始终以相同的顺序锁定互斥锁(即所谓的锁定层次结构)。

how would I design the Producer and Consumer now ? 我现在将如何设计生产者和消费者?

Designing your producer and consumer is up to you and depends highly on your requirements. 设计生产者和消费者取决于您,并且高度取决于您的要求。 A producer / consumer schema is used to decouple the producing of work load from the actual processing. 生产者/使用者方案用于将工作负载的产生与实际处理分离。 It's a buffer for work. 这是工作的缓冲。

who will have the ownership of this Producer, Consumer and Monitor ? 谁将拥有此生产者,消费者和监控者的所有权?

Depending on your design, it might make sense, that a Producer owns the queue and the queue owns the consumers. 根据您的设计,可能有意义的是,生产者拥有队列,而队列拥有使用者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM