Thread Signaling Within Transaction

149 Views Asked by At

I'm currently playing with Intel TSX (Transactional Synchronization Extensions) which is available on the newer Intel Haswell chips and I've been thinking about how to properly do signaling between different threads. I tried using pthread's condition variable and the transaction just keeps aborting which is sort of understandable. That said, does anybody know any effective ways to signal another thread to wake up if the current one is inside a transaction?

void firstThread()     
{
    if ((status = _xbegin()) == _XBEGIN_STARTED) {
        if(someCondition) {
            // signal secondThread to wake up
        }
    }
    else {
        cerr << "Transaction failed\n";
    }
    _xend();
}

void secondThread()
{
    waitForSignal();
    // do something
}
1

There are 1 best solutions below

0
On

The fundamental contract of a transaction is the isolation of state from other threads, until commit time. As a result you'll not be able to do any signalling while remaining transactional**.

Consider two threads, one running non-transactionally waiting at a barrier, and the other arrives at the barrier, but executing a transaction. If the non-transactional thread proceeds, and then the transactional thread is rolled back, for some independent reason, then the non-transactional thread will run wild, and can't be brought back to the barrier after the fact!

In general, in the transactional programming model, anything which can't be rolled-back must abort the transaction, so you'll have to move your signalling code outside the transaction.


**: There may be an exception in some architectures in the future. Transactional extentions to the POWER architecture propose a suspension model for transactions, that allows interleaving of non-transactional and transactional code in a single thread. However, this support isn't in real hardware yet, and certainly not in Intel's RTM implementation.