hello everyone...probably this is my first post here so CMIIW...btw currently I have a problem regarding locking implementation on sorted singly-linked list. So far I am only able to implement coarse-locking method with mutex, and the code is as follows :
Are you sure pred->mtx is locked when you call unlock() on line 9?
Note that operator= of std::unique_lock will unlock the mutex that it had locked before. In other words, it looks like line 14 will essentially unlock pred->mtx.
Note that access to first also needs to be protected. This was an issue already with your previous code.
It's not only the write accesses that need to be protected. If a value can be modified then you also need to protect the reads so that you don't end up in a situation where one thread modifies the value while other threads are reading it.
This is hundreds - I lost the will to carry on counting.
And that's before adding the extra work to make the transition from one node to the next safe.
Even if it can be made to work, it will perform poorly.
Between the assignment and the lock, an asynchronous process could have removed the node from the list entirely.
Yes - but isn't it worse than that as the value of first could change after it's value is obtained but before it is assigned to succ as the assignment is not atomic. Don't you first need a lock on first and then assign to succ?