locking

Iterate over STL container using indices safe way to avoid using locks?

空扰寡人 提交于 2020-01-05 05:26:07
问题 Wondering if it's safe to iterate over a STL container such as a vector in the following manner to avoid locking on reads/writes but allowing for push_back() operations only by any "writing" threads. for (size_t i = 0; i < vec.size(); i++) { const T& t = *vec[i]; // do something with t } I understand that iterators can be invalidated by changes to the container but perhaps if we make sure the initial container size is large enough for any future additions, it should also be safe to iterate

Release multiple locks atomically in Java

不羁的心 提交于 2020-01-05 05:24:58
问题 If I have acquired a number of locks, how do I release the Set of locks atomically? 回答1: As locks are treated as independend objects (see this question) and the Java language does not (yet, as of my knowledge) provide a method in its JVM (where it would have to be implemented to be really atomically) to release multiple locks in one step, it is simply not possible. Maybe it is possible to redesign your application so that an atomic release of several locks is not needed anymore. (Maybe you

Distributed Lock Service over MySql/GigaSpaces/Netapp [closed]

冷暖自知 提交于 2020-01-05 05:09:26
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . Disclaimer : I already asked this question, but without the deployment requirement. I got an answer that got 3 upvotes, and when I edited the question to include the deployment requirement the answer then became irrelevant. The reason I'm resubmitting is because SO considers the

Guaranteed memcached lock

青春壹個敷衍的年華 提交于 2020-01-05 04:03:51
问题 So, I'm trying to implement a distributed lock using memcached and add()'s store only if does not exist contract (Java & spymemcached, but applicable in any language of course). Of course, if an instance goes away, then we lose the lock so the thought was add the lock 3 times (e.g. MyLock1, MyLock2, MyLock3) which will very likely hash out to 3 different instances. But, I've realized that if an instances goes down the hash then obviously changes (using spymemcached's Redistribute failure mode

Ordered execute threads

一笑奈何 提交于 2020-01-05 03:32:10
问题 I have an console application which interact another user interface. Interface sends commands and my application should to process them. It is important to keep listening my console application while processing and execute commands in ordered. So i listen interface on main thread and execute commands in another thread. Below example is what i am trying and problem is execution threads are not ordered. Second thing is i am using lock in ProcessCommand method but i am not sure it is safe or not

synchronizing access to allocated memory

我的梦境 提交于 2020-01-05 02:33:27
问题 Is there a way to synchronize access to each element in an allocated memory. For example, if I allocate memory using the following code int* counters = new int[10]; is there a way to synchronize modification of each counter separately (being able to modify counters[0], counters[1]...counters[9] at the same time) so that modification of, let's say, counters[0] won't block counters[9] until the lock is released to update counters[9] and the other counters while a thread is updating a specific

.net file random access recoard locking

独自空忆成欢 提交于 2020-01-05 01:35:11
问题 Summary: In .net When locking a record in a random access file we cannot access records ahead of the locked record in the file. To demonstrate the issue I have written two simple programs one opens and locks a record and the other tries to read through. The results are that when locking record number 9 out of the 10 in the first program we are able to read records 1 and 2 but no more! The expectation (And this is our experience with VB6) is that you should be able to read all the records

Protecting critical code from being called again

我怕爱的太早我们不能终老 提交于 2020-01-04 23:02:01
问题 I need to protect a critical area of my code, which is multi-threaded. I want to prevent it from being called multiple times before the other thread is finished. This is what I am working with: - (void) filterAllEventsIntoDictionary{ // start critical area if (self.sortedKeys.count != 0) { [self.sortedKeys removeAllObjects]; } dispatch_async(self.filterMainQueue, ^{ [self internal_filterAllEventsIntoDictionary]; dispatch_sync(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); });

Protecting critical code from being called again

丶灬走出姿态 提交于 2020-01-04 23:01:08
问题 I need to protect a critical area of my code, which is multi-threaded. I want to prevent it from being called multiple times before the other thread is finished. This is what I am working with: - (void) filterAllEventsIntoDictionary{ // start critical area if (self.sortedKeys.count != 0) { [self.sortedKeys removeAllObjects]; } dispatch_async(self.filterMainQueue, ^{ [self internal_filterAllEventsIntoDictionary]; dispatch_sync(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); });

How to move money with MongoDB?

那年仲夏 提交于 2020-01-04 04:45:46
问题 Account has embedded Transactions amount (positive for received transactions, negative for outgoing transactions) User wants to send money. We need to compute account's balance to check if there is enough money. In pseudocode: send_money(amount) balance = sum(account's all transactions) // Mongo Query 1 if (amount <= balance) add a new transaction to account // Mongo Query 2 But isn't it possible that two concurrent mongo connections both pass Query 1 and proceed to make Query 2? Say user's