locking

Reading interlocked variables

岁酱吖の 提交于 2019-12-18 10:34:58
问题 Assume: A. C++ under WIN32. B. A properly aligned volatile integer incremented and decremented using InterlockedIncrement() and InterlockedDecrement() . __declspec (align(8)) volatile LONG _ServerState = 0; If I want to simply read _ServerState, do I need to read the variable via an InterlockedXXX function? For instance, I have seen code such as: LONG x = InterlockedExchange(&_ServerState, _ServerState); and LONG x = InterlockedCompareExchange(&_ServerState, _ServerState, _ServerState); The

We need to lock a .NET Int32 when reading it in a multithreaded code?

大憨熊 提交于 2019-12-18 10:29:16
问题 I was reading the following article: http://msdn.microsoft.com/en-us/magazine/cc817398.aspx "Solving 11 Likely Problems In Your Multithreaded Code" by Joe Duffy And it raised me a question: "We need to lock a .NET Int32 when reading it in a multithreaded code?" I understand that if it was an Int64 in a 32-bit SO it could tear, as it is explained in the article. But for Int32 I imagined the following situation: class Test { private int example = 0; private Object thisLock = new Object();

In Java what is the performance of AtomicInteger compareAndSet() versus synchronized keyword?

本秂侑毒 提交于 2019-12-18 10:27:47
问题 I was implementing a FIFO queue of requests instances (preallocated request objects for speed) and started with using the "synchronized" keyword on the add method. The method was quite short (check if room in fixed size buffer, then add value to array). Using visualVM it appeared the thread was blocking more often than I liked ("monitor" to be precise). So I converted the code over to use AtomicInteger values for things such as keeping track of the current size, then using compareAndSet() in

Parallel.ForEach with adding to list

徘徊边缘 提交于 2019-12-18 10:23:12
问题 I'm trying to run multiple functions that connect to a remote site (by network) and return a generic list. But I want to run them simultaneously. For example: public static List<SearchResult> Search(string title) { //Initialize a new temp list to hold all search results List<SearchResult> results = new List<SearchResult>(); //Loop all providers simultaneously Parallel.ForEach(Providers, currentProvider => { List<SearchResult> tmpResults = currentProvider.SearchTitle((title)); //Add results

How long will a C# lock wait, and what if the code crashes during the lock?

天涯浪子 提交于 2019-12-18 10:16:07
问题 I saw the following code, and wanted to use it for a simple activity which may only be executed one at a time, and won't occur frequently (so the chance of occurring twice at a time is very small, but you never know). So the code: // class variable private static object syncRoot = new object(); // in a method: lock (syncRoot) { DoIt(); } When another thread comes by and wants to execute the code, how long will it wait until the lock is released? Forever, or can you somehow set a timeout? And

What is the difference between Lock and RLock

会有一股神秘感。 提交于 2019-12-18 10:07:53
问题 From the docs: threading.RLock() -- A factory function that returns a new reentrant lock object. A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again without blocking; the thread must release it once for each time it has acquired it. I am not sure why do we need this? what's the difference between Rlock and Lock ? 回答1: The main difference is that a Lock can only be acquired once. It cannot be

How to use a mutex

試著忘記壹切 提交于 2019-12-18 09:06:37
问题 I have one thread, that is sending data stored in a buffer of type List< string> via tcp. Another thread is writing into the buffer. As I am not very familiar with c# I'd like to know how I should use lock or Mutex correctly. This is the code I'd like to use eventually: while(buffer.isLocked()) { buffer.wait(); } buffer.lockBuffer(); buffer.add(tcpPacket); buffer.unlockBuffer(); buffer.notify(); This is my current code. I hope someone can help me complete it. public class Buffer { private

C# Lock syntax - 2 questions

吃可爱长大的小学妹 提交于 2019-12-18 08:56:33
问题 Can I use string as locker in the Lock ? lock("something") Can I do lock without braces if its only one line ? lock("something") foo(); 回答1: 1) Yes, strings are (generally) interned ( by default , thanks @Eric), so any instance of the same "something" would point to the same object, therefore you'd be ok. This is very bad practice though, because someone else, in another library for example, could lock on your string, thus giving the potential for deadlocks. See here: Using string as a lock

How to understand the “non-fair” mode of ReentrantReadWriteLock?

守給你的承諾、 提交于 2019-12-18 05:56:33
问题 ReentrantReadWriteLock has a fair and non-fair(default) mode, but the document is so hard for me to understand it. How can I understand it? It's great if there is some code example to demo it. UPDATE If I have a writing thread, and many many reading thread, which mode is better to use? If I use non-fair mode, is it possible the writing thread has little chance to get the lock? 回答1: Non-fair means that when the lock is ready to be obtained by a new thread, the lock gives no guarantees to the

fopen file locking in PHP (reader/writer type of situation)

一世执手 提交于 2019-12-18 05:05:22
问题 I have a scenario where one PHP process is writing a file about 3 times a second, and then several PHP processes are reading this file. This file is esentially a cache. Our website has a very insistent polling, for data that changes constantly, and we don't want every visitor to hit the DB every time they poll, so we have a cron process that reads the DB 3 times per second, processes the data, and dumps it to a file that the polling clients can then read. The problem I'm having is that,