Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 139359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:25:14+00:00 2026-05-11T07:25:14+00:00

I’m trying to write a thread-safe queue using pthreads in c++. My program works

  • 0

I’m trying to write a thread-safe queue using pthreads in c++. My program works 93% of the time. The other 7% of the time it other spits out garbage, OR seems to fall asleep. I’m wondering if there is some flaw in my queue where a context-switch would break it?

// thread-safe queue // inspired by http://msmvps.com/blogs/vandooren/archive/2007/01/05/creating-a-thread-safe-producer-consumer-queue-in-c-without-using-locks.aspx // only works with one producer and one consumer #include <pthread.h> #include <exception>  template<class T> class tsqueue {     private:         volatile int m_ReadIndex, m_WriteIndex;         volatile T *m_Data;         volatile bool m_Done;         const int m_Size;         pthread_mutex_t m_ReadMutex, m_WriteMutex;         pthread_cond_t m_ReadCond, m_WriteCond;     public:         tsqueue(const int &size);         ~tsqueue();         void push(const T &elem);         T pop();         void terminate();         bool isDone() const; };  template <class T> tsqueue<T>::tsqueue(const int &size) : m_ReadIndex(0), m_WriteIndex(0), m_Size(size), m_Done(false) {     m_Data = new T[size];     pthread_mutex_init(&m_ReadMutex, NULL);     pthread_mutex_init(&m_WriteMutex, NULL);     pthread_cond_init(&m_WriteCond, NULL);     pthread_cond_init(&m_WriteCond, NULL); }  template <class T> tsqueue<T>::~tsqueue() {     delete[] m_Data;     pthread_mutex_destroy(&m_ReadMutex);     pthread_mutex_destroy(&m_WriteMutex);     pthread_cond_destroy(&m_ReadCond);     pthread_cond_destroy(&m_WriteCond); }   template <class T> void tsqueue<T>::push(const T &elem) {     int next = (m_WriteIndex + 1) % m_Size;     if(next == m_ReadIndex) {         pthread_mutex_lock(&m_WriteMutex);         pthread_cond_wait(&m_WriteCond, &m_WriteMutex);         pthread_mutex_unlock(&m_WriteMutex);     }     m_Data[m_WriteIndex] = elem;     m_WriteIndex = next;     pthread_cond_signal(&m_ReadCond); }  template <class T> T tsqueue<T>::pop() {     if(m_ReadIndex == m_WriteIndex) {         pthread_mutex_lock(&m_ReadMutex);         pthread_cond_wait(&m_ReadCond, &m_ReadMutex);         pthread_mutex_unlock(&m_ReadMutex);         if(m_Done && m_ReadIndex == m_WriteIndex) throw 'queue empty and terminated';     }     int next = (m_ReadIndex +1) % m_Size;     T elem = m_Data[m_ReadIndex];     m_ReadIndex = next;     pthread_cond_signal(&m_WriteCond);     return elem; }  template <class T> void tsqueue<T>::terminate() {     m_Done = true;     pthread_cond_signal(&m_ReadCond); }  template <class T> bool tsqueue<T>::isDone() const {     return (m_Done && m_ReadIndex == m_WriteIndex); } 

This could be used like this:

// thread 1 while(cin.get(c)) {     queue1.push(c); } queue1.terminate();   // thread 2 while(!queue1.isDone()) {     try{ c = queue1.pop(); }     catch(char const* str){break;}     cout.put(c); } 

If anyone sees a problem with this, please say so 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:25:14+00:00Added an answer on May 11, 2026 at 7:25 am

    If you want anything with decent performance I would strongly suggest dumping your R/W lock and just use a very simple spinlock. Or if you really think you can get the performance you want with R/W lock, i would roll your own based on this design(single word R/W Spinlock) from Joe Duffy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The first time an IsInitiating=true operation is called through a… May 11, 2026 at 2:48 pm
  • added an answer You're correct: it has to do with how long it… May 11, 2026 at 2:48 pm
  • added an answer Nehe have a port of most of their tutorials to… May 11, 2026 at 2:48 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.