(CopyOnWrite) in a multi-threaded environment in the trap (c)
(CopyOnWrite) in a multi-threaded environment in the trap (1)
(CopyOnWrite) in a multi-threaded environment in the trap (2)
In fact, this bug does not occur in:
When UI main thread in the use of the string strLog update Edit,
AddText by other threads at the same time calling for m_strStore + = operator of this situation,
But in:
When UI main thread calls strLog Analysis of the structure function,
AddText by other threads at the same time calling for m_strStore + = operator such times.
Why should there be such a situation?
First, we look at when UI main thread calls strLog Analysis of the structure function:
About the program flow is:
A1. Check whether more than one reference count
A2. Yes, the reference count by one, returning to the
A3. Otherwise, the release of a string
Note 1-Step 2 is not an atomic operation, and read the reference count when there is no critical-protection.
Then we look at the other threads AddText call for m_strStore + = operation happening in the
B1. Determine whether more than one reference count
B2. Yes, the reference count by one, and the allocation of new string at the
B3. Whether, in the current string of succession on the + = operator, if the space is insufficient, may have to reallocate string.
Similarly, 1-Step 2 is not an atomic operation, and read the reference count when there is no critical-protection.
The implementation of this consider the following sequence:
A1, / / 2 reference count found
B1 / / 2 reference count found
B2 / / reference count by one, and the allocation of new string at the
A2 / / reference count by one, returning to the
End b2 in the implementation of the time, has a reference count, but a thread running a2 next time, the reference count by one once again, into 0. At the same time, this has become a memory, "Guhunshugui", and not by any one std:: string object owned by a memory leak.
We are now discovered a problem, how to correct this error?
(CopyOnWrite) in a multi-threaded environment in the trap (4)








0 Comments to “(CopyOnWrite) in a multi-threaded environment in the trap (c)”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.