Reputation: 818
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:
Multiple threads are running continuously updating their own counters.
SIGALRM is registered and it periodically tells each of these threads to print their counters
Upvotes: 0
Views: 236
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