Clarrification about "trap"

I know in ruby we use “trap” command to handle OS signals while passing
the signal and the proc object as parameters respectively. when i call
this trap function before any infinite loop it does work perfectly.
Means it listens to the keybord signals and do as i want. But the
problem i have is that im not clear about the way it works… In c++ we
use separate thread to listen for signals and use another thread to run
the program. Is the same scenario happens here with trap
function???.. Please give me a clarification

Thanx in advance

Thats how it is done:

I’m short on an explanation right now though, better let’s wait for an
appropriate answer.