locking

Boost named_mutex and remove() command

[亡魂溺海] 提交于 2019-11-30 19:32:32
I have a class which can be created by multiple threads. But at one function the code needs to be protected, so I decided to use the boost interprocess mutex. Every class creates or opens the same Mutex in it's constructor: MyClass::MyClass() { boost::interprocess::named_mutex m_Lock( boost::interprocess::open_or_create, "myLock" ); } So now there comes the point where the critical code part is called: int MyClass::MyFunction() { boost::interprocess::scoped_lock<boost::interprocess::named_mutex> lock( m_Lock, boost::interprocess::try_to_lock); if(!lock) { return -1; } // else do some stuff

Under what conditions can a thread enter a lock (Monitor) region more than once concurrently?

喜夏-厌秋 提交于 2019-11-30 19:26:51
(question revised): So far, the answers all include a single thread re-entering the lock region linearly, through things like recursion, where you can trace the steps of a single thread entering the lock twice. But is it possible somehow, for a single thread (perhaps from the ThreadPool, perhaps as a result of timer events or async events or a thread going to sleep and being awaken/reused in some other chunk of code separately) to somehow be spawned in two different places independently of each other, and hence, run into the lock re-entrance problem when the developer didn't expect it by

Update MySQL table in chunks

∥☆過路亽.° 提交于 2019-11-30 18:56:56
问题 I am trying to update a MySQL InnoDB table with c. 100 million rows. The query takes close to an hour, which is not a problem. However, I'd like to split this update into smaller chunks in order not to block table access. This update does not have to be an isolated transaction. At the same time, the splitting of the update should not be too expensive in terms of additional overhead. I considered looping through the table in a procedure using : UPDATE TABLENAME SET NEWVAR=<expression> LIMIT

File locks for linux

半腔热情 提交于 2019-11-30 18:51:02
I tried using temp files: char *temp = tempnam(NULL, "myapp_"); printf("Tempname: %s", temp) // Prints /tmp/myapp_random while (1) { } But when I check /tmp (while the app is still running), the myapp_random is not there! As for using File Locks, I can't get a good grasp on it, I tried looking at <fcntl.h> but it seems to focus on locks in specified portions of a file. I just want to use the file entirely as a lock (which is why I prefer trying the temp file approach). Any ideas? tempnam doesn't create the file, it just gives you a filename that didn't exist at the time you called it. You

SQL Server, insert one row locks whole table

╄→гoц情女王★ 提交于 2019-11-30 18:23:23
We have an issue with some deadlock and I posted this question . With some help and a lot of searching myself I believe I figured out what is going on. In order to solve the deadlocks without controlling lock escalation I need to understand why sql server locks the whole table on inserting one row. Here is my insert statement (with renamed variables): DECLARE @Type1 INT = 11, @Type2 INT = NULL, @Value1 VARCHAR(20) = '0', @Value2 VARCHAR(20) = '0', @Value3 VARCHAR(20) = '0', @Value4 VARCHAR(20) = '0', @Date1 DATETIME = '2011-11-25', @Date2 DATETIME = '2011-11-25', @Value5 NVARCHAR(50) = '',

import inside of a Python thread

血红的双手。 提交于 2019-11-30 18:14:47
I have some functions that interactively load python modules using __import__ I recently stumbled upon some article about an "import lock" in Python, that is, a lock specifically for imports (not just the GIL). But the article was old so maybe that's not true anymore. This makes me wonder about the practice of importing in a thread. Are import / __import__ thread safe? Can they create dead locks? Can they cause performance issues in a threaded application? EDIT 12 Sept 2012 Thanks for the great reply Soravux. So import are thread safe, and I'm not worrying about deadlocks, since the functions

Making a password lock for an app?

北城以北 提交于 2019-11-30 18:02:07
问题 I'm wanting to make a password unlock screen for my app, and I'm not sure how I'd go about it. I'm wanting it to look like the Apple-designed version of it, which is the passcode lock setting screen. How might I go about doing something like this, where as soon as all four digits are entered the code is immediately checked against a pre-set password? Thanks! 回答1: most likely you create the view and when all the fields are set you check against the known password or you check the hash of the

Java File Locking

随声附和 提交于 2019-11-30 18:00:00
I have several threads (some of which are spawned by Process X, others by Process Y, et cetera ), and each thread needs to write to a file MyFile . However, if Thread T1 starts writing to MyFile first, then, when Thread T2 starts writing, it needs to wait for T1 to release the file , so that it can read the contents that were written in Thread T1 . In other words, each thread would have a finalizeThread method, like so: private void finalizeThread() { File f = new File("MyFile.dat"); f.createNewFile(); // atomically creates the file, if it doesn't exist locked_section { readContentsFromFile(f)

How do I copy a file or folder that is locked under windows programmatically?

坚强是说给别人听的谎言 提交于 2019-11-30 17:50:25
What are the API calls to copy a file that is currently locked. I'm hoping to be able to use .Net, but Win32 calls would be fine as well. Please feel free to chime in about the same functionality on Unix, or any other OS. You can use the VSS (Volume Shadow Copy Service, not Visual SourceSafe) API for this purpose. While powerful, this isn't exactly an easy-to-use API: the Overview of Processing a Backup Under VSS should give you an idea what's involved. Even though it's a relatively recent API, .NET support for VSS is pretty much (and inexcusably) nonexistent. You can't call most of the API

Locking Linux Serial Port

本秂侑毒 提交于 2019-11-30 17:41:40
问题 I have an issue that I'm trying to solve regarding the serial port in Linux. I'm able to open, read from, and close the port just fine. However, I want to ensure that I am the only person reading/writing from the port at any given time. I thought that this was already done for me after I make the open() function call. However, I am able to call open() multiple times on the same port in my program. I can also have two threads which are both reading from the same port simultaneously. I tried