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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:39:39+00:00 2026-06-06T20:39:39+00:00

The following (pseudo-) code induces some behaviour which I dont understand. I have 2

  • 0

The following (pseudo-) code induces some behaviour which I dont understand. I have 2 threads running parallel. Both do the same (complex) calculation but I dont know which will finish first. Repeatedly run, there are cases where the first is faster and cases where the second is faster. This is okay and works as intended. Then, the first successfull thread should terminate the other thread and both should fork together. However if the first solver finishs, everything works out but if the second finishes first, the “join command” does not recognize that the first solver is terminated (so the join waits forever, the programm does not continue). Any ideas what I did wrong or what I could do different?

void* thread_function(...)
{
   do_some_complex_task();
   if (successfull && first_finsihed)
   {
        pthread_chancel(other_thread);
   }
}

int main()
{
    std::vector<pthread_t*> store;
    store.resize(2);

    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

    pthread_create(store[0], NULL, thread_function, ...);
    pthread_create(store[1], NULL, thread_function, ...);

    pthread_join(*store[0], NULL);
    pthread_join(*store[1], NULL);
}

PS. If the pseudo code is not detailed enough please let me know.

  • 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-06T20:39:40+00:00Added an answer on June 6, 2026 at 8:39 pm

    Based on the pseudo code, one problem may be that the threads have deferred cancellation (the default value) instead of asynchronous cancellation. If the canceled thread never reaches a cancellation point, then pthread_join would block. When calling pthread_create, the newly created thread inherits the calling thread’s:

    • signal mask (pthread_signmask)
    • floating point environment (fenv)
    • capabilities
    • CPU affinity mask (sched_setaffinity)

    Try invoking pthread_setcanceltype from within threads you wish to cancel. You may also want to consider attaching a debugger to the program to identify the current state of the threads.

    Ideally, if it is at all possible, consider avoiding pthread_cancel. Although the pthread-calls are documented, it can be difficult to obtain and maintain the exact behavior due to all of the minor details. It is generally easier to set a flag that indicates that the thread is set to exit, and begin to return from functions when it is set. In do_some_complex_task, consider breaking the complex task into smaller tasks, and check for cancellation between each smaller task.

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

Sidebar

Related Questions

I have the following piece of pseudo-C/Java/C# code: int a[]= { 30, 20 };
i have following pseudo code : void siftup(int n) pre condition n>0 && heap(1,n-1)
I have the following (pseudo) code root = _iface.createRoot(...) Label l = new Label(hello
I have the following pseudo code that works: KeyBindings[Keys.Right] += Method1; KeyBindings[Keys.Right] += Method2;
I have the following pseudo-code in my book for a breadth-first search: function breadth_first_search:
I have a following pseudo-code. public boolean onContextItemSelected(MenuItem aItem) { switch(aItem.getItemId()) { case A:
BACKGROUND: I have xaml similar to the following pseudo code ... <DataContext><vm:parentVM/></DataContext> <Grid x:Name=LayoutRoot>
I have the following pseudo code: class A: mutex lockForB class B: def __init__(self,
I have the following pseudo code using EF Code First: User user = GetFromCache();
I have a button click event handler with the following pseudo code: private void

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.