locking

Having issues with flock() function

懵懂的女人 提交于 2019-12-01 14:03:04
I have a question about how flock() works, particularly in python. I have a module that opens a serial connection (via os.open() ). I need to make this thread safe. It's easy enough making it thread safe when working in the same module using threading.Lock() , but if the module gets imported from different places, it breaks. I was thinking of using flock() , but I'm having trouble finding enough information about how exactly flock works. I read that flock() unlocks the file once the file is closed. But is there a situation that will keep the file open if python crashes? And what exactly is

Why doesn't C# allow a null value to be locked?

江枫思渺然 提交于 2019-12-01 13:40:14
问题 C# doesn't allow locking on a null value. I suppose I could check whether the value is null or not before I lock it, but because I haven't locked it another thread could come along and make the value null! How can I avoid this race condition? 回答1: Lock on a value that is never null, e.g. Object _lockOnMe = new Object(); Object _iMightBeNull; public void DoSomeKungFu() { if (_iMightBeNull == null) { lock (_lockOnMe) { if (_iMightBeNull == null) { _iMightBeNull = ... whatever ...; } } } } Also

Do I need to add some locks or synchronization if there is only one thread writing and several threads reading?

与世无争的帅哥 提交于 2019-12-01 13:03:10
Say I have a global object: class Global { public static int remoteNumber = 0; } There is a thread runs periodically to get new number from remote, and updates it (only write): new Thread { @override public void run() { while(true) { int newNumber = getFromRemote(); Global.remoteNumber = newNumber; Thread.sleep(1000); } } } And there are one or more threads using this global remoteNumber randomly (only read): int n = Global.remoteNumber; doSomethingWith(n); You can see I don't use any locks or synchronize to protected it, is it correct? Is there any potential issue that might cause problems?

Java file locking mechanism for file based process communication

拟墨画扇 提交于 2019-12-01 12:16:20
问题 I have two java process (JAR) one is writing to a text file on every 1 min and another is reading that file and call a web service to store data in database. Is there any way to lock the file when it is on write mode? I have observed that when wvdial is dialing a modem its create a lock file in /var/lock/ttyUSB0..LOCK I think. I want a this kind of procedure if the file is on write mode the another process could wait till write done. After writing the process can read the file content. Please

C# System.Timers.Timer odd behavior?

≯℡__Kan透↙ 提交于 2019-12-01 11:11:33
My goal is to write a code snippet that lets me have an exclusive access to an object (f.ex a txt file) in concurrent environment. With this in mind I was testing the simple program built on use of two System.Timers timers. Event handlers of both timers share the same lock object (Please see the code below). The timers start simultaneously with different interval, 3s for timer1 and 1s for timer2. Timer1 supposed to work only for one cycle, during which it's event handler will sleep for 10s and thus keeping the lock. What's surprised me is that when the lock released, I don't get all stacked in

HttpApplicationState - Why does Race condition exist if it is thread safe?

我与影子孤独终老i 提交于 2019-12-01 10:54:47
I just read an article that describes how HttpApplicationState has AcquireRead() / AcquireWrite() functions to manage concurrent access. It continues to explain, that in some conditions however we need to use an explict Lock() and Unlock() on the Application object to avoid a Race condition. I am unable to understand why a race condition should exist for Application state if concurrent access is implicitly handled by the object. Could someone please explain this to me ? Why would I ever need to use Application.Lock() and Application.Unlock() ? Thank You ! The AcquireRead and AcquireWrite

PHP download blocks rest of requests

 ̄綄美尐妖づ 提交于 2019-12-01 09:14:20
I am downloading files from my server using a very simple script: header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($fichero)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); It works fine, but while the browser is downloading the file I cannot browse through the site or downloads other files

Is using an existing object rather than creating a specific lock object safe?

你离开我真会死。 提交于 2019-12-01 09:14:09
EDIT: As it turns out when I was browsing I found a question the appears to be the same as mine which I didn't find earlier: Difference between lock(locker) and lock(variable_which_I_am_using) I am looking at some code and trying to get my head around the locking thing and I am getting there I think. Now I noticed in some code I am reviewing that an object is created like so: private HashSet<Graphic> clustersInUse = new HashSet<Graphic>(); Then further in the code is used like so: lock (clustersInUse) { // Do something with the Hashset } Now, is there a problem doing this rather than creating

How to work around ORA-02014: cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc

 ̄綄美尐妖づ 提交于 2019-12-01 08:49:06
I want to lock one record in a table. The record is specified as "the next that has ID greater than..." CREATE TABLE test (id number); SELECT id FROM (SELECT id FROM test WHERE id > 10 ORDER BY id) WHERE ROWNUM = 1 FOR UPDATE; This seems intuitive and easy. But it is not. Any ideas? P.S. I do need the existing query to remain the same because it is a cursor and there are several places that use this cursor's %rowtype. I think you're going to need something like: SELECT id FROM test WHERE id = (SELECT MIN(id) FROM test WHERE id > 10) FOR UPDATE; 来源: https://stackoverflow.com/questions/3166615

SQL Server: how to acquire exclusive lock to prevent race condition?

£可爱£侵袭症+ 提交于 2019-12-01 08:45:14
I have the following T-SQL code: SET TRANSACTION ISOLATION LEVEL SERIALIZABLE BEGIN TRANSACTION T1_Test /*This is a dummy table used for "locking" and it doesn't contain any meaningful data.*/ UPDATE lockTable SET ID = 1 WHERE ID = 1 DECLARE @Count TINYINT SELECT @Count = COUNT(*) FROM debugSP WAITFOR DELAY '00:00:5'; INSERT INTO debugSP (DateCreated, ClientId, Result) SELECT GETDATE(), @@SPID, @Count COMMIT TRANSACTION T1_Test I am using "locking" hack marked with comment to acquire the exclusive lock. NOTE: using TABLOCKX or UPDLOCK hints will not work because I have broken ATOMIC-ity by