Solved

pthread blocking queue

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

636 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