locking

How to use a mutex

你。 提交于 2019-11-29 16:38:31
I have one thread, that is sending data stored in a buffer of type List< string> via tcp. Another thread is writing into the buffer. As I am not very familiar with c# I'd like to know how I should use lock or Mutex correctly. This is the code I'd like to use eventually: while(buffer.isLocked()) { buffer.wait(); } buffer.lockBuffer(); buffer.add(tcpPacket); buffer.unlockBuffer(); buffer.notify(); This is my current code. I hope someone can help me complete it. public class Buffer { private Mutex mutex; private List<string> buffer; private bool locked = false; public Buffer() { mutex = new Mutex

Using the same lock for multiple methods

。_饼干妹妹 提交于 2019-11-29 16:30:09
问题 I haven't had any issues using the same lock for multiple methods so far, but I'm wondering if the following code might actually have issues (performance?) that I'm not aware of: private static readonly object lockObj = new object(); public int GetValue1(int index) { lock(lockObj) { // Collection 1 read and/or write } } public int GetValue2(int index) { lock(lockObj) { // Collection 2 read and/or write } } public int GetValue3(int index) { lock(lockObj) { // Collection 3 read and/or write } }

Is it possible to read-lock a file?

百般思念 提交于 2019-11-29 15:41:27
I'm developing an application which checks for changes made to a file by a separate program (not written by me). If a change is detected, it opens the file, reads the last line, then closes the file. I'm using the following code to make sure my program doesn't try to lock the file, but only opens it in read mode: FileStream fs = new FileStream( _scannerFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader sr = new StreamReader(fs); var str = sr.ReadToEnd(); sr.Close(); fs.Close(); Unfortunately, in spite of this, I'm still getting the following error whenever my program

Pessimistic lock in T-SQL

我的未来我决定 提交于 2019-11-29 14:43:39
If i SELECT a row for updating in MS SQL Server, and want to have it locked till i either update or cancel, which option is better :- 1) Use a query hint like UPDLOCK 2) Use REPEATABLE READ isolation level for the transaction 3) any other option. Thanks, Chak. Neither. You almost never want to hold a transaction open while your user is inputting data. If you have to implement a pessimistic lock like this, people generally do it by rolling their own functionality. Consider the full ramifications of what you are doing. I once worked on a system that implemented locking like this. You often run

What is wrong with this solution to locking and managing locked exceptions?

。_饼干妹妹 提交于 2019-11-29 14:43:36
问题 My objective is a convention for thread-safe functionality and exception handling within my application. I'm relatively new to the concept of thread management/multithreading. I am using .NET 3.5 I wrote the following helper method to wrap all my locked actions after reading this article http://blogs.msdn.com/b/ericlippert/archive/2009/03/06/locks-and-exceptions-do-not-mix.aspx, which was linked in response to this question, Monitor vs lock. My thought is that if I use this convention

Oracle locking with SELECT…FOR UPDATE OF

我只是一个虾纸丫 提交于 2019-11-29 14:08:37
问题 I'm selecting from tables FOO and BAR. I'd like to lock the records of FOO which are being returned, but I don't want the records of BAR to be locked. cursor c_foobar is select foo.*, bar.* from foo, bar where foo.id = bar.foo_id for update of <what should I put here?> It seems like I need to specify individual columns, but I want the entire record of foo to be locked. e.g. I wish I could do something like: cursor c_foobar is select foo.*, bar.* from foo, bar where foo.id = bar.foo_id for

Correct way to take a exclusive lock

半城伤御伤魂 提交于 2019-11-29 14:04:32
I am writing a procedure that will be reconciling finical transactions on a live database. The work I am doing can not be done as a set operation so I am using two nested cursors. I need to take a exclusive lock on the transaction table while I am reconciling per client, but I would like to release the lock and let other people run their queries in between each client I process. I would love to do a exclusive lock on a row level instead of a table level, but what I have read so far says I can not do with (XLOCK, ROWLOCK, HOLDLOCK) if the other transactions are running at READCOMMITED isolation

Read-Write lock with GCD

我只是一个虾纸丫 提交于 2019-11-29 14:00:04
问题 My application makes heavy use of GCD, and almost everything is split up in small tasks handled by dispatches. However, the underlying data model is mostly read and only occasionally written. I currently use locks to prevent changes to the critical data structures while reading. But after looking into locks some more today, I found NSConditionLock and some page about read-write locks. The latter is exactly what I need. I found this implementation: http://cocoaheads.byu.edu/wiki/locks . My

Release of flock in case of errors?

随声附和 提交于 2019-11-29 13:33:56
Imagine the following Perl code (here in pseudo code): successfully acquired flock for FILEHANDLER # line 1 some error or maybe simply a call to exit() # line 2 close FILEHANDLER (which also releases the lock) # line 3 In this case I wouldn't release the lock, as the Perl script ends in line 2. In that case, is the lock ever released by the operating system? Does it see "hey, the script that acquired the lock crashed" and release the lock? Does it release the lock immediately? Also, is there one Perl instance running for each script, so that it's clear which script crashed/stopped without

Can ToArray() throw an exception?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 13:28:21
问题 While the answer to this question is excellent, it implies that you should surround calls to List.ToArray() in a lock for concurrency. this blog post also implies that it could fail catastrophically (but rarely). I typically use ToArray rather than a lock in when enumerating Lists or other collections in order to avoid the "Collection Modified, Enumeration may not complete" exception. This answer and the blog post have called that assumption into question. The documentation for List.ToArray()