golang standard library sync.Map the difference between nil and expunge

This is not sync.Map source detailed interpretation, but different focus state entry, in particular to distinguish between state and expunge nil state.

sync.Map storage structure entry is the value which has three values, value (true value), respectively, nil, expunge (arbitrary pointer as a marker). If it is, then as a state machine, which is herein referred to, respectively, three states value, nil, expunge.

A brief summary:

value status: entry is stored inside the true value. At this time, the corresponding entry key, there are three cases, key are present only in the read; key only present in the dirty; dirty in both read and villages

nil state: read the key is deleted when its corresponding value (i.e., entry) p value is set to nil. In this case there is only read the key (ditry is nil); read and are present in the dirty.

expunge state: In this case only the key present in the read and does not exist in the dirty.

The sync.Map abstract to deposit, withdraw, remove the three operations. Change the entry in the view of the state of these three operations.

  1, deposit: sync.Map initial state, a new key-value pair k: A v: A, stored in the first dirty, in which case entry is the state value, key exists only in dirty.

  2, taken: first read does not exist, ranging from the dirty. Not taken from the several read, the read promoted dirty, dirty is emptied. value remains the state entry, corresponding to the key is present in the read.

  3, deposit: on the other key k: B v: B, when stored, copied to Dirty read occurs, read at the same time the amended marked true. At this time, key A is present in the read and dirty, entry to the state value.

  4, Delete: delete key A, entry A the p-value is set to nil. At this time, key A is present in the read and dirty, entry is nil state.

  5, deposit: another key for k: C v: C.

  6. Take: read many times key C, triggering dirty promoted to read, dirty is cleared. At this time, key A is present in the read only medium, entry is nil state.

  7, deposit: on the other key k: D v: D, when depositing, the occurrence of the read copy dirty, entry key A transition from the state to expunge nil state, key A exists only in the read.

  8. Take: Once Read Many key D, triggering dirty promoted to read, dirty is cleared, then key A is completely removed.

Why would delete and expunge two states with nil to mark a value?

First clear, when expunge appear. When the value of the read in key A to nil, then triggers a read operation to copy the other dirty, nil is converted to expunge. At this time, dirty not empty, and the key A does not exist in the dirty. If the re-assignment of key A, modified read, need to synchronize modify dirty, dirty in order to protect when promoted to read the time, including all values.

Nil look emptying, is nil when the state of the A key, and key A dirty read is present in the read or only present in the (empty dirty), and a dirty read because the same key value is a pointer to the same , so in both cases, the re-assignment of key A, all without consideration dirty.

 

 

 

Guess you like

Origin www.cnblogs.com/DillGao/p/11118842.html