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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:41:18+00:00 2026-05-14T16:41:18+00:00

I try to inject the exception to thread using signals, but some times the

  • 0

I try to inject the exception to thread using signals, but some times the exception is not get caught. For example the following code:

void _sigthrow(int sig)
{
    throw runtime_error(strsignal(sig));
}
struct sigaction sigthrow = {{&_sigthrow}};

void* thread1(void*)
{
    sigaction(SIGINT,&sigthrow,NULL);
    try
    {
        while(1) usleep(1);
    }
    catch(exception &e)
    {
        cerr << "Thread1 catched " << e.what() << endl;
    }
};

void* thread2(void*)
{
    sigaction(SIGINT,&sigthrow,NULL);
    try
    {
        while(1);
    }
    catch(exception &e)
    {
        cerr << "Thread2 catched " << e.what() << endl; //never goes here
    }
};

If I try to execute like:

int main()
{
    pthread_t p1,p2;

    pthread_create( &p1, NULL, &thread1, NULL );
    pthread_create( &p2, NULL, &thread2, NULL );

    sleep(1);

    pthread_kill( p1, SIGINT);
    pthread_kill( p2, SIGINT);

    sleep(1);

    return EXIT_SUCCESS;
}

I get the following output:

Thread1 catched Interrupt
terminate called after throwing an instance of 'std::runtime_error'
  what():  Interrupt
Aborted

How can I make second threat catch exception?
Is there better idea about injecting exceptions?

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

    G++ assumes that exceptions can only be thrown from function calls. If you’re going to violate this assumption (eg, by throwing them from signal handlers), you need to pass -fnon-call-exceptions to G++ when building your program.

    Note, however that this causes G++ to:

     Generate code that allows trapping instructions to throw
     exceptions.  Note that this requires platform-specific runtime
     support that does not exist everywhere.  Moreover, it only allows
     _trapping_ instructions to throw exceptions, i.e. memory
     references or floating point instructions.  It does not allow
     exceptions to be thrown from arbitrary signal handlers such as
     `SIGALRM'.
    

    This means that exceptioning out from the middle of some random code is NEVER safe. You can only except out of SIGSEGV, SIGBUS, and SIGFPE, and only if you pass -fnon-call-exceptions and they were triggered due to a fault in the running code. The only reason this worked on thread 1 is because, due to the existence of the usleep() call, G++ was forced to assume that it might throw. With thread 2, G++ can see that no trapping instruction was present, and eliminate the try-catch block.

    You may find the pthread cancellation support more akin to what you need, or otherwise just add a test like this somewhere:

    if (*(volatile int *)terminate_flag) throw terminate_exception();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We try to convert from string to Byte[] using the following Java code: String
I try to add an addons system to my Windows.Net application using Reflection; but
I try Request.Form.Set(k, v) but it's throwing exception Collection is read-only
I try to inject log4net in a ILogger property of my service class but
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
I have been using Unity for quite a while but I have always used
I tried to do some refactoring to convert an each block into an inject,
I know its probably possible, but is it practical and doable to try and
I know its probably possible, but is it practical and doable to try and
I have the following interface CatalogVersionService which exposes some services. As well I have

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.