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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:40:32+00:00 2026-06-09T14:40:32+00:00

We’ve been given some code to look at in preparation for an exam regarding

  • 0

We’ve been given some code to look at in preparation for an exam regarding pthreads and cancelling them properly. We’ve been asked to both explain what is wrong, and fix it.

My answer: The thread handler function enters a wait state without releasing the mutex, but I feel like I’m missing something. Here is the code, the lines adding a clean up handler were added to fix the program. Again, I’m not quite sure WHY this works. (I understand the concept of the clean up handler, but I don’t understand why it fixes this particular problem – another reason why I’m not sure that my answer is correct)

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
const int flag = 1;

static void * cancelme(void *bb_in)
{
  pthread_mutex_lock(&mutex);

  /* Install a cleanup handler */
  pthread_cleanup_push(pthread_mutex_unlock, &mutex);

  while (flag) {
    pthread_cond_wait(&cv, &mutex);
  }

  //before insertion of cleanup handlers: pthread_mutex_unlock(&mutex); was here

  /* Uninstall cleanup handler */
  pthread_cleanup_pop(1);



  return (NULL);
}

int main()
{
  pthread_t t;
  pthread_create(&t, NULL, cancelme, NULL);
  sleep(1);
  pthread_cancel(t);

  printf("Waiting for thread to finish...\n");
  pthread_join(t, NULL);
  printf("Thread finished...\n");

  printf("Waiting to acquire lock...\n");
  pthread_mutex_lock(&mutex);
  printf("Lock acquired\n");

  return (0);
}

Any feedback much appreciated.

  • 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-09T14:40:33+00:00Added an answer on June 9, 2026 at 2:40 pm

    It looks like the code to push the pthread_mutex_unlock function onto the thread’s cleanup stack via pthread_cleanup_push makes the code snippet work because otherwise the thread would grab the shared lock and be forced to exit without releasing it.

    By pushing the pthread_mutex_unlock function onto the cleanup stack, the thread is ensuring that the mutex will get unlocked when the thread exits, even if the exit is forced and the pthread_cleanup_pop function is not hit.

    In essence the cleanup queue gives you the ability to ensure code gets executed / resources are freed / etc upon exit. The pthread_cleanup_pop function just calls the top function on the cleanup stack, while exiting calls all the functions on the cleanup stack. See the man of pthread_cleaup_push for more info.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am writing an app with both english and french support. The app requests
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.