locking

In Java can I depend on reference assignment being atomic to implement copy on write?

混江龙づ霸主 提交于 2019-12-17 18:41:55
问题 If I have an unsynchronized java collection in a multithreaded environment, and I don't want to force readers of the collection to synchronize [1] , is a solution where I synchronize the writers and use the atomicity of reference assignment feasible? Something like: private Collection global = new HashSet(); // start threading after this void allUpdatesGoThroughHere(Object exampleOperand) { // My hypothesis is that this prevents operations in the block being re-ordered synchronized(global) {

How to lock on an integer in C#?

旧巷老猫 提交于 2019-12-17 18:26:34
问题 Is there any way to lock on an integer in C#? Integers can not be used with lock because they are boxed (and lock only locks on references). The scenario is as follows: I have a forum based website with a moderation feature. What I want to do is make sure that no more than one moderator can moderate a post at any given time. To achieve this, I want to lock on the ID of the post. I've had a couple of ideas so far (e.g. using a dictionary<int, object>), but I'm looking for a better and cleaner

Oracle select for update behaviour

戏子无情 提交于 2019-12-17 18:18:11
问题 The problem we try to solve looks like this. We have a table full of rows which represent cards. The purpose of reservation transaction is to assign a card to a client A card can not belong to many clients After some time (if it is not bought) a card has to be returned to the pool of available resurces Reservation can be done by many clients at the same time We use Oracle database for storing the data, so solution has to work at least on Oracle 11 Our solution is to assign a status to the

Why is ConcurrentBag<T> so slow in .Net (4.0)? Am I doing it wrong?

六月ゝ 毕业季﹏ 提交于 2019-12-17 17:43:08
问题 Before I started a project, I wrote a simple test to compare the performance of ConcurrentBag from (System.Collections.Concurrent) relative to locking & lists. I am extremely surprised that ConcurrentBag is over 10 times slower than locking with a simple List. From what I understand, the ConcurrentBag works best when the reader and writer is the same thread. However, I hadn't thought it's performance would be so much worse than traditional locks. I have run a test with two Parallel for loops

How to avoid race condition when using a lock-file to avoid two instances of a script running simultaneously?

北城余情 提交于 2019-12-17 17:38:43
问题 A typical approach to avoid two instances of the same script running simultaneously looks like this: [ -f ".lock" ] && exit 1 touch .lock # do something rm .lock Is there a better way to lock on files from a shell-script, avoiding a race condition? Must directories be used instead? 回答1: Yes, there is indeed a race condition in the sample script. You can use bash's noclobber option in order to get a failure in case of a race, when a different script sneaks in between the -f test and the touch

std::lock_guard or std::scoped_lock?

时光总嘲笑我的痴心妄想 提交于 2019-12-17 17:29:29
问题 C++17 introduced a new lock class called std::scoped_lock. Judging from the documentation it looks similar to the already existing std::lock_guard class. What's the difference and when should I use it? 回答1: The scoped_lock is a strictly superior version of lock_guard that locks an arbitrary number of mutexes all at once (using the same deadlock-avoidance algorithm as std::lock ). In new code, you should only ever use scoped_lock . The only reason lock_guard still exists is for compatibility.

std::lock_guard or std::scoped_lock?

社会主义新天地 提交于 2019-12-17 17:28:27
问题 C++17 introduced a new lock class called std::scoped_lock. Judging from the documentation it looks similar to the already existing std::lock_guard class. What's the difference and when should I use it? 回答1: The scoped_lock is a strictly superior version of lock_guard that locks an arbitrary number of mutexes all at once (using the same deadlock-avoidance algorithm as std::lock ). In new code, you should only ever use scoped_lock . The only reason lock_guard still exists is for compatibility.

how to prevent the Image.FromFile() method to lock the file

青春壹個敷衍的年華 提交于 2019-12-17 16:21:37
问题 I am using following code to put JPG's into a DataGridView 's Image cell. If strFileName.ToLower.EndsWith(".jpg") Then Dim inImg As Image = Image.FromFile(strFileName) DataGridView4.Rows.Add() DataGridView4.Rows(DataGridView4.Rows().Count - 1).Cells(0).Value = inImg End If The problem is that I need to save this file from within the program, but i get the message that the file is beeing used by another program . So i tried to add inImg.Dispose() before the end if, but then the program doesnt

android unlock screen intent?

社会主义新天地 提交于 2019-12-17 12:31:19
问题 Is there an intent that is fired when a user unlocks their screen? I want my app to adjust the brightness when the screen turns on, but the problem im running into is that the screen on intent is fired on the lock screen and it does not adjust the display on that screen. 回答1: Look at the disableKeyguard method in the KeyguardLock class. 回答2: Yes, the ACTION_USER_PRESENT is broadcasted after the user unlocks: http://developer.android.com/reference/android/content/Intent.html#ACTION_USER

MySQL 'select for update' behaviour

最后都变了- 提交于 2019-12-17 10:44:39
问题 As per the MySql documentation, MySql supports Multiple granularity locking(MGL). case-1 Opened terminal-1: // connected to mysql mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> select id, status from tracking_number limit 5 for update; +----+--------+ | id | status | +----+--------+ | 1 | 0 | | 2 | 0 | | 3 | 0 | | 4 | 0 | | 5 | 0 | +----+--------+ 5 rows in set (0.00 sec) mysql> left it opened and opened terminal-2: // connected to mysql mysql> start transaction; Query