While implementing the transactional memory algorithm TL2, we noticed that it allows data races: while committing the write transaction, a reading transaction can read the same memory region.
However, the invalid read will be further invalidated because the version of the lock will be updated after a successful write. Therefore, we will not use the invalid read result and the transaction will be aborted.
Data races in C++ causes Undefined Behavior. Is there a way to tell the compiler that data races are OK so formally we don't have any UB in our program?
One possible solution would be allocating the transactional memory like a bunch of std::atomic
:
auto memory_region = new std::atomic<char>[size];
and work with memory using atomic operations with std::memory_order_relaxed
.
However, I was wondering if there is another more elegant solution, that doesn't provide any overhead.