Solved

pthread blocking queue

Posted on 2013-12-07
4
796 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

740 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