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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:12:38+00:00 2026-06-12T16:12:38+00:00

Say I have a Thread A, which wants to send signals to Threads B,

  • 0

Say I have a Thread A, which wants to send signals to Threads B, C and D. Can I do something like this.

SendSignalTo( ThreadB, SIGUSR1 );
SendSignalTo( ThreadC, SIGUSR1 );
SendSignalTo( ThreadD, SIGUSR1 );

With SIGUSR1 signal handler, defined differently for Thread B, C and D, like as follows.

void SIGUSR1_Handler_for_ThreadB(...){...}
void SIGUSR1_Handler_for_ThreadC(...){...}
void SIGUSR1_Handler_for_ThreadD(...){...}

And if not, what is the alternative.

  • 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-12T16:12:39+00:00Added an answer on June 12, 2026 at 4:12 pm

    You can send a signal to a specific thread using pthread_kill(), or if you don’t mind being GNU-specific, using pthread_sigqueue() (which can specify one int or void * the handler can access via info->si_value).

    There is only one signal handler per signal for the process. This means that a specific signal will always call the same handler function, no matter which thread it happens to be. If one thread sets a new signal handler, the signal handler will change for all threads.

    However, the workaround is trivial: use a per-thread function pointer to define which function the signal handler should call. Just remember the signal handler limitations — you can only use async-signal safe functions in a signal handler.

    /* Simplify by defining the signal handler function type, assume SA_SIGINFO */
    typedef void (*signal_handler_t)(int, siginfo_t *, void *);
    
    /* Per-thread variable pointing to the desired function */
    static __thread signal_handler_t  thread_handler = NULL;
    
    /* Process-wide actual signal handler */
    static void signal_handler(int signum, siginfo_t *info, void *context)
    {
        signal_handler_t  func;
    
        func = __sync_fetch_and_or(&thread_handler, (signal_handler_t)0);
    
        if (func)
                func(signum, info, context);
    }
    

    The atomic load (__sync_fetch_and_or()) allows you to trivially change the per-thread handler using a simple atomic store at any point in time, without even blocking the signal. Switching to function new_thread_handler is then

        signal_handler_t  func;
    
        do {
            func = thread_handler;
        } while (!__sync_bool_compare_and_swap(&thread_handler, func, new_thread_handler));
    

    The __sync_fetch_and_or() and the function switch can both be replaced by one C++11-style __atomic_ call, but I don’t have a GCC recent enough yet, so I’m still using the old-style __sync_ calls.

    POSIX also supports real-time signals, SIGRTMIN+0, SIGRTMIN+1, .., SIGRTMAX. They have the added benefit that more than one of them can be pending at the same time. They are much better suited for this kind of thing than the traditional signals.

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

Sidebar

Related Questions

Let's say I started a thread and I have something like this: ...//initiate all
Lets say we have this code: bool KeepGoing = true; DataInThread = new Thread(new
Using non-thread safe libraries with threads. Say I have a library that makes a
I have two threads, lets say thread A and thread B. Thread A post's
Let's say I have a static class with a static method. Multiple threads can
lets say i have an event which gets fired like 10 times per secod
Let's say I have a situation in Silverlight where there is a background thread
Let's say that I have the following code that's run in one thread of
Let's say I have 10 threads running simultaneously. Is there any way of calling
Lets say have this immutable record type: public class Record { public Record(int x,

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.