locking

Spinlocks, How Useful Are They?

半腔热情 提交于 2019-11-29 20:26:33
How often do you find yourself actually using spinlocks in your code? How common is it to come across a situation where using a busy loop actually outperforms the usage of locks? Personally, when I write some sort of code that requires thread safety, I tend to benchmark it with different synchronization primitives, and as far as it goes, it seems like using locks gives better performance than using spinlocks. No matter for how little time I actually hold the lock, the amount of contention I receive when using spinlocks is far greater than the amount I get from using locks (of course, I run my

What is the difference between Lock and RLock

戏子无情 提交于 2019-11-29 20:25:59
From the docs : threading.RLock() -- A factory function that returns a new reentrant lock object. A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again without blocking; the thread must release it once for each time it has acquired it. I am not sure why do we need this? what's the difference between Rlock and Lock ? The main difference is that a Lock can only be acquired once. It cannot be acquired again, until it is released. (After it's been released, it can be re-acaquired by any thread). An RLock

When and how to use Python's RLock

巧了我就是萌 提交于 2019-11-29 19:43:31
Reading through the Python docs I came across RLock . Can someone explain to me (with example) a scenario in which RLock would be preferred to Lock ? With particular reference to: RLock 's “recursion level”. How is this useful? A threads "ownership" of an RLock object Performance? User This is one example where I see the use: Useful when you want to have thread-safe access from outside the class and use the same methods from inside the class: class X: def __init__(self): self.a = 1 self.b = 2 self.lock = threading.RLock() def changeA(self): with self.lock: self.a = self.a + 1 def changeB(self)

How to prevent screen lock on my application with swift on IOS

断了今生、忘了曾经 提交于 2019-11-29 19:13:30
How can I prevent screen lock only when using Navigation. Waze has the option to do that, how can I do this in my App? atwalsh Use this: Objective-C: [[UIApplication sharedApplication] setIdleTimerDisabled: YES]; Swift (legacy): UIApplication.sharedApplication().idleTimerDisabled = true Swift 3/4: UIApplication.shared.isIdleTimerDisabled = true Make sure to import UIKit . Here is the link to the documentation from apple.developer.com. For Swift 3.0 here are two options depending on where you want to invoke the code: Inside AppDelegate.swift: application.idleTimerDisabled = true Outside

Pure-Ruby concurrent Hash

拈花ヽ惹草 提交于 2019-11-29 18:44:59
What's the best way to implement a Hash that can be modified across multiple threads, but with the smallest number of locks. For the purposes of this question, you can assume that the Hash will be read-heavy. It must be thread-safe in all Ruby implementations, including ones that operate in a truly simultaneous fashion, such as JRuby, and it must be written in pure-Ruby (no C or Java allowed). Feel free to submit a naïve solution that always locks, but that isn't likely to be the best solution. Points for elegance, but a smaller likelihood of locking wins over smaller code. ara t howard Okay,

How lock by method parameter?

故事扮演 提交于 2019-11-29 18:29:51
问题 string Get(string key){ lock(_sync){ // DoSomething } } If DoSomething depend only on key, I want key dependent lock. I think it may be dictionary with sync objects. Is there any complete solution? Something like real example What is the best way to lock cache in asp.net? 回答1: Well, you could create a Dictionary<string, object> and lazily populate it with objects to lock on. For example: readonly Dictionary<string, object> dictionary = new Dictionary<string, object>(); readonly object

How to use tcp_keepalives settings in Postgresql?

大城市里の小女人 提交于 2019-11-29 17:49:20
问题 Postgresql has 3 keepalive settings for managing dropped connections (in postgresql.conf): tcp_keepalives_count tcp_keepalives_idle tcp_keepalives_interval By default these are 0. The behavior I would like is for Postgresql to drop client connections after a period of time, should the client lose its network connection or go to sleep. I am currently using these values: tcp_keepalives_count = 1 tcp_keepalives_idle = 60 tcp_keepalives_interval = 60 I am running PostgreSQL 8.4 on Mac OS X, but

How to properly lock a value type?

本秂侑毒 提交于 2019-11-29 17:29:59
问题 I was reading about threading and about locking. It is common practise that you can't (well should not) lock a value type. So the question is, what is the recommended way of locking a value type? I know there's a few ways to go about doing one thing but I haven't seen an example. Although there was a good thread on MSDN forums but I can't seem to find that now. Thanks 回答1: Use another object for the lock. int valueType; object valueTypeLock = new object(); void Foo() { lock (valueTypeLock) {

PHP: How do I avoid reading partial files that are pushed to me with FTP?

你说的曾经没有我的故事 提交于 2019-11-29 17:04:03
问题 Files are being pushed to my server via FTP. I process them with PHP code in a Drupal module. O/S is Ubuntu and the FTP server is vsftp. At regular intervals I will check for new files, process them with SimpleXML and move them to a "Done" folder. How do I avoid processing a partially uploaded file? vsftp has lock_upload_files defaulted to yes. I thought of attempting to move the files first, expecting the move to fail on a currently uploading file. That doesn't seem to happen, at least on

Best way to find SQL Locks in SQL Server 2008

早过忘川 提交于 2019-11-29 16:42:46
问题 What is the best way to find the SQL locks along wih the user associated with that lock in SQL Server 2008? 回答1: select * from sys.dm_tran_locks will list all current locks, granted or pending, along with the requesting session id. select * from sys.dm_exec_sessions will list all current sessions, including the client host and login name. But going this way is very seldom what you want. For a more digestible form, use the Activity Monitor and watch the blocking as reported there. 回答2: Run