Solved

pthread blocking queue

Posted on 2013-12-07
4
744 Views
Last Modified: 2013-12-08
Hi ,


I want to create a pthread that is reading events from a blocking queue and execute them.

Any help on how to do that in pthread ?
0
Comment
Question by:bachra04
  • 2
  • 2
4 Comments
 
LVL 2

Author Comment

by:bachra04
ID: 39703960
to clarify I have a list and I want to block on that list until it gets an element :

basically blocking pop_front in pthread.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39704363
Are you thinking along the lines of the following?

template <typename T>
class EventQueue {

public:
    EventQueue() {
        pthread_mutex_init(&m_queue_lock, NULL);
        pthread_cond_init(&m_event_cond, NULL);
    }

    ~EventQueue() {
        pthread_mutex_lock(&m_queue_lock);
        m_queue.clear();
        pthread_mutex_unlock(&m_queue_lock);
    }

    void push_back(T new_event) {
        pthread_mutex_lock(&m_queue_lock);
        m_queue.push_back(new_event);
        pthread_mutex_unlock(&m_queue_lock);

        pthread_cond_signal(&m_event_cond);  // signals that there now is an element to retrieve
    }

    T pop_front() {  // will wait until there is an element to return
        T ret;
        pthread_mutex_lock(&m_queue_lock);
        while (m_queue.empty()) {
            pthread_cond_wait(&m_event_cond, &m_queue_lock);
        }
        ret = m_queue.front();
        pthread_mutex_unlock(&m_queue_lock);
        return ret;
    }

    void clear_all() {
        pthread_mutex_lock(&m_queue_lock);
        if (!m_queue.empty())
            m_queue.pop_front();
        pthread_mutex_unlock(&m_queue_lock);
    }

private:
    std::deque<T> m_queue;
    pthread_mutex_t m_queue_lock;
    pthread_cond_t m_event_cond;
};

Open in new window


(Code found on the interwebs and modified for your purpose, it used to be a thread pool)

BTW, you can of course use e.g. e 'std::list' or any other STL container that meets your needs instead of a 'dequeue'.
0
 
LVL 2

Author Comment

by:bachra04
ID: 39704390
Hi Jkr,

That's what i'm looking for. my only concern is that if pop_front is waiting undefinitely, does clear all stop that  or should I send a pthread_cond_signal(&m_event_cond)in that case ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 39704404
The problem with just signaling the condition variable is that you have nothing to return in that case. To avoid that, you could rewrite it as

    bool pop_front(T& ret) {  // will wait until there is an element to return

        pthread_mutex_lock(&m_queue_lock);
        if (m_queue.empty()) {
            pthread_cond_wait(&m_event_cond, &m_queue_lock);
        }

        if (m_queue.empty()) return false; // signalled without an addition to the container

        ret = m_queue.front();
        pthread_mutex_unlock(&m_queue_lock);

        return true;
    }

Open in new window


and evaluate to boolean return value to take that into account. And yes, you could as well set the condition variable in clear as well, depending on whether that is your desired functionality.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now