locking

Is it good practice to use mkdir as file-based locking on linux?

与世无争的帅哥 提交于 2019-12-30 09:01:28
问题 I wanted to quickly implement some sort of locking in perl program on linux, which would be shareable between different processes. So I used mkdir as an atomic operation, which returns 1 if the directory doesn't exist and 0 if it does. I remove the directory right after the critical section. Now, it was pointed to me that it's not a good practice in general (independently on the language). I think it's quite OK, but I would like to ask your opinion. edit: to show an example, my code looked

Is it good practice to use mkdir as file-based locking on linux?

早过忘川 提交于 2019-12-30 09:01:20
问题 I wanted to quickly implement some sort of locking in perl program on linux, which would be shareable between different processes. So I used mkdir as an atomic operation, which returns 1 if the directory doesn't exist and 0 if it does. I remove the directory right after the critical section. Now, it was pointed to me that it's not a good practice in general (independently on the language). I think it's quite OK, but I would like to ask your opinion. edit: to show an example, my code looked

Thread safety object - static or not?

十年热恋 提交于 2019-12-30 08:14:19
问题 I was recently in an interview and the tech guy asked me about how to make an application thread-safe. Well, after explaining the lock() correctly, he said it is not a good idea to have the object as static. private static readonly object _syncLock = new object(); He claimed the reason is that static makes that object slower for threads to lock than if it was non static. Is this true? EDIT: Nonetheless I am still not sure. What is the difference between these three approaches? private static

create a lock file in bash to avoid duplicate execution

南笙酒味 提交于 2019-12-30 07:49:09
问题 I'm not very good on bash I've been modifying a code to create a lock file so a cron don't execute a second time if the first process hasn't finish. LOCK_FILE=./$(hostname)-lock (set -C; : > $LOCK_FILE) 2> /dev/null if [ $? != "0" ]; then echo "already running (lock file exists); exiting..." exit 1 fi trap 'rm $LOCK_FILE' INT TERM EXIT when I run it for the first time I get the message already running as if the file already existed. Perhaps I'm missing something 回答1: #!/bin/sh ( # Wait for

create a lock file in bash to avoid duplicate execution

人走茶凉 提交于 2019-12-30 07:49:08
问题 I'm not very good on bash I've been modifying a code to create a lock file so a cron don't execute a second time if the first process hasn't finish. LOCK_FILE=./$(hostname)-lock (set -C; : > $LOCK_FILE) 2> /dev/null if [ $? != "0" ]; then echo "already running (lock file exists); exiting..." exit 1 fi trap 'rm $LOCK_FILE' INT TERM EXIT when I run it for the first time I get the message already running as if the file already existed. Perhaps I'm missing something 回答1: #!/bin/sh ( # Wait for

What's wrong with this fix for double checked locking?

戏子无情 提交于 2019-12-30 06:47:53
问题 So I've seen a lot of articles now claiming that on C++ double checked locking, commonly used to prevent multiple threads from trying to initialize a lazily created singleton, is broken. Normal double checked locking code reads like this: class singleton { private: singleton(); // private constructor so users must call instance() static boost::mutex _init_mutex; public: static singleton & instance() { static singleton* instance; if(!instance) { boost::mutex::scoped_lock lock(_init_mutex); if(

Python: threading + lock slows my app down considerably

China☆狼群 提交于 2019-12-30 06:47:51
问题 Say I have a function that writes to a file. I also have a function that loops repeatedly reading from said file. I have both of these functions running in separate threads. (Actually I am reading/writing to registers via MDIO which is why I can't have both threads executing concurrently, only one or the other, but for the sake of simplicity, let's just say it's a file) Now when I run the write function in isolation, it executes fairly quickly. However when I'm running threaded and have it

Custom Lock Screen Delay When Wake

天涯浪子 提交于 2019-12-30 05:35:07
问题 I'm trying to make a custom lock screen app, but I'm not sure if I'm going about it the right way. I have a broadcast receiver that listens to when the screen is turned on and starts my lock screen activity. This receiver is registered inside a service, which also disables the default lock screen. The problem is, there is a slight delay between when the screen is turned on and the lock screen activity shows up. How would I go about doing it so that it shows up right away? My code for the

Why does row level locking not appear to work correctly in SQL server?

喜夏-厌秋 提交于 2019-12-30 03:34:05
问题 This is a continuation from When I update/insert a single row should it lock the entire table? Here is my problem. I have a table that holds locks so that other records in the system don’t have to take locks out on common resources, but can still queue the tasks so that they get executed one at a time. When I access a record in this locks table I want to be able to lock it and update it (just the one record) without any other process being able to do the same. I am able to do this with a lock

Cross-process read-write synchronization primative in .NET?

房东的猫 提交于 2019-12-30 03:29:05
问题 Is there a read/write locking mechanism that works across processes (similar to Mutex, but read/write instead exclusive locking)? I would like to allow concurrent read access, but exclusive write access. 回答1: No. As Richard noted above, there is no such out of the box mechanism in .NET. This is how to implement it using a mutex and a semaphore. Method #1 is described in http://www.joecheng.com/blog/entries/Writinganinter-processRea.html, quoting: // create or open global mutex GlobalMutex