Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are std::signal and std::raise thread-safe?

The C and C++ standards support the concept of signal. However, the C11 standard says that the function signal() cannot be called in multi-threaded environments, or the behavior is undefined. But I think the signal mechanism is by nature for multi-threaded environments.

A quote from the C11 standard 7.14.1.1.7

"Use of this function in a multi-threaded program results in undefined behavior. The implementation shall behave as if no library function calls the signal function."

Any explanations about this?

The following code is self-evident.

#include <thread>
#include <csignal>

using namespace std;

void SignalHandler(int)
{
    // Which thread context here?
}

void f()
{
    //
    // Running in another thread context.
    //
    raise(SIGINT); // Is this call safe?
}

int main()
{
    //
    // Register the signal handler in main thread context.
    //
    signal(SIGINT, SignalHandler);

    thread(f).join();
}
like image 450
xmllmx Avatar asked Jan 30 '13 20:01

xmllmx


People also ask

Is std :: set insert thread safe?

None of the STL containers is thread safe, so std::set in particular isn't. In your case, the issue isn't even really thread safety, though: You simply share an object across multiple threads (fine) and modify it in one thread (fine as well).

Is std :: map thread safe?

It isn't thread safe, insert from two threads and you can end up in an inconstant state.

How do you ensure thread safety in C++?

An object is thread-safe for reading from multiple threads. For example, given an object A, it is safe to read A from thread 1 and from thread 2 simultaneously. If an object is being written to by one thread, then all reads and writes to that object on the same or other threads must be protected.

Do threads share signal handlers?

All threads in a process share the set of signal handlers set up by sigaction(2) and its variants. A thread in one process cannot send a signal to a specific thread in another process.


2 Answers

But I think the signal mechanism is by nature for multi-threaded environments.

I think this sentence is the central misunderstanding. signal() is a method for inter-process communication, not for inter-thread. Threads share common memory and can therefore communicate via mutexes and control structures. Processes don't have common memory and must make-do with some explicit communication structures like signal() or the filesystem.

like image 172
thiton Avatar answered Sep 28 '22 11:09

thiton


I think you're confusing signaling, which is process specific, with communication between threads. If it is sharing information between threads that you're after, you will probably find what you want in the new C++11 thread support library. Of course, it depends on what you really want to do.

From what I can tell of your code, you want a thread to "signal" an event in some way and you want to be able to run some code when that event is signalled. Given that, I'd take a closer look at the Futures section in the thread support library.

like image 38
Carl Avatar answered Sep 28 '22 10:09

Carl