locking

How to synchronize/lock correctly when using CountDownLatch

情到浓时终转凉″ 提交于 2019-12-12 12:16:06
问题 It boils down to one thread submitting job via some service. Job is executed in some TPExecutor. Afterwards this service checks for results and throw exception in original thread under certain conditions (job exceeds maximum number of retries, etc.). Code snippet below roughly illustrate this scenario in legacy code: import java.util.concurrent.CountDownLatch; public class IncorrectLockingExample { private static class Request { private final CountDownLatch latch = new CountDownLatch(1);

Postgres, update and lock ordering

怎甘沉沦 提交于 2019-12-12 12:04:18
问题 I'm working on Postgres 9.2. There are 2 UPDATEs, each in their own transactions. One looks like: UPDATE foo SET a=1 WHERE b IN (1,2,3,4); The other is similar: UPDATE foo SET a=2 WHERE b IN (1,2,3,4); These could possibly run at the same time and in reality have 500+ in the 'IN' expression. I'm sometimes seeing deadlocks. Is is true that that order of items in the 'IN' expression may not actually influence the true lock ordering? 回答1: Yes. I think the main issue here is that IN checks for

Prevent simultaneous transactions in a web application

二次信任 提交于 2019-12-12 11:30:50
问题 We have a web application (it is a game) with lots of various forms and elements which act as buttons and trigger some actions on server. The problem is that users can sometimes confuse our application if he clicks on buttons too fast or opens the website in two tabs and then issues some actions simultaneously. We have some basic protection - MySQL transactions, some double-click preventing Javascripts, but anyway sometimes something just skips out. Of course, the best way would be to

How do I get the handle for locking a file in Delphi?

可紊 提交于 2019-12-12 11:20:38
问题 The LockFile API takes a file handle. I normally use TStream for file access, so I'm unsure how to get the appropriate handle, given an ANSIString filename only. My purpose is to lock a file (which may not exist originally) during a process, write some information to other users, and then unlock and delete it. I would appreciate sample code or pointers to it to make this reliable. 回答1: You can use the LockFile function in conjunction with CreateFile and UnlockFile functions. See this example

How can I lock a directory in C on a linux machine

旧巷老猫 提交于 2019-12-12 10:46:36
问题 Will flock or lockf work on a directory? I there another way to lock a directory in C on a linux machine? 回答1: Yes, more info about using flock on file/directory can be found here 回答2: You can't open a directory for writing, so that means you can't get a write lock on it. Even if you could, please keep in mind that flock and fcntl and other kinds of POSIX locks are advisory, so they don't actually prevent software that doesn't respect the lock from doing things. Maybe you want to look at

Java: what happens when a new Thread is started from a synchronized block?

心不动则不痛 提交于 2019-12-12 10:39:41
问题 First question here: it is a very short yet fundamental thing in Java that I don't know... In the following case, is the run() method somehow executed with the lock that somemethod() did acquire? public synchronized void somemethod() { Thread t = new Thread( new Runnable() { void run() { ... <-- is a lock held here ? } } t.start(); ... (lengthy stuff performed here, keeping the lock held) ... } 回答1: No. run() starts in its own context, synchronization-wise. It doesn't hold any locks. If it

Why are the atomics much slower than the lock in this uncontended case?

回眸只為那壹抹淺笑 提交于 2019-12-12 10:05:10
问题 I wrote something using atomics rather than locks and perplexed at it being so much slower in my case I wrote the following mini test: #include <pthread.h> #include <vector> struct test { test(size_t size) : index_(0), size_(size), vec2_(size) { vec_.reserve(size_); pthread_mutexattr_init(&attrs_); pthread_mutexattr_setpshared(&attrs_, PTHREAD_PROCESS_PRIVATE); pthread_mutexattr_settype(&attrs_, PTHREAD_MUTEX_ADAPTIVE_NP); pthread_mutex_init(&lock_, &attrs_); } void lockedPush(int i); void

Invoking Method on UI thread from within a Lock()

若如初见. 提交于 2019-12-12 09:44:35
问题 I have two methods, MethodA & MethodB . MethodB has to run on the UI thread. I need them to run one after the other without allowing MethodC to run between them. MethodC is called when a user clicks on a lovely little button. What I did to ensure this is put a Lock around the code thus: lock (MyLock) { MethodA(param1, param2); MyDelegate del = new MyDelegate(MethodB); if (this.IsHandleCreated) this.Invoke(del); } And for MethodC : public void MethodC() lock (MyLock) { Do bewildering stuff....

How to give priority to certain queries?

纵然是瞬间 提交于 2019-12-12 09:37:57
问题 On certain occasions, when several back-end process happen to run at the same time (queue management is something else, I can solve it like that, but this is not the question here), I get General error: 1205 Lock wait timeout exceeded; try restarting transaction ROLLING BACK The process which has less priority is the one that locks the table, due to the fact that it started a few minutes before the high priority one. How do I give priority to a query over an already running process? Hope it

Timeout expired when querying SQL Server via NHibernate

ⅰ亾dé卋堺 提交于 2019-12-12 09:26:02
问题 I have an ASP.Net MVC application that uses SQL Server 2005 via NHibernate. I am getting getting the following error message sporadically: "System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean