New Posts  All Forums:Forum Nav:

Signals with Threads

post #1 of 3
Thread Starter 
I have a process with multiple threads running. If one thread sends a signal to the process, will every thread see the signal? If not which thread will? Are signals thread-safe?
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #2 of 3
Well seeing as no-one else seems to be answering, I'll give it a go... I'm kinda learning this stuff myself at the moment in my (limited) spare time but here's what I think I've understood so far (picking stuff up from various sites).

If anyone who knows this stuff thinks I've made a mistake please let me know... always happy to learn I'm assuming you're using Unix / Linux or BSD/MacOS here - Windows has a completely different (and more simple) model.

First rule for implementing signal handlers: don't make any assumptions about which thread is handling the signal.

In a single-threaded application, all signal handlers run on the main thread. In a multithreaded application, signals that are not tied to a specific hardware error (such as an illegal instruction) get delivered to whichever thread happens to be running at the time.

If multiple threads are running simultaneously, the signal is delivered to whichever one the system happens to pick - it's completely arbitrary. i.e. signals can be delivered to any active thread of your application.

If a specific thread wants to handle a given signal, you need to work out some way of notifying that thread when the signal arrives. You cannot assume that installation of a signal handler from that thread will result in the signal being delivered to the same thread.

If you only want some threads to be able to handle the signal, and you want to disable it for the rest, you can use pthread_sigmask(). If you need it to be delivered to a specific worker thread, all the other threads must be masked. To disable the handling of a signal altogether, you can use sigprocmask().

No... I changed my mind - the first rule of signal handling is that signals and multiple threads are a bad combo and should be avoided like the plague

Anyway hope that was some help and you're not more confused than me now.

Incidentally, I just found this
http://softwareblogs.intel.com/2006/...posix-threads/
post #3 of 3
Thread Starter 
Quote:
Originally Posted by Stormwolf View Post
Well seeing as no-one else seems to be answering, I'll give it a go... I'm kinda learning this stuff myself at the moment in my (limited) spare time but here's what I think I've understood so far (picking stuff up from various sites).

If anyone who knows this stuff thinks I've made a mistake please let me know... always happy to learn I'm assuming you're using Unix / Linux or BSD/MacOS here - Windows has a completely different (and more simple) model.

First rule for implementing signal handlers: don't make any assumptions about which thread is handling the signal.

In a single-threaded application, all signal handlers run on the main thread. In a multithreaded application, signals that are not tied to a specific hardware error (such as an illegal instruction) get delivered to whichever thread happens to be running at the time.

If multiple threads are running simultaneously, the signal is delivered to whichever one the system happens to pick - it's completely arbitrary. i.e. signals can be delivered to any active thread of your application.

If a specific thread wants to handle a given signal, you need to work out some way of notifying that thread when the signal arrives. You cannot assume that installation of a signal handler from that thread will result in the signal being delivered to the same thread.

If you only want some threads to be able to handle the signal, and you want to disable it for the rest, you can use pthread_sigmask(). If you need it to be delivered to a specific worker thread, all the other threads must be masked. To disable the handling of a signal altogether, you can use sigprocmask().

No... I changed my mind - the first rule of signal handling is that signals and multiple threads are a bad combo and should be avoided like the plague

Anyway hope that was some help and you're not more confused than me now.

Incidentally, I just found this
http://softwareblogs.intel.com/2006/...posix-threads/
That's pretty much what I thought - I had just heard some differing things and wanted to be sure. Condition variables are better suited to this particular problem anyway, I was just wondering about how signals worked in a multithreaded environment
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming