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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:00:05+00:00 2026-06-04T00:00:05+00:00

I’ve got a strange problem with pthread_cond_timedwait(): according to the POSIX specification it is

  • 0

I’ve got a strange problem with pthread_cond_timedwait(): according to the POSIX specification it is a cancellation point. However, when I’m calling pthread_cancel() on the thread, it never gets cancelled! Instead, pthread_cond_timedwait() continues to run normally. It doesn’t lock or anything, it just keeps running as if pthread_cancel() had never been called. As soon as I insert a pthread_testcancel() call, however, the thread gets cancelled correctly! Without the call to pthread_testcancel(), the thread is never cancelled although I’m calling pthread_cond_timedwait() all the time.

Does anybody have an idea what’s going wrong here? Thanks a lot!

EDIT: Here comes the code:

#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

// replacement function because OS X doesn't seem to have clock_gettime()
static int clock_gettime(int clk_id, struct timespec* t)
{
    struct timeval now;
    int rv = gettimeofday(&now, NULL);

        if(rv) return rv;

    t->tv_sec = now.tv_sec;
        t->tv_nsec = now.tv_usec * 1000;

    return 0;
}

static void *threadproc(void *data)
{
    pthread_mutex_t mutex;
    pthread_mutexattr_t attr;               
    pthread_cond_t cond;

    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);

    pthread_mutex_init(&mutex, &attr);      
    pthread_mutexattr_destroy(&attr);

    pthread_cond_init(&cond, NULL);

    for(;;) {

        struct timespec ts;

        clock_gettime(0, &ts);

        // wait 60ms
        ts.tv_nsec += 60 * 1000000;

        pthread_mutex_lock(&mutex);
        pthread_cond_timedwait(&cond, &mutex, &ts);
        pthread_mutex_unlock(&mutex);   

#if 0
        pthread_testcancel();
#endif      
    }

    return NULL;    
}

int main(int argc, char *argv[])
{
    pthread_t pThread;

    pthread_create(&pThread, NULL, threadproc, NULL);

    printf("Waiting...\n");
    sleep(5);
    printf("Killing thread...\n");

    pthread_cancel(pThread);
    pthread_join(pThread, NULL);

    printf("Ok!\n");

    return 0;
}
  • 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-04T00:00:07+00:00Added an answer on June 4, 2026 at 12:00 am

    Your expectations about how the code should behave are correct, and in fact it works as expected on other systems I just tested. I think you’ve just found (yet another) bug in OSX.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am using parse_ini_file to read the contents of a file however it is
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.