Solved

pthread blocking queue

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 75
convert char array to number in c 5 85
White board coding practice 3 86
Test the speeds on my PC Drives 12 43
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

786 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