locking

Reader/Writer Locks in C++

泄露秘密 提交于 2019-12-17 02:09:11
问题 I'm looking for a good reader/writer lock in C++. We have a use case of a single infrequent writer and many frequent readers and would like to optimize for this. Preferable I would like a cross-platform solution, however a Windows only one would be acceptable. 回答1: Newer versions of boost::thread have read/write locks (1.35.0 and later, apparently the previous versions did not work correctly). They have the names shared_lock, unique_lock, and upgrade_lock and operate on a shared_mutex. 回答2:

Select top N with “for update skip locked” in Oracle

若如初见. 提交于 2019-12-14 03:45:19
问题 In Oracle, I can select the top 1 message in a sorted table with select messageid from( select messageid, RANK() over (order by messageid asc) as msg_rank from messages ) where msg_rank=1; And as I discovered in a previous question I can select a row exclusively with select * from messages where rownum < 2 for update skip locked; However I can't merge these two concepts together select messageid from( select messageid, RANK() over (order by messageid asc) as msg_rank from messages ) where msg

Multi-threaded Objective-C accessors: GCD vs locks

会有一股神秘感。 提交于 2019-12-14 03:39:41
问题 I'm debating whether or not to move to a GCD-based pattern for multi-threaded accessors. I've been using custom lock-based synchronization in accessors for years, but I've found some info (Intro to GCD) and there seems to be pros of a GCD-based approach. I'm hoping to start a dialog here to help myself and others weigh the decision. The pattern looks like: - (id)something { __block id localSomething; dispatch_sync(queue, ^{ localSomething = [something retain]; }); return [localSomething

.NET Multithreading: Lock object with log when locked for to much time

那年仲夏 提交于 2019-12-14 02:19:11
问题 In system we have methods that lock an object by specific params. As implementation, we have LockManager with Enter method that receives a key for a lock, check if lock object exists in internal dictionary, if not, it creates it and then locks. What I want to do, is to set "X expected time" for specific lock, and if an object was locked for more that X time, I want to write a message to our log. Below is a source code for my lock manager: public class MyLockManager<T> { protected Dictionary<T

How to unlock boost::upgrade_to_unique_lock (made from boost::shared_mutex)?

旧时模样 提交于 2019-12-14 01:28:41
问题 So I had some shared_mutex and done this: boost::upgrade_lock<boost::shared_mutex> lock(f->mutex); boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock); now I want to "unlock it" or at least downgrade it to something like: boost::shared_lock<boost::shared_mutex> lock_r(f->mutex); How to do such thing? Is it possible? 回答1: If you let the upgrade_to_unique_lock go out of scope, it will automatically downgrade back to upgrade ownership. For example void foo() { boost::upgrade_lock

Concurrent file usage in C#

坚强是说给别人听的谎言 提交于 2019-12-13 20:15:39
问题 I have one application that will read from a folder and wait for a file to appear in this folder. When this file appear, the application shall read the content, execute a few functions to external systems with the data from the file and then delete the file (and in turn wait for next file). Now, I want to run this application on two different machines but both listen in the same folder. So it’s the exact same application but two instances. Let’s call it instance A and instance B. So when a

Synchronized and locks in singleton factory

痞子三分冷 提交于 2019-12-13 16:52:13
问题 I have a singleton factory (edit: renamed "loader" to avoid confusion with factory pattern) that creates objects (in my example DAOs) or returns them if already created: public class DAOLoader { private static final DAOLoader INSTANCE = new DAOLoader(); private UserDAO userDAO; private MessageDAO messageDAO; private final Object lockUserDAO = new Object(); private final Object lockMessageDAO = new Object(); private DAOLoader() {} public static DAOLoader getInstance() { return INSTANCE; }

Using the lock statement

耗尽温柔 提交于 2019-12-13 16:23:32
问题 I have a Bitmap that I'm trying to save and I keep getting the "Object is currently in use elsewhere" error. Now I know GDI+ sucks with threading and that I'm supposed to use the lock{} statement but it's not working. My code is below, what am I doing incorrectly? Bitmap bitmap = new Bitmap(); lock (bitmap) { bitmap.Save([filepath], ImageFormat.Png); } 回答1: You should lock other dummy object: var objectForLock = new object() lock (objectForLock) { Bitmap bitmap = new Bitmap(); bitmap.Save(

Java synchronize statement around a lock

ぃ、小莉子 提交于 2019-12-13 15:26:56
问题 I was wondering if synchronize (lock) { ... } Where lock is an instance of java.util.concurrent.locks.Lock , treats lock like any other object or as the try-finally idiom i.e. lock.lock(); try { ... } finally { lock.unlock(); } 回答1: Lock documentation: Note that Lock instances are just normal objects and can themselves be used as the target in a synchronized statement. Acquiring the monitor lock of a Lock instance has no specified relationship with invoking any of the lock() methods of that

Thread-Safe lazy instantiating using MEF

血红的双手。 提交于 2019-12-13 13:18:32
问题 // Member Variable private static readonly object _syncLock = new object(); // Now inside a static method foreach (var lazyObject in plugins) { if ((string)lazyObject.Metadata["key"] = "something") { lock (_syncLock) { // It seems the `IsValueCreated` is not up-to-date if (!lazyObject.IsValueCreated) lazyObject.value.DoSomething(); } return lazyObject.value; } } Here I need synchronized access per loop. There are many threads iterating this loop and based on the key they are looking for, a