Rohit Walavalkar
Rohit Walavalkar

Reputation: 818

Thread communication on receiving a signal

I have spawned multiple threads from my main thread. All these threads have the same start routine. This routine contains a counter that is local to the routine. I would like to print this counter periodically. How do I achieve this ?

I know that we can use conditional variables for communication between the different threads. But I do not want to block any of my threads and make them wait for a particular signal, in short counter has to updated continuously.

To summarize this is what I want to achieve:

  1. Multiple threads are running continuously updating their own counters.

  2. SIGALRM is registered and it periodically tells each of these threads to print their counters

Upvotes: 0

Views: 236

Answers (1)

John Stone
John Stone

Reputation: 86

just use a global variable as a counter for SIGALRM which is updated in the handler,and then in the routine, check if the global counter value changed, print the local counter and save the value of the global counter to a local variable

Upvotes: 2

Related Questions