thread-safety

Will a calling thread see modifications to local variables after thread.join()?

天大地大妈咪最大 提交于 2019-12-23 12:13:31
问题 In the simplest possible example, let's say I have a function that starts a thread, which in turn sets the value of a local variable to true. We join the thread, then leave the function. bool func() { bool b = false; std::thread t([&]() { b = true; }); t.join(); return b; } Will this function return true, or is the behavior undefined? 回答1: Yes, it must return true. [thread.thread.member] void join(); 4 Effects : Blocks until the thread represented by *this has completed. 5 Synchronization :

Why are atomic operations considered thread-safe?

故事扮演 提交于 2019-12-23 12:12:02
问题 How are atomic operations made thread-safe? I've read about the subject in Wikipedia's article on thread-safety. But the article didn't really explain the process behind the scenes. In other words, why can't an "atomic" operation executed by a thread A be interrupted by a thread B? 回答1: An atomic operation will either be completed or not done at all. Other threads will not be able to see the operation "in progress" -- it will never be viewed in a partially complete state. This is what the

C# Random Number Generator getting stuck in a cycle

我与影子孤独终老i 提交于 2019-12-23 10:47:34
问题 I am using .NET to create an artificial life program and I am using C#'s pseudo random class defined in a Singleton. The idea is that if I use the same random number generator throughout the application, I could merely save the seed and then reload from the seed to recompute a certain interesting run. public sealed class RandomNumberGenerator : Random { private static readonly RandomNumberGenerator instance = new RandomNumberGenerator(); RandomNumberGenerator() { } public static

python thread safe mutable object copy

血红的双手。 提交于 2019-12-23 10:07:02
问题 Is python's copy module thread safe? If not, how should I copy\deepcopy mutable objects in a thread-safe manner in python? 回答1: Python's GIL protects bytecodes, not Python statements (see short or long explanations). As both copy.copy() and copy.deepcopy() are implemented in python, they are certainly more than a single bytecode, so no, they are not thread safe! If you must work with multiple threads, and there are many cases you should such as having IO dedicated threads, do what must be

How to implement thread-safe container with natural looking syntax?

痴心易碎 提交于 2019-12-23 08:34:05
问题 Preface Below code results in undefined behaviour, if used as is: vector<int> vi; ... vi.push_back(1); // thread-1 ... vi.pop(); // thread-2 Traditional approach is to fix it with std::mutex : std::lock_guard<std::mutex> lock(some_mutex_specifically_for_vi); vi.push_back(1); However, as the code grows, such things start looking cumbersome, as everytime there will be a lock before a method. Moreover, for every object, we may have to maintain a mutex. Objective Without compromising in the

Is Hunspell thread safe?

自作多情 提交于 2019-12-23 08:28:06
问题 Is the Hunspell spelling library thread-safe? 回答1: The answer is NO, A simple multi-threaded test application revealed that Hunspell uses per-instance resources for the spelling process, so only one thread can use it at any time - use locks/work queue/or instanciate a per-thread Hunspell instance. 来源: https://stackoverflow.com/questions/4639789/is-hunspell-thread-safe

Is this combination of ConcurrentDictionary and ConcurrentQueue thread-safe?

耗尽温柔 提交于 2019-12-23 07:32:37
问题 I'm using the ConcurrentDictionary and ConcurrentQueue classes from .NET 4 in the following code. Is this code thread-safe? If not, how can I make it thread-safe? public class Page { public string Name {get; set; } } public class PageQueue { private ConcurrentDictionary<int, ConcurrentQueue<Page>> pages = new ConcurrentDictionary<int, ConcurrentQueue<Page>>(); public void Add(int id, Page page) { if (!this.pages.ContainsKey(id)) this.pages[id] = new ConcurrentQueue<Page>(); this.pages[id]

Is this combination of ConcurrentDictionary and ConcurrentQueue thread-safe?

☆樱花仙子☆ 提交于 2019-12-23 07:32:06
问题 I'm using the ConcurrentDictionary and ConcurrentQueue classes from .NET 4 in the following code. Is this code thread-safe? If not, how can I make it thread-safe? public class Page { public string Name {get; set; } } public class PageQueue { private ConcurrentDictionary<int, ConcurrentQueue<Page>> pages = new ConcurrentDictionary<int, ConcurrentQueue<Page>>(); public void Add(int id, Page page) { if (!this.pages.ContainsKey(id)) this.pages[id] = new ConcurrentQueue<Page>(); this.pages[id]

Why isn’t ReadOnlyDictionary thread-safe?

自古美人都是妖i 提交于 2019-12-23 07:19:26
问题 I’m looking for a readonly-dictionary to be accessed from multiple threads. While ConcurrentDictionary exposes such capabilities, I don’t want to have the overhead and the strange API. .Net 4.5 while providing such a class, the documentation states that only static calls are safe. I wonder why? 回答1: ReadOnlyDictionary is just a wrapper around any other dictionary. As such, it's only as thread-safe as the underlying dictionary. In particular, if there's a thread modifying the underlying

Is an atomic property thread safe? [duplicate]

只愿长相守 提交于 2019-12-23 06:36:56
问题 This question already has answers here : What's the difference between the atomic and nonatomic attributes? (26 answers) Atomic properties vs thread-safe in Objective-C (5 answers) Closed 4 years ago . I have gone through many answers about atomic and non-atomic properties. But I'm not able to understand whether atomic properties are thread safe? Please explain it with an example. 回答1: Yes an / one atomic property is thread-safe. That is what atomicity stands for. CAUTION But neither are two