locking

C# Threading/Lock confusion

青春壹個敷衍的年華 提交于 2019-11-30 04:55:05
问题 I have the following code: var items = new List<string> {"1", "2", "3"}; // 200 items foreach(var item in items) { ThreadPool.QueueUserWorkItem((DoWork), item); } private void DoWork(object obj) { lock(this) { using(var sw = File.AppendText(@"C:\somepath.txt") { sw.WriteLine(obj); } } } Because of the threading, for some reason, I get a random number of the 200 items written out to the file. 60 or 127 or sometimes only 3. If I remove the ThreadPool and just write inside the original foreach

Locking with timeout pattern

那年仲夏 提交于 2019-11-30 04:50:56
问题 lock uses this pattern if(Monitor.Enter(lock)) try { ... } finally { Monitor.Exit(lock); } // using this style to reduce post "height" if we don't want to wait infinite we can provide timeout if(!Monitor.TryEnter(lock, timeout)) throw new TimeoutException(); try { ... } finally { Monitor.Exit(lock); } I have scenario when method has to obtain multiple locks before it start doing anything. This looks awful: if(!Monitor.TryEnter(lockA, timeout)) throw new TimeoutException(); try { if(!Monitor

Are empty synchronized blocks optimized out by Java compiler?

让人想犯罪 __ 提交于 2019-11-30 04:49:47
问题 Suppose somewhere in my code I write an empty synchronized block: synchronized(obj){ //No code here } So as the synchronized block does not contain any code, will JIT compiler optimize that out by not locking on obj as it will be of no use? Java compiler does similar tricks such as Lock coarsening but will this synchronized block be also optimized out? EDIT: As per the point made by assylias, synchronized(new Object()){ //empty block } will the JIT compiler now be able to optimize this out,

Is it safe if more git commands are run on the same repo in parallel?

本小妞迷上赌 提交于 2019-11-30 04:43:28
I am interested if it is safe to run things like git push and git commit in parallel (for example in cron jobs, jenkins jobs etc.). Is there some locking mechanism built-in in git so these operations are serialized or this can corrupt the repository? Yes. Git works by writing references in a manner that allows for this. If you are doing a commit at the same time as a push, push will only go from the references down to the objects they contain. If the commit finishes and updates the branch reference on time, it will get pushed. If it doesn't, the old reference will be pushed. You won't get

Destination Array not long enough?

流过昼夜 提交于 2019-11-30 04:39:48
I have a class with the following method: public List<Bike> bikesCopy { get { List<Bike> bs; lock (_bikes) bs = new List<Bike>(_bikes); return bs; } } Which makes a copy of another list, private List<Bike> _bikes; The strange thing now is, that I get the following error: Destination array was not long enough. Check destIndex and length, and the array's lower bounds. What is the problem here? I would say the error lies in the object _bikes not being thread safe. As commented, somewhere there is a modify of the _bikes object that is not being lock'ed. It is a split second error where the

select_for_update in development Django

风流意气都作罢 提交于 2019-11-30 04:38:05
问题 The Django documentation states that: If you were relying on “automatic transactions” to provide locking between select_for_update() and a subsequent write operation — an extremely fragile design, but nonetheless possible — you must wrap the relevant code in atomic(). Is the reason why this no longer works is that autocommit is done at the database layer and not the application layer? Previously the transaction would be held open until a data-altering function is called: Django’s default

Threading in Java: How to lock an object?

断了今生、忘了曾经 提交于 2019-11-30 04:15:57
The following Function is executing in its own thread: private void doSendData() { try { //writeToFile(); // just a temporary location of a call InetAddress serverAddr = InetAddress.getByName(serverAddress); serverAddr.wait(60000); //Log.d("TCP", "C: Connecting..."); Socket socket = new Socket(serverAddr, portNumber); socket.setSoTimeout(3000); try { //Log.d("TCP", "C: Sending: '" + message + "'"); PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); String message = packData(); out.println(message); Log.d("TCP", "C: Sent."); Log.d(

Why doesn't Lock'ing on same object cause a deadlock? [duplicate]

孤街浪徒 提交于 2019-11-30 04:10:46
Possible Duplicate: Re-entrant locks in C# If I write some code like this: class Program { static void Main(string[] args) { Foo(); Console.ReadLine(); } static void Foo() { lock(_lock) { Console.WriteLine("Foo"); Bar(); } } static void Bar() { lock(_lock) { Console.WriteLine("Bar"); } } private static readonly object _lock = new object(); } I get as output: Foo Bar I expected this to deadlock, because Foo acquires a lock, and then waits for Bar to acquire the lock. But this doesn't happen. Does the locking mechanism simply allow this because the code is executed on the same thread? For the

Under what conditions can a thread enter a lock (Monitor) region more than once concurrently?

一个人想着一个人 提交于 2019-11-30 04:03:47
问题 (question revised): So far, the answers all include a single thread re-entering the lock region linearly, through things like recursion, where you can trace the steps of a single thread entering the lock twice. But is it possible somehow, for a single thread (perhaps from the ThreadPool, perhaps as a result of timer events or async events or a thread going to sleep and being awaken/reused in some other chunk of code separately) to somehow be spawned in two different places independently of

How to create a distributed lock with Redis?

拈花ヽ惹草 提交于 2019-11-30 03:42:30
On the redis documentation, I found a primitive lock can be implemented via SETNX: http://redis.io/commands/setnx C4 sends SETNX lock.foo in order to acquire the lock The crashed client C3 still holds it, so Redis will reply with 0 to C4. C4 sends GET lock.foo to check if the lock expired. If it is not, it will sleep for some time and retry from the start. Instead, if the lock is expired because the Unix time at lock.foo is older than the current Unix time, C4 tries to perform: GETSET lock.foo Because of the GETSET semantic, C4 can check if the old value stored at key is still an expired