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

  • SEARCH
  • Home
  • 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 8884543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:05:07+00:00 2026-06-14T21:05:07+00:00

I am seeing unexpected behavior for mutexes/conditions in C POSIX threads depending on whether

  • 0

I am seeing unexpected behavior for mutexes/conditions in C POSIX threads depending on whether the mutex and condition variables are set in the global scope (which works) on in a struct (which sometimes works).

I programming on a Mac and then running the same code on a Linux machine. I copied the code from this example, which works as expected in both machines: http://publib.boulder.ibm.com/infocenter/iseries/v7r1m0/index.jsp?topic=%2Fapis%2Fusers_73.htm
This example has the pthread_mutex_t and pthread_cond_t in the global scope:

pthread_cond_t      cond  = PTHREAD_COND_INITIALIZER;
pthread_mutex_t     mutex = PTHREAD_MUTEX_INITIALIZER;
...
pthread_mutex_lock(&mutex);
...

However if I change this to store the cond and mutex in a struct, it works on the Mac but does not work on Linux.

Here is an overview of the changes I made:

typedef struct _test_data_t {
  pthread_mutex_t cond;
  pthread_cond_t mutex;
} test_data_t;
...
pthread_mutex_lock(&(test_data->mutex));
...

Here is the output I get on Mac (which works)

Create 5 threads
Thread blocked
Thread blocked
Thread blocked
Thread blocked
Thread blocked
Wake up all waiting threads...
Wait for threads and cleanup
Main completed

Here is the output on Linux (which does not work)

Create 5 threads
Thread blocked // Hangs here forever, other threads can't lock mutex

Does anyone know why this might be happening? I will admit that I am not a C expert so I don’t know what could have happened in the switch from using a global variable to a struct variable.

Thanks in advance for your help.


Here is the code (with some error checking stripped out for brevity):

typedef struct _test_data_t {
  int conditionMet;
  pthread_mutex_t cond;
  pthread_cond_t mutex;
} test_data_t;

void *threadfunc(void *parm)
{
  int           rc;
  test_data_t *test_data = (test_data_t *) parm;

  rc = pthread_mutex_lock((pthread_mutex_t *)&(test_data->mutex));

  while (!test_data->conditionMet) {
    printf("Thread blocked\n");
    rc = pthread_cond_wait(&test_data->cond, &test_data->mutex);
  }

  rc = pthread_mutex_unlock(&test_data->mutex);
  return NULL;
}

void runThreadTest() {

  int NTHREADS = 5;

  int                   rc=0;
  int                   i;

  // Initialize mutex/condition.
  test_data_t test_data;
  test_data.conditionMet = 0;
  rc = pthread_mutex_init(&test_data.mutex, NULL);
  rc = pthread_cond_init(&test_data.cond, NULL);

  // Create threads.
  pthread_t             threadid[NTHREADS];

  printf("Create %d threads\n", NTHREADS);
  for(i=0; i<NTHREADS; ++i) {
    rc = pthread_create(&threadid[i], NULL, threadfunc, &test_data);
  }

  sleep(5);
  rc = pthread_mutex_lock(&test_data.mutex);

  /* The condition has occurred. Set the flag and wake up any waiting threads */
  test_data.conditionMet = 1;
  printf("Wake up all waiting threads...\n");
  rc = pthread_cond_broadcast(&test_data.cond);

  rc = pthread_mutex_unlock(&test_data.mutex);

  printf("Wait for threads and cleanup\n");
  for (i=0; i<NTHREADS; ++i) {
    rc = pthread_join(threadid[i], NULL);
  }

  pthread_cond_destroy(&test_data.cond);
  pthread_mutex_destroy(&test_data.mutex);

  printf("Main completed\n");
}
  • 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-14T21:05:08+00:00Added an answer on June 14, 2026 at 9:05 pm

    The problem is that the member named mutex is a pthread_cond_t and the member named cond is a pthread_mutex_t. Casts that should be unnecessary may be hiding that fact.

    typedef struct _test_data_t {
      int conditionMet;
      pthread_mutex_t cond; // <-- poorly named
      pthread_cond_t mutex; // <-- poorly named
    } test_data_t;
    

    This line in the thread function should not need a cast:

      rc = pthread_mutex_lock((pthread_mutex_t *)&(test_data->mutex));
    

    However, you have several calls that don’t have the cast (so the compiler should be complaining loudly on those). I’m beginning to think this might be a typo that’s a red herring.

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

Sidebar

Related Questions

I am seeing unexpected behavior with the description list <dl> tag. I am using
Seeing some unexpected behavior with Python tonight. Why is the following printing out 'not
I have spring security with https settings. I'm seeing an unexpected behavior when trying
I'm seeing some unexpected behavior in Grails' createCriteria. I have a domain class that
I'm seeing some unexpected behavior in the NuGram IDE Eclipse plug-in for ABNF grammar
I'm seeing a behaviour in Firefox which seems to me unexpected. This is not
I'm attempting to integrate yaml-cpp into a project, but I'm seeing some unexpected errors
If variables in Java are accessed from multiple threads, one must ensure that they
I have a system where I am seeing strange behavior with the serial ports
I am experimenting with SCHED_FIFO and I am seeing some unexpected behaviour. The server

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.