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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:07:01+00:00 2026-05-23T14:07:01+00:00

What I want to accomplish is that a main thread tries a normal deferred

  • 0

What I want to accomplish is that a main thread tries a normal deferred cancel first on a worker thread (executing code that is for my purposes a black box), and then if the thread is still running after a timeout (pthread_timedjoin_np()), I want to do an asynchronous cancel. The problem I’m having is that pthread_setcanceltype() is only for the calling thread. Is there some workaround or hack that will let me do this? I want to avoid using signals as at least under Linux it seems that an asynchronous cancel will still execute C++ destructors of the thread’s objects, which is important for me.

  • 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-05-23T14:07:01+00:00Added an answer on May 23, 2026 at 2:07 pm

    There are some cases, when pthread_setcanceltype() must actually do a cancel (see source below). So, this is a reason, why there is no pthread_setcanceltype_for_thread(). The actual cancel type is the field in pthread struct, which must be changed atomically.

    ftp://sources.redhat.com/pub/glibc/snapshots/glibc-latest.tar.bz2/glibc-20090518/nptl/pthread_setcanceltype.c

    __pthread_setcanceltype (type, oldtype)
         int type;
         int *oldtype;
    {
      volatile struct pthread *self;
    
      self = THREAD_SELF;
    
      int oldval = THREAD_GETMEM (self, cancelhandling);
      while (1)
        {
          int newval = (type == PTHREAD_CANCEL_ASYNCHRONOUS
                        ? oldval | CANCELTYPE_BITMASK
                        : oldval & ~CANCELTYPE_BITMASK);
    
          /* Store the old value.  */
          if (oldtype != NULL)
            *oldtype = ((oldval & CANCELTYPE_BITMASK)
                        ? PTHREAD_CANCEL_ASYNCHRONOUS : PTHREAD_CANCEL_DEFERRED);
    
          /* Update the cancel handling word.  This has to be done
             atomically since other bits could be modified as well.  */
          int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
                                                  oldval);
          if (__builtin_expect (curval == oldval, 1))
            {
              if (CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS (newval))
                {
                  THREAD_SETMEM (self, result, PTHREAD_CANCELED);
                  __do_cancel ();  // HERE THE CANCELLING
                }
    
              break;
            }
    
          /* Prepare for the next round.  */
          oldval = curval;
        }
    
      return 0;
    }
    strong_alias (__pthread_setcanceltype, pthread_setcanceltype)
    

    If you have a big need to change canceltype externally, you can hack the library and set the field directly.

    PS: for NPTL (current implementation of pthreads in glibc on Linux)
    the easiest way to see how to get struct pthread from int pthread_t is … pthread_join:

     pthread_join (pthread_t threadid, thread_return) 
     { 
         struct pthread *pd = (struct pthread *) threadid;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im not sure that its possible to do what I want to accomplish. I
I'm stunned by a simple thing that I want to accomplish but does not
Maybe I should first give an idea of what I want to accomplish as
I want show a form as a dialog but problem is that the worker
That is the question. What I want to accomplish is something similar to the
I want to accomplish a GLSL Shader, that can texture and color my vertex
If I want to create a window that is not a main application window
What I want to accomplish: Images that should be either single or in albums
I currently have code that reads 4 lines and I want to be able
I want to accomplish the following scenario in Drupal: You have 2 content-types. Lets

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.