1

I'm writing some TBB code and would like to use the TBB read/write mutex. There is a return value for upgrading to a writer shown:

//Change reader lock to writer lock. Return false if lock
// was released and reacquired. true otherwise, including if
// the lock was already a writer lock.
bool RW::scoped_lock::upgrade_to_writer()

What is the significance of this return value? For reference, my implementation looks correct to me but Intel Inspector is reporting a data race inside the critical section and I'm suspicious that it may have something to do with this variable.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
dromodel
  • 9,581
  • 12
  • 47
  • 65
  • 1
    The comment explains. If it returns `false`, then it means that the lock was released and reacquired, so the data that the lock was protecting was momentarily unprotected. – Raymond Chen Oct 30 '19 at 19:02
  • So I should be wary of the value having changed if the return value was false? – dromodel Oct 30 '19 at 20:43
  • That's what the comment says, yes. – Raymond Chen Oct 30 '19 at 20:53
  • Odd design choice, it seems, to release a held reader lock in an attempt to upgrade it. – 500 - Internal Server Error Oct 30 '19 at 20:55
  • On some architectures it is necessary. It will tell you if it released, and you know the value may have changed if it had to be released. – cahuson Oct 30 '19 at 23:58
  • If two or more readers hold the lock and try to upgrade simultaneously, only one can succeed, no matter which architecture is used. The only alternative to this design choice would be to release the lock without re-acquiring, which seems to be worse. – Alexey Kukanov Nov 06 '19 at 13:25

0 Answers0