locking

Exit critical region

僤鯓⒐⒋嵵緔 提交于 2019-12-10 20:28:53
问题 Consider several threads executing concurrently the following code: long gf = 0;// global variable or class member //... if (InterlockedCompareExchange(&gf, 1, 0)==0) // lock cmpxchg { // some exclusive code - must not execute in concurrent gf = 0; // this is ok ? or need //InterlockedExchange(&gf, 0); // [lock] xchg } Treat the code above as C-like pseudo-code, which will be translated more-or-less directly into assembly without the usual concessions to compiler optimizations such as re

Sequential asynchronous reads using Stream.BeginRead

ε祈祈猫儿з 提交于 2019-12-10 20:07:32
问题 I'm writing a class that exposes a subsection of a stream for reading. Since data may be read from multiple different subsections at the same time, only one operation may be active at any one time. I had the idea of locking the underlying stream before every operation. Is locking the stream around the BeginRead call sufficient to ensure that concurrent asynchronous reads from different positions in the underlying stream happen correctly? public sealed class SubStream : Stream { // ... public

MySQL select for update returns empty set even though a row exists

筅森魡賤 提交于 2019-12-10 19:52:36
问题 I'm seeing a strange issue with MySQL's "select for update". I am using version 5.1.45. I have two tables: mysql> show create table tag; +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------

How to ensure locking-order to avoid deadlock?

冷暖自知 提交于 2019-12-10 19:34:17
问题 Assume there are two objects of the following Account class - account1 and account2. And there are two threads T1 and T2. T1 is transferring amount 100 from account1 to account2 as follows: account1.transfer(account2, 100); Similarly, T2 is transferring amount 50 from account2 to account1: account2.transfer(account1, 50); The transfer() method is obviously prone to deadlock as two threads T1 and T2 would be trying to acquire lock in the reverse order. (Thread T1 will try acquiring lock on

SQLite synchronization when accessed by applications on different machines

£可爱£侵袭症+ 提交于 2019-12-10 19:07:27
问题 I'm wondering how SQLite implements it. Is it based on file-locking? Surely the entire DB isn't locked for every user that accesses it; that would be extremely inefficient. Is it based on multiple files or just one big file? Would be nice if someone could give a short overview of how synchronization and locking is done in sqlite, or, of course, provide a link to one. 回答1: Have a look at the SQL Lite FAQ and this on locking. Hope this helps. 来源: https://stackoverflow.com/questions/589675

How to wait during SELECT that pending INSERT commit?

岁酱吖の 提交于 2019-12-10 18:52:59
问题 I'm using PostgreSQL 9.2 in a Windows environment. I'm in a 2PC (2 phase commit) environment using MSDTC. I have a client application, that starts a transaction at the SERIALIZABLE isolation level, inserts a new row of data in a table for a specific foreign key value (there is an index on the column), and vote for completion of the transaction (The transaction is PREPARED). The transaction will be COMMITED by the Transaction Coordinator. Immediatly after that, outside of a transaction, the

How to make a shared resource thread-safe when using dependency injection?

大城市里の小女人 提交于 2019-12-10 18:43:22
问题 My current application is using single instance of an object as a global variable for many of the main components, which I understand is considered inferior to using dependency injection. I wish to make my applications open source in the future, but first I want to refactor the code to use the most recommended techniques for team collaboration so that other developers will be able to change my source code more easily. Example of a shared resource: In the CFML language, you have the Server

How Indices Cope with MVCC?

放肆的年华 提交于 2019-12-10 18:38:39
问题 Greetings Overflowers, To my understanding (and I hope I'm not right) changes to indices cannot be MVCCed. I'm wondering if this is also true with big records as copies can be costly. Since records are accessed via indices (usually), how MVCC can be effective ? Do, for e.g., indices keep track of different versions of MVCCed records ? Any recent good reading on this subject ? Really appreciated ! Regards 回答1: Index itself can have both the records which can be pruned before returning. So in

Java shared condition between classes throws IllegalMonitorStateException: null

混江龙づ霸主 提交于 2019-12-10 18:28:36
问题 I have structure something like this: Lock wrapper - is used to store lock, condition and an object from response public class LockWrapper{ private Lock lock; private Condition myCondition; private MyObject myObject; public LockWrapper(Lock lock, Condition myCondition) { this.lock = lock; this.myCondition = myCondition; } public Condition getMyCondition() { return myCondition; } public MyObject getMyObject() { return myObject; } public void setObject(MyObject myObject) { this.myObject =

Resource manager with ReentrantLocks

依然范特西╮ 提交于 2019-12-10 17:46:21
问题 I am trying to implement a resource handler class that assigns the resources (strings, stored in an array) to multiple clients that can try to acquire the lock on a set of resources and unlock them by an ID given by the lock method. I'm trying to use fair ReentrantReadWriteLock-s, one for each resource. I only see the log of the client. There are several problems, sometimes a thread won't stop requesting and acquiring resources, sometimes deadlock happens, and sometimes the releaseLock fails.