locking

C# prevent access to all object methods from other threads

血红的双手。 提交于 2019-12-10 10:24:04
问题 I have an object that have to be used by only a single Thread at a time. For example my Object contains 3 methods A , B and C and I want to lock the object (all the methods/attributes are locked) if a Thread access the method A . The main difficultie is that I can't modify the code of that object. I have to prevent multithreads access where i'm calling the object. My first thought was to use the singleton pattern but i didn't manage to make it work! 回答1: If you can't change the code of the

mySQL - Apply a row level lock using mysqli

佐手、 提交于 2019-12-10 10:14:17
问题 Using PHP's mysqli how do you apply a row level lock? Row level locks stop anyone editing currently present rows that match your criteria right? but do they stop a user inserting a row which matches your criteria? Thanks 回答1: if you want to lock a specific row from editing, use FOR UPDATE at the end of a SELECT query. this locks the row in your transaction and prevents other users from updating it. this only works in transactional storage engines like innodb. in answer to your questions, yes,

In Hazelcast, is it possible to use clustered locks that do _not_ care about the local thread that performs the lock/unlock operations?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 09:31:55
问题 Hazelcast locks (such as http://www.hazelcast.com/docs/1.9.4/manual/multi_html/ch02s07.html) as I understand it behave the same way as the Java concurrency primitives but across the cluster. The makes it possible to use to synchronize between thread in the local process as well as over the cluster. However, is there any way I can opt out of this behaviour? In my current project, I need a way of coordinating unique ownership of a resource across the cluster but want to aquire and release this

Need to beat the GC and have object destroyed once it goes out of scope

依然范特西╮ 提交于 2019-12-10 09:24:54
问题 I have several sections of code that I need to protect with a Mutex. The problem is that the code looks something like this: lock(mylockobject) { if(!foo()) throw new MyException("foo failed"); if(!bar()) throw new MyException("bar failed"); } Using lock, it works as I'd like, but now I need to use a mutex. The obvious problem here is that if I acquire the mutex and foo() or bar() fails, I would have to explicity release the mutex before throwing each exception. In C++, I would take advantage

Making pthread_rwlock_wrlock recursive

断了今生、忘了曾经 提交于 2019-12-10 05:26:56
问题 I have a problem regarding the behaviour of the pthread function pthread_rwlock_wrlock. The specification linked above states that when one thread has locked the lock for writing and the same thread locks it again, it results in undefined behaviour (I could actually observe this in that on x86 Linux calling this function is a noop and on PowerPC Linux it stalls the thread). The behaviour I need would be a read write lock that has the following characteristics: read-locking by a thread

Bug in PostgreSQL locking mechanism or misunderstanding of the mechanism

三世轮回 提交于 2019-12-10 03:28:39
问题 We encountered an issue with PostgreSQL 9.0.12 locking mechanism. This is our minimal code to reproduce the issue: Scenario Transaction 1 Transaction 2 BEGIN BEGIN ...... select trees for update; update apples; --passes update apples; -- stuck! reproduce code: If you want to try it in your PostgreSQL - here is a code you can copy/paste. I have a following db schema: CREATE TABLE trees ( id integer primary key ); create table apples ( id integer primary key, tree_id integer references trees(id

What is the difference between lock(this) and lock(thisLock)?

。_饼干妹妹 提交于 2019-12-10 03:14:48
问题 I'm reading lock Statement (C# Reference) where I saw this code: class Account { private Object thisLock = new Object(); //... int Withdraw(int amount) { lock (thisLock) { //.... } } //... } I'm wondering if it would make any difference if we write lock(this) instead of lock(thisLock) in the above example. Please see the complete example if your answer depends on it. If you think there is indeed some difference between lock(this) and lock(thisLock) , then please help me understanding the

Difference between FOR UPDATE OF and FOR UPDATE

依然范特西╮ 提交于 2019-12-10 02:58:40
问题 What makes difference, when I use FOR UPDATE OF SAL or simply write FOR UPDATE . According to O'Reilly The OF list of the FOR UPDATE clause does not restrict you to changing only those columns listed. Locks are still placed on all rows; the OF list just gives you a way to document more clearly what you intend to change. If you simply state FOR UPDATE in the query and do not include one or more columns after the OF keyword, then the database will then lock all identified rows across all tables

Test a lock with out acquiring it?

ε祈祈猫儿з 提交于 2019-12-10 02:32:05
问题 I have objects, they get locks. I want to test if they are locked without acquiring a lock. The idea is if I TryEnter() then i have to Exit() if true to only check the lock correctly. Seems like a really basic question, how is it done? 回答1: What possible information can you get from knowing the lock was unlocked back when you looked at it? By the time you make a decision based on that information, the lock may be already taken. 回答2: Because the lock statement is equivalent to: System

Do I need to synchronize thread access to an int?

流过昼夜 提交于 2019-12-10 00:44:44
问题 I've just written a method that is called by multiple threads simultaneously and I need to keep track of when all the threads have completed. The code uses this pattern: private void RunReport() { _reportsRunning++; try { //code to run the report } finally { _reportsRunning--; } } This is the only place within the code that _reportsRunning 's value is changed, and the method takes about a second to run. Occasionally when I have more than six or so threads running reports together the final