Printing backtrace in signal handler

I am facing problem with synchronization main thread and signal handler one.

Let's consider simple class:

class StackPrinter
        StackPrinter* Instance()
            // Implemented classic singleton

        std::vector<void*> PrintBacktrace(pthread_t threadId)

            std::vector<void*> stack;
            _backtrace = &stack[0];

            // set up signal

            // kill thread so the signal will be raised

            // synchronise this thread with signalHandler with select and read methods

            return stack;

    void** _backtrace;

    void signalHander(int sigNumber)

        // synchronise this thread with PrintBactrace with write method

So this code normally would work just fine: we raise a signal, we handle it in signalHandler method -> notify about finishing work in signalHandler method through write function -> receive that notification through select function with timeout and life can go on... but what if for example this thread (threadId) will be in uninterruptible state?

I won't enter signalHandler method, select() method will receive timeout, but then the app will end it's job in PrintBactrace method returning empty vector stack and we don't know whether this thread won't change it's state so signalHandler method will be executed after all when maybe other process got instance of StackPrinter class and try to raise another signal. So finally another process will use _backtrace array when first one might still want to use it as well.

Please, do remember that everything that is being used in signalHandler method must be async-signal-safe (

EDIT: For a moment I had an idea of static map where key would be thread and value could be vector. So I would cut void** backtrace and paste it in signalHandler function, so the only job of it is to get backtrace and put it in map. If it will end in normal time main thread will get this vector from the map - if not no one will ever get this data. Assuming that thi situation is rare there is no risk of too much used memory. What do you think?