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 8990725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:33:48+00:00 2026-06-15T22:33:48+00:00

I have a three threads that I would like to serialize I am using

  • 0

I have a three threads that I would like to serialize
I am using pthreads is C++. I am trying to order the output so that it will be {A,B,C,A,B,C,A,B,C,……………}. I am doing this because I have so many threads that I would like to serialize.
the output that I would like to have is:

Thread A
Thread B
Thread C
Thread A
Thread B
Thread C
Thread A
Thread B
Thread C
Thread A
Thread B
Thread C
........
........

This is the code that I am having. It hangs sometimes and sometime run for one or two loops and then hangs. I would like to hear what you think the problem.
My code is:
thread_test.cpp

#include <stdio.h>
#include <pthread.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int condition = 0;
int count = 0;

void* thread_c( void * arg )
{
   while( 1 )
   {
      pthread_mutex_lock( &mutex );
      while( condition != 2 )
         pthread_cond_wait( &cond, &mutex );
      printf( "Thread C");
      condition = 0;
      pthread_cond_signal( &cond );
      pthread_mutex_unlock( &mutex );
   }

   return( 0 );
}

void* thread_b( void * arg )
{
   while( 1 )
   {
      pthread_mutex_lock( &mutex );
      while( condition != 1 )
         pthread_cond_wait( &cond, &mutex );
      printf( "Thread B" );
      condition = 2;
      pthread_cond_signal( &cond );
      pthread_mutex_unlock( &mutex );
   }

   return( 0 );
}

void*  thread_a( void * arg )
{
   while( 1 )
   {
      pthread_mutex_lock( &mutex );
      while( condition != 0 )
         pthread_cond_wait( &cond, &mutex );
      printf( "Thread A");
      condition = 1;
      pthread_cond_signal( &cond );      
      pthread_mutex_unlock( &mutex );
   }
   return( 0 );
}

int main( void )
{
    pthread_t  thread_a_id;
    pthread_create( &thread_a_id, NULL, &thread_a, NULL );
    pthread_t  thread_b_id;
    pthread_create( &thread_b_id, NULL, &thread_b, NULL );
    pthread_t  thread_c_id;
    pthread_create( &thread_c_id, NULL, &thread_c, NULL );
    int a = pthread_join(thread_a_id, NULL);
    int b = pthread_join(thread_b_id, NULL);
    int c = pthread_join(thread_c_id, NULL);
}

To compile the code, I use

g++ -lpthread -std=gnu++0x thread_test.cpp
  • 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. Editorial Team
    Editorial Team
    2026-06-15T22:33:50+00:00Added an answer on June 15, 2026 at 10:33 pm

    I think the issue is that pthread_cond_signal() is free to pick whichever waiting thread it wishes, whereas your code depends on it picking a specific thread.

    If I replace pthread_cond_signal() with pthread_cond_broadcast(), I can no longer get the code to stall. I am mentioning this as an observation; I am yet to convince myself that this is a correct fix.

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

Sidebar

Related Questions

I have a program with three threads. I call them like this: if __name__
There have been a couple of threads on this topic in the past that
I know that there are some threads have a similar issue with this thread.
I have a multi-threaded application that is using pthreads. I have a mutex() lock
I have a series of 'tasks' that I would like to run in separate
I have an existing java/scala application using a global thread pool. I would like
I am creating a program that will multiple background tasks simultaneously. I would like
Is there a pre-implemented linked list out there that would have been implemented using
So I have been trying to write a java program, that involves Threads. It
OS is Linux, working with pthreads I have two worker threads that run forever,

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.