c++ - Lockless reader/writer -
I have some data that has been read and updated by multiple threads. Both should read and write atoms. I was thinking of doing it this way:
// values from ATOM to straight SValues (double A; double b; double c; double D;}; class test {public : Test () {m_pValues = & amp; m_values;} SValues * LockAndGet () {// Unless we can not get the ownership of the Indicator (true) {SValues * pValues = (SValues *): InterlockedExchange (* * long *) M_P value, 0xffffffff); If (Pivelles! = (Seals *) 0xffffffff) {Return P value;}}} Zero Unlock (Cillows * PVLs) {// Return Parent Other thread It can lock: Interlock exchange (* long *) M_P Plus, (long) PVLs);} Private: S value * M_P values; SValues m_values;}; Zero TestFunc () {test test; SValues Update * pValues = test.LockAndGet (); // or check the values. Unlock (Yellow);}
Indicator to steal the data every read and write Tax is secured, which should be used to make a thread, but two interlocked instructions for each access Require there to write a lot of read and I can not tell in advance if there is any more to write or read more.
Can this be more effective? It is also locked during reading, but when it is possible to write more than enough, it reads that there is no point in optimizing for reading, as long as it does not impose a fine on writing.
I was thinking that without an interlocked instruction, the indicator (with a sequence number), copying the data, and then there is a way to say that the sequence number has changed, in which case it Should try again. This will require some memory barriers, though, and I do not know whether it can improve speed or not.
----- Edit -----
Thank you all, great comment! I have not actually run this code, but after today I will try to compare the current method with an important section (if I get the time) I am still looking for an optimal solution, I will be back to more advanced comments. Thanks again!
What you wrote is essentially spinlock if you are going to do so, You can use, such as If you really want a spinlock, use one of the system-provided or library instead of your own writing.
Other possibilities include copying some types of write-on-pointer, to store the data structure, and read the pointer (atomically) on the reading side. Create a new example on the writing side (essentially copy the old data) and swap the atom on the pointer if the old write-up is needed and if you have more than one author, then you can either either loop an exchange-exchange It will be necessary to ensure that you have read it (beware of ABA issues) or mute x authors. If you do this then you need to be careful how you manage memory - you have no need to retrieve data instances when a thread does not refer to it (but not before).
Comments
Post a Comment