Solved

pthread blocking queue

Posted on 2013-12-07
4
751 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

920 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

15 Experts available now in Live!

Get 1:1 Help Now