简体   繁体   English

在两个生产者和一个消费者的线程安全队列中提升条件不起作用

[英]boost conditional not working on thread safe queue with two producers and one consumer

I have two threads that add to a "thread safe" queue. 我有两个线程添加到“线程安全”队列。 However when the second thread attempts to "push" contents. 但是当第二个线程试图“推送”内容时。 The consumer is not notified that contents are available. 不通知消费者内容可用。 The queue continues to grow but the notify_one() never notifies the conditional in the consuming method. 队列继续增长,但notify_one()从不在使用方法中通知条件。 Why is this? 为什么是这样?

#ifndef CONCURRENT_QUEUE_H
#define CONCURRENT_QUEUE_H

#include <queue>
#include <boost/thread.hpp>

template<typename Data>
class concurrent_queue {
private:
    std::queue<Data> the_queue;
    mutable boost::mutex the_mutex;
    boost::condition_variable the_condition_variable;
public:
    void push(Data const& data) {
        boost::mutex::scoped_lock lock(the_mutex);
        the_queue.push(data);
        lock.unlock();
        the_condition_variable.notify_one();
    }

    void wait_and_pop(Data& popped_value) {
        boost::mutex::scoped_lock lock(the_mutex);
        while(the_queue.empty()) {
            the_condition_variable.wait(lock);
        }

        popped_value=the_queue.front();
        the_queue.pop();
    }
};
#endif

This code does work in Fedora 14 using boost 1.51.0, however it does not work in boost 1.50.0 in windows 7. 这段代码在Fedora 14中使用boost 1.51.0工作,但在Windows 7的boost 1.50.0中不起作用。

INCLUDEPATH += \
    . \
    /home/mehoggan/Devel/x86-fps/boost_1_50_0/include

LDFLAGS += -Wl,-rpath=/home/mehoggan/Devel/x86-fps/boost_1_50_0/lib

LIBS += \
    -L/home/mehoggan/Devel/x86-fps/boost_1_50_0/lib \
    -lboost_system \
    -lboost_thread \
    -lz

#ifndef CONCURRENT_QUEUE_H
#define CONCURRENT_QUEUE_H

#include <queue>
#include <boost/thread.hpp> // Using boost 1.50.0

template<typename Data>
class concurrent_queue {
private:
    std::queue<Data> the_queue;
    mutable boost::mutex the_mutex;
    boost::condition_variable the_condition_variable;
public:
    void push(Data const& data) {
        boost::mutex::scoped_lock lock(the_mutex);
        the_queue.push(data);
        lock.unlock();
        the_condition_variable.notify_all();
    }

    void wait_and_pop(Data& popped_value) {
        boost::mutex::scoped_lock lock(the_mutex);
        while(the_queue.empty()) {
            the_condition_variable.wait(lock);
        }

        popped_value=the_queue.front();
        the_queue.pop();
    }
};
#endif

concurrent_queue<int> the_queue;

void thread1func() {
    do {
        the_queue.push(1);
    } while (true);
}

void thread2func() {
    do {
        the_queue.push(2);
    } while (true);
}

void thread3func() {
    do {
        int read;
        the_queue.wait_and_pop(read);

        std::cout << "I read from thread " << read << std::endl;
    } while (true);
}

int main(int argc, char *argv[]) {
    boost::thread thread1 = boost::thread(thread1func);
    boost::thread thread2 = boost::thread(thread2func);
    boost::thread thread3 = boost::thread(thread3func);

    thread1.join();
    thread2.join();
    thread3.join();
}

Your Makefile doesn't seem to use -pthread/-mthreads. 你的Makefile似乎没有使用-pthread / -mthreads。 In general don't expect your code to work at all if you are not asking for the compiler to generate multi-threaded code. 一般情况下,如果您不要求编译器生成多线程代码,请不要指望您的代码可以正常工作。

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

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