thread-safety

Why is threading used for sockets?

不羁的心 提交于 2021-02-19 08:53:05
问题 Ever since I discovered sockets, I've been using the nonblocking variants, since I didn't want to bother with learning about threading. Since then I've gathered a lot more experience with threading, and I'm starting to ask myself.. Why would you ever use it for sockets? A big premise of threading seems to be that they only make sense if they get to work on their own set of data. Once you have two threads working on the same set of data, you will have situations such as: if(!hashmap.hasKey(

How to make java class thread safe?

寵の児 提交于 2021-02-19 02:37:14
问题 I have a java class as below class User { String name; String phone; public String getName() { return name; } public String getPhone() { return phone; } } The way this class is used is, for every thread 1 object of this User class is created. Now since there is one copy of object for every thread, can i call this class as thread safe? Do I need to synchronize these methods? 回答1: The way you presented it, if each thread has its one copy, then it can be called thread-safe, as maximum of

Is Extension method thread safe?

江枫思渺然 提交于 2021-02-19 00:41:01
问题 Is this Extension method thread safe? public static class Extensions { public static void Raise<T>(this EventHandler<T> handler, object sender, T args) where T : EventArgs { if (handler != null) handler(sender, args); } } or do I need to change it to this? public static class Extensions { public static void Raise<T>(this EventHandler<T> handler, object sender, T args) where T : EventArgs { var h = handler; if (h!= null) h(sender, args); } } 回答1: You found an interesting loop hole, it tripped

Is Extension method thread safe?

醉酒当歌 提交于 2021-02-19 00:39:56
问题 Is this Extension method thread safe? public static class Extensions { public static void Raise<T>(this EventHandler<T> handler, object sender, T args) where T : EventArgs { if (handler != null) handler(sender, args); } } or do I need to change it to this? public static class Extensions { public static void Raise<T>(this EventHandler<T> handler, object sender, T args) where T : EventArgs { var h = handler; if (h!= null) h(sender, args); } } 回答1: You found an interesting loop hole, it tripped

confused about atomic class: memory_order_relaxed

这一生的挚爱 提交于 2021-02-18 11:41:14
问题 I am studying this site: https://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync, which is very helpful to understand the topic about atomic class. But this example about relaxed mode is hard to understand: /*Thread 1:*/ y.store (20, memory_order_relaxed) x.store (10, memory_order_relaxed) /*Thread 2*/ if (x.load (memory_order_relaxed) == 10) { assert (y.load(memory_order_relaxed) == 20) /* assert A */ y.store (10, memory_order_relaxed) } /*Thread 3*/ if (y.load (memory_order_relaxed) == 10) assert

Is it thread safe to reset and copy shared_ptr simultaneously?

旧城冷巷雨未停 提交于 2021-02-17 21:50:41
问题 Boost documentation describes shared pointer's behavior when accessing it from multiple threads simultaneously. Particularly they give some examples: shared_ptr<int> p(new int(42)); //--- Example 1 --- // thread A shared_ptr<int> p2(p); // reads p // thread B shared_ptr<int> p3(p); // OK, multiple reads are safe //--- Example 2 --- // thread A p.reset(new int(1912)); // writes p // thread B p2.reset(); // OK, writes p2 //--- Example 3 --- // thread A p = p3; // reads p3, writes p // thread B

Exit a method if another thread is executing it

做~自己de王妃 提交于 2021-02-17 21:31:54
问题 I have a method in a multi-threaded application and I'd like the following behavior when this method is invoked: If no other threads are currently executing the method, execute it. If another thread is currently executing the method then exit the method without executing it. The lock statement in C# is useful for waiting until a thread has completed execution, but I don't want to serialize access to this method but rather bypass executing said method if it is being executed by another thread.

is boost::property_tree::ptree thread safe?

放肆的年华 提交于 2021-02-17 15:14:36
问题 I'm using boosts read_json in a couple of threads in a piece of code. A simplified breakdown of the call is below. I'm getting segfaults in one of the threads (and sometimes the other) and it's making me think that read_json is not thread safe (Or I'm just using it in a dumb way) void someclass::dojson() { using boost::property_tree::ptree; ptree pt; std::stringstream ss(json_data_string); read_json(ss,pt); } Now json_data_string is different between the two classes (it's just json data

Why does libc++'s implementation of shared_ptr use full memory barriers instead of relaxed?

别说谁变了你拦得住时间么 提交于 2021-02-17 12:22:38
问题 In boost's implementation of shared_ptr , it uses relaxed memory ordering to increment its reference count. This appears safe as decrements use acquire/release to make sure that any previous decrements are visible to the thread before releasing memory. This method seems correct and appears in Herb Sutters talk on atomics In libc++'s implementation uses full memory barriers template <class T> inline T increment(T& t) _NOEXCEPT { return __sync_add_and_fetch(&t, 1); } template <class T> inline T

C# thread-safety on increment/decrement operations [duplicate]

帅比萌擦擦* 提交于 2021-02-17 04:47:24
问题 This question already has answers here : How come INC instruction of x86 is not atomic? [duplicate] (3 answers) Closed 6 years ago . I was trying to investigate a simple piece of code with two threads accessing a shared integer variable, one incrementing and the other decrementing it: static int n = 0; static void Main() { var up = new Task(() => { for (int j = 0; j < 400000; j++) ++n; }); up.Start(); for (int i = 0; i < 400000; i++) --n; up.Wait(); Console.WriteLine(n); Console.ReadKey(); }