4 ms·
> what if we want to call Transfer() and some other function together without a possible race condition between them? Assuming you've got some sort of database
by taspeotis 12y ago
> what if we want to call Transfer() and some other function together without a possible race condition between them?
Assuming you've got some sort of database or something that has a notion of a transaction use a TransactionScope [1] with something like IsolationLevel.RepeatableRead. They are quite flexible. They are created with thread affinity [2] so any operation on the same thread can detect their presence and enlist in them. Make a TransactionScope, call your methods in whichever order you want, commit it.
[1] https://msdn.microsoft.com/en-us/library/system.transactions.transactionscope%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/library/system.transactions...
[2] Sort of. They can be shared between threads but the easiest way to use them is to treat them like they're bound to a thread.