race-condition

Is a copy-on-return operation executed prior or after lock_guard destructor? [duplicate]

 ̄綄美尐妖づ 提交于 2020-07-15 04:36:48
问题 This question already has answers here : C++ return value created before or after auto var destruction? (2 answers) in C++ which happens first, the copy of a return object or local object's destructors? [duplicate] (4 answers) Closed 2 years ago . Is the get_a() function safe for race-conditions or do I need to explicitly copy str_ as in get_b() in order to have a thread-safe function? class Class { public: auto get_a() -> std::string { auto&& guard = std::lock_guard{mutex_}; return str_; }

WRITE_ONCE in linux kernel lists

こ雲淡風輕ζ 提交于 2020-07-04 05:42:29
问题 I am reading the linux kernel implementation of doubled linked list. I do not understand the use of the macro WRITE_ONCE(x, val) . It is defined as follow in compiler.h: #define WRITE_ONCE(x, val) x=(val) It is used seven times in the file, such as static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next) { next->prev = new; new->next = next; new->prev = prev; WRITE_ONCE(prev->next, new); } I have read that it is used for avoiding race conditions. I

WRITE_ONCE in linux kernel lists

折月煮酒 提交于 2020-07-04 05:42:09
问题 I am reading the linux kernel implementation of doubled linked list. I do not understand the use of the macro WRITE_ONCE(x, val) . It is defined as follow in compiler.h: #define WRITE_ONCE(x, val) x=(val) It is used seven times in the file, such as static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next) { next->prev = new; new->next = next; new->prev = prev; WRITE_ONCE(prev->next, new); } I have read that it is used for avoiding race conditions. I

WRITE_ONCE in linux kernel lists

北战南征 提交于 2020-07-04 05:41:41
问题 I am reading the linux kernel implementation of doubled linked list. I do not understand the use of the macro WRITE_ONCE(x, val) . It is defined as follow in compiler.h: #define WRITE_ONCE(x, val) x=(val) It is used seven times in the file, such as static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next) { next->prev = new; new->next = next; new->prev = prev; WRITE_ONCE(prev->next, new); } I have read that it is used for avoiding race conditions. I

Avoid race condition ?. operator

大憨熊 提交于 2020-06-28 05:53:05
问题 Does the ?. operator that can be used to invoke a delegate or event avoid race conditions? Eg. avoid race-condition manually: //The event-invoking method that derived classes can override. protected virtual void OnShapeChanged(ShapeEventArgs e) { // Make a temporary copy of the event to avoid possibility of // a race condition if the last subscriber unsubscribes // immediately after the null check and before the event is raised. EventHandler<ShapeEventArgs> handler = ShapeChanged; if (handler

Adding to a generic dictionary causes IndexOutOfRangeException

♀尐吖头ヾ 提交于 2020-06-09 10:59:20
问题 I'm using a dictionary inside of some Task. Logically I have set it up so that my Keys will never clash, though sometimes when I am adding to the dictionary I get this Exception. Index was outside the bounds of the array. at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) at Rpc.<MapIntoRpc>b__4[T](Object x) in Rpc.cs:line 113 at System.Threading.Tasks.Task`1.InvokeFuture(Object

What is the random factor in node v10 event loop?

对着背影说爱祢 提交于 2020-05-26 09:31:32
问题 My question is about nodejs event loop Consider this code (async () => { let val = 1 const promise = new Promise(async resolve => { resolve() await new Promise(async r => { setTimeout(r) }) await promise val = 2 }) await promise await new Promise(resolve => setTimeout(resolve)) console.log(val) })() With node 10.20.1 (latest version of node 10) for ((i = 0; i < 30; i++)); do /opt/node-v10.20.1-linux-x64/bin/node race-timeout.js; done With node 12.0.0 (first version of node 12) for ((i = 0; i

SwiftUI View (apparently) laid out before init runs

好久不见. 提交于 2020-05-17 05:54:28
问题 TL;DR It seems that the ContentView below evaluates the body's if statement before init has run. Is there a race condition, or is my mental model out-of-order? Kudos A shout-out to Asperi, who provided the state-initializer equivalent that solves today's problem. Code Why does ContentView display "dummy is nil"? It seems something gets closed over before the initializer sets dummy . What is it about the second assignment that fixes things? class Dummy { } struct ContentView: View { @State

How to deal with race conditions in event listeners and shared state?

谁说胖子不能爱 提交于 2020-04-18 05:47:54
问题 I have 2 event listeners that operate on the same shared data/state. For instance: let sharedState = { username: 'Bob', isOnline: false, }; emitter.on('friendStatus', (status) => { sharedState.isOnline = status.isOnline; }); emitter.on('friendData', (friend) => { if (sharedState.isOnline) { sharedState.username = friend.username; } }); My problem is that these events are emitted at any order. The friendData event might come in before the friendStatus . But friendData does something with the

Safely removing list mapping from ConcurrentDictionary

☆樱花仙子☆ 提交于 2020-03-19 06:06:39
问题 I have a ConcurrentDictionary which maps a simple type to a list: var dict = new ConcurrentDictionary<string, List<string>>(); I can use AddOrUpdate() to cater for both initialization of the list when the first value is added, and addition of subsequent values to the list. However, the same isn't true for removal. If I do something like: public void Remove(string key, string value) { List<string> list; var found = dict.TryGetValue(key, out list); if (found) { list.Remove(value); if (list