Is iterating ConcurrentHashMap values thread safe?

What does it mean? That means that each iterator you obtain from a ConcurrentHashMap is designed to be used by a single thread and should not be passed around. This includes the syntactic sugar that the for-each loop provides. What happens if I try to iterate the map with two threads at the same time? … Read more

What Makes a Method Thread-safe? What are the rules?

If a method (instance or static) only references variables scoped within that method then it is thread safe because each thread has its own stack: In this instance, multiple threads could call ThreadSafeMethod concurrently without issue. public class Thing { public int ThreadSafeMethod(string parameter1) { int number; // each thread will have its own variable … Read more

Concurrent HashSet in .NET Framework?

Your implementation is correct. The .NET Framework does not provide a built-in concurrent hashset type, unfortunately. However, there are some workarounds. ConcurrentDictionary (recommended) This first one is to use the class ConcurrentDictionary<TKey, TValue> in the namespace System.Collections.Concurrent. In the case, the value is pointless, so we can use a simple byte (1 byte in memory). … Read more

Automating the InvokeRequired code pattern

Lee’s approach can be simplified further public static void InvokeIfRequired(this Control control, MethodInvoker action) { // See Update 2 for edits Mike de Klerk suggests to insert here. if (control.InvokeRequired) { control.Invoke(action); } else { action(); } } And can be called like this richEditControl1.InvokeIfRequired(() => { // Do anything you want with the control … Read more

Java synchronized method lock on object, or method?

If you declare the method as synchronized (as you’re doing by typing public synchronized void addA()) you synchronize on the whole object, so two thread accessing a different variable from this same object would block each other anyway. If you want to synchronize only on one variable at a time, so two threads won’t block … Read more

Is local static variable initialization thread-safe in C++11? [duplicate]

The relevant section 6.7: such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. […] If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. Then there’s a … Read more

Queue.Queue vs. collections.deque

Queue.Queue and collections.deque serve different purposes. Queue.Queue is intended for allowing different threads to communicate using queued messages/data, whereas collections.deque is simply intended as a datastructure. That’s why Queue.Queue has methods like put_nowait(), get_nowait(), and join(), whereas collections.deque doesn’t. Queue.Queue isn’t intended to be used as a collection, which is why it lacks the likes … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)