-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Describe more mutex problems and add code examples
- Loading branch information
1 parent
c394e78
commit 7107156
Showing
5 changed files
with
120 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Field { | ||
int _property; | ||
public: | ||
int property() const { return _property; } | ||
}; | ||
|
||
list<Field> _fields; | ||
mutex _fields_mutex; | ||
|
||
{ | ||
unique_lock<mutex> guard{_fields_mutex}; | ||
auto it = find_if(_fields.begin(), _fields.end(), | ||
[item_to_remove](auto const& field) { return field.property() == item_to_remove; } ); | ||
|
||
if (it != _fields.end()) { | ||
_fields.erase(it); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Field { | ||
int _property; | ||
public: | ||
int property() const { return _property; } | ||
}; | ||
|
||
list<Field> _fields; | ||
mutex _fields_mutex; | ||
|
||
list<Field> obsolete_field; | ||
{ | ||
unique_lock<mutex> guard{_fields_mutex}; | ||
auto it = find_if(_fields.begin(), _fields.end(), | ||
[item_to_remove](auto const& field) { return field.property() == item_to_remove; } ); | ||
|
||
if (it != _fields.end()) { | ||
obsolete_field.splice(obsolete_field.end(), _fields, it); | ||
} | ||
} | ||
|
||
obsolete_field.clear(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Field { | ||
int _property; | ||
public: | ||
int property() const { return _property; } | ||
}; | ||
|
||
unordered_map<Field> _fields; | ||
mutex _fields_mutex; | ||
|
||
{ | ||
unordered_map<int, Field>::note_type obsolete_node; | ||
{ | ||
unique_lock<mutex> guard{_fields_mutex}; | ||
obsolete_node = _fields.extract(_fields.find(key_to_remove)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Field { | ||
int _property; | ||
public: | ||
int property() const { return _property; } | ||
}; | ||
|
||
vector<Field> _fields; | ||
mutex _fields_mutex; | ||
|
||
vector<Field> obsolete_fields; | ||
{ | ||
unique_lock<mutex> guard{_fields_mutex}; | ||
auto it = remove_if(_fields.begin(), _fields.end(), | ||
[items_to_remove](auto const& field) { return field.property() == items_to_remove; } ); | ||
|
||
obsolete_fields.resize(distance(it, _fields.end())); | ||
std::move(it, _fields.end(), obsolete_fields.begin()); | ||
} | ||
obsolete_fields.resize(0); |