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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:58:58+00:00 2026-05-23T16:58:58+00:00

// threadA.c int main() { int res; pthread_t a_thread; void *thread_result; res = pthread_create(&a_thread,

  • 0
// threadA.c
int main() {
    int res;
    pthread_t a_thread;
    void *thread_result;

    res = pthread_create(&a_thread, NULL, thread_function, NULL);
    if (res != 0) {
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }
    sleep(3);
    printf("Canceling thread...\n");
    res = pthread_cancel(a_thread);
    if (res != 0) {
        perror("Thread cancelation failed");
        exit(EXIT_FAILURE);
    }
    printf("Waiting for thread to finish...\n");
    res = pthread_join(a_thread, &thread_result);
    if (res != 0) {
        perror("Thread join failed");
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
    int i, res, j;
    res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    if (res != 0) {
        perror("Thread pthread_setcancelstate failed");
        exit(EXIT_FAILURE);
    }
    res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
    if (res != 0) {
        perror("Thread pthread_setcanceltype failed");
        exit(EXIT_FAILURE);
    }
    printf("thread_function is running\n");
    for(i = 0; i < 10; i++) {
        printf("Thread is still running (%d)...\n", i);
        sleep(1);
    }
    pthread_exit(0);
}

Output is as follows:

$ ./threadA
thread_function is running
Thread is still running (0)...
Thread is still running (1)...
Thread is still running (2)...
Canceling thread...
Waiting for thread to finish...
$

When finishing waiting 3 seconds, the main thread issues the command pthread_cancel to stop the child thread and the child thread really starts to response the cancellation after the command pthread_join is invoked.

At the moment, the main thread runs to the line immediately after pthread_join, the child thread is running inside the loop of the following code,

    for(i = 0; i < 10; i++) {
        printf("Thread is still running (%d)...\n", i);
        sleep(1);
    }

I don’t see any checking statement inside this loop, but the main thread still is able to cancel
the child thread. I assume that the POSIX multithread system has a checking system internally so that it can terminate the child thread when the pthread_join is called in the main thread.

Question>

Basically, I need to understand how the child thread can be cancelled inside the loop without itself checking any flags.

Also, please correct my description if anything is wrong.

  • 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-23T16:58:58+00:00Added an answer on May 23, 2026 at 4:58 pm

    What’s happening is that your loop contains at least one cancellation point, sleep (and possibly two, since printf is an optional cancellation point).

    Being a cancellation point means the function contains logic similar to:

    if (thread_local_cancellation_flag) {
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE);
        pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED);
        pthread_exit(PTHREAD_CANCELED);
    }
    

    In reality, however, it’s a little bit more complicated, because if the cancellation request arrives while the function is “waiting” or “blocking” for some event to take place (like sleep time to expire, or input from a socket), it must be acted upon. Thus some sort of asynchronous delivery mechanism is required, and the typical implementation is to use signals for this, but it’s actually extremely difficult to get right, and popular implementations don’t do so well. For some ugly corner cases in glibc which other implementations probably share, see this bug report:

    http://sourceware.org/bugzilla/show_bug.cgi?id=12683

    In your case, what’s almost surely happening is that the cancellation request arrives (via a signal) while the thread is waiting in sleep, and a signal handler runs, determines that it’s in the middle of a cancellable operation, and acts on the cancellation request.

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

Sidebar

Related Questions

The function header for pthread_create looks like this: int pthread_create(pthread_t * thread, const pthread_attr_t
#include<pthread.h> #include<stdio.h> #include<semaphore.h> void func(); int a; int main() { pthread_t thread1; sem_t semaphore1;
A question about threads in C/C++... C++0x syntax #include <thread> void dummy() {} int
public class PlayText extends Thread { private int duration; private String text; private PlayerScreen
How do I modify an int atomically and thread-safely in Java? Atomically increment, test
Exception in thread Thread-2 java.lang.NumberFormatException: For input string: 3 int test = Integer.parseInt(result[0]); This
I am using ACE threads and need each thread to have its own int
Is there a portable way of getting thread and/or process identifier (string, int, ...)
Can Thread.getContextClassLoader() be null ? The javadoc is not really clear. Should a library
The main() function creates a thread that is supposed to live until the user

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.