locking

.NET Threading - is lock needed for assignments

纵然是瞬间 提交于 2019-11-30 14:49:03
问题 I've got some multi threaded code I'd like to increase the performace of a bit, so I'm wondering if I can get rid of a lock. I've got a field member: private IList<ServerStatus> status; It's updated in a thread like so: status = GetUpdatedStatus(); And it's used in another thread like this: var currentStatus = status; So the question is, can the above yield any problems without locks around the two assignment statements ? I guess the only scenario I can see is currentStatus being null, but

Efficient transaction, record locking

点点圈 提交于 2019-11-30 14:42:18
I've got a stored procedure, which selects 1 record back. the stored procedure could be called from several different applications on different PCs. The idea is that the stored procedure brings back the next record that needs to be processed, and if two applications call the stored proc at the same time, the same record should not be brought back. My query is below, I'm trying to write the query as efficiently as possible (sql 2008). Can it get done more efficiently than this? CREATE PROCEDURE GetNextUnprocessedRecord AS BEGIN SET NOCOUNT ON; --ID of record we want to select back DECLARE @iID

Locking on an interned string?

旧街凉风 提交于 2019-11-30 14:39:09
问题 Update: It is acceptable if this method is not thread safe, but I'm interested in learning how I would make it thread safe. Also, I do not want to lock on a single object for all values of key if I can avoid it. Original Question: Suppose I want to write a higher order function that takes a key and a function, and checks if an object has been cached with the given key. If is has, the cached value is returned. Otherwise, the given function is run and the result is cached and returned. Here's a

Using WITH NOLOCK Table Hint in Query Using View - Does it Propagate Within the View?

Deadly 提交于 2019-11-30 14:35:27
问题 If a "WITH NOLOCK" query hint is used on a View in SQL Server, does it propagate that hint to the view definition itself, even if NOLOCK is NOT used for the raw tables in the View definition? The reason to need this is that sometimes the support staff wants to do huge time-consuming queries but would rather not force this lock on all queries using the view within the application itself. 回答1: Yes, NOLOCK will propagate to the tables used by the view definition (at least in SQL Server 2005).

What's the right pattern for waiting on a file lock to be released?

谁都会走 提交于 2019-11-30 14:22:14
I need to open a file but if it's currently not available I need to wait until it's ready. What's the best approach to take? SCENARIO I'm using files as a persistent caching mechanism for application data. This data needs to be read and deserialized often (written only once, and deleted occasionally). I have a cleanup process that runs on a separate thread that determines which files are no longer needed and deletes them. Opening and reading of files may happen concurrently (rarely, but could happen) and I want the process to wait and try to read the data again. Thanks! csharptest.net I'm not

Locking Row in SQL 2005-2008

被刻印的时光 ゝ 提交于 2019-11-30 13:57:55
Is there a way to lock a row in the SQL 2005-2008 database without starting a transaction, so other processes cannot update the row until it is unlocked? You can use RowLock or other hints but you should be careful .. The HOLDLOCK hint will instruct SQL Server to hold the lock until you commit the transaction. The ROWLOCK hint will lock only this record and not issue a page or table lock. The lock will also be released if you close your connection or it times out. I'd be VERY careful doing this since it will stop any SELECT statements that hit this row dead in their tracks. SQL Server has

Why should you lock threads?

时光怂恿深爱的人放手 提交于 2019-11-30 13:53:54
I've read a lot of examples on locking threads.. but why should you lock them? From my understanding, when you initiate threads without joining them, they will compete with the main thread and all other threads for resources and then execute, sometimes simultaneously, sometimes not. Does locking ensure that threads DON'T execute simultaneously? Also, what wrong with threads executing simultaneous? Isn't that even better? (faster overall execution) When you lock threads, will it lock them all or can you choose which ones you want to be locked? (Whatever locking actually does...) I'm referring

Synchronized stored procedure execution in mysql

谁都会走 提交于 2019-11-30 13:39:59
I have a stored procedure in mysql thats to perform a task that needs to be synchronized such that if two application calls the stored procedure, only one can access a section of code to perform the task, keeping the other one to get blocked until the first one finishes the task. DELIMITER $$ CREATE PROCEDURE SP_GEN_ID(IN NAME VARCHAR(20)) BEGIN DECLARE maxLen int default 0; START TRANSACTION; #the section of code that needs to be synchronized COMMIT END; $$ DELIMITER ; So, if two applications call the stored procedure simultaneously, the task has to be synchronized. a. But Start TRANSACTION

java: wait(), notify() and synchronized blocks

拥有回忆 提交于 2019-11-30 13:28:30
I learned that calling an Object's wait() method will release the object monitor, if present. But I have some questions regarding calling notify() on this object by another thread: (when) will the waiting thread wake up, if another (a 3rd) thread owns the object monitor in the meanwhile? will the waiting thread wake up, if a 3rd thread called wait() on this object? is it possible to determine if a thread is waiting for notifying a particular object (java 1.4/java 5) What's happening if wait() will be called in the finalize() method? notify will wake one thread waiting on the monitor. Unless

Put one thread to sleep until a condition is resolved in another thread

こ雲淡風輕ζ 提交于 2019-11-30 13:13:16
问题 Here are two chunks of code that accomplish (what I think is) the same thing. I basically am trying to learn how to use Java 1.5's concurrency to get away from Thread.sleep(long). The first example uses ReentrantLock, and the second example uses CountDownLatch. The jist of what I am trying to do is put one thread to sleep until a condition is resolved in another thread. The ReentrantLock provides a lock on the boolean I'm using to decide whether to wake the other thread or not, and then I use