locking

Using lock statement with ThreadPool in C#

自作多情 提交于 2019-12-11 01:35:40
问题 I have a multi-threaded program (C#) where I have to share global static variables between threads that may take some time to execute (sending data request to another system using WCF). The problem is that using the lock statement does not seem to guarantee mutual exclusion when it's declared outside of the ThreadPool. static void Main(string[] args) { public static int globalVar = 0; public object locker; System.Timers.Timer timer1 = new System.Timers.Timer(1000); timer1.Elapsed += new

Don't use System.Threading.Timer to synchronize?

五迷三道 提交于 2019-12-11 01:07:03
问题 I have some code looking like this: using System.Threading; public sealed class MyClass : IDisposable { private readonly Timer _timer; public MyClass() { _timer = new Timer(MyCallback); } public void SomeMethod() { lock (_timer) { ... } } ... } When I run code analysis (FxCop) I get the error message CA2002 : Microsoft.Reliability : 'MyClass.SomeMethod' locks on a reference of type 'Timer'. Replace this with a lock against an object with strong-identity. I found some documentation here. An

Check if file is in use [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-11 00:55:21
问题 This question already has answers here : Is there a way to check if a file is in use? (18 answers) Closed 3 years ago . Hi guys I need some help. I'm trying to open a textfile on a servern from multiple clients at the same time so I'm not locking the file when reading from it. Like this: new StreamReader(File.Open(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) Now I'm trying to check if this file is used by any of the clients (because I want to write something new to it),

Semaphore_create causes kernel panic

末鹿安然 提交于 2019-12-11 00:11:04
问题 I am developing a kernel extension. I require to use wait and signal mechanism to wait for particular events (programming logics). I am trying to use semaphores as part of the kernel extension to implement the wait and signal methodology. The creation of semaphore is causing a kernel panic. Need help in figuring out the right implementation. Let me know if I am using it wrong or if there is any other simpler mechanism to wait and signal for kernel development. The current code which I am

What Phenomena does MySQL try to prevent by locking the whole table upon executing Delete statement with the condition on a non-indexed column

荒凉一梦 提交于 2019-12-10 23:54:28
问题 Using the MySQL isolation level of Repeatable Read. Given table test having non-indexed column quantity : id | quantity -------------------- 1 | 10 2 | 20 3 | 30 Tx1 executes 1st, note it is not committed yet, meaning that all the acquired locks are not released yet. Tx1: START TRANSACTION; DELETE FROM test WHERE quantity=10; Now executing Tx2 Tx2: START TRANSACTION; INSERT INTO test(quantity) VALUES (40); COMMIT; For Tx2 I get the following result: Lock wait timeout exceeded; try restarting

Explicit lock to table(s) in SQL Server?

时间秒杀一切 提交于 2019-12-10 23:48:42
问题 Is it possible to do an explicit shared lock on a table in SQL Server? I need to do the following: shared lock table copy table to temp table, for manipulation exclusive lock table copy stuff from tempTable to table release all locks Basically, I want to be sure that nothing is added to the table, while I'm doing stuff to the temp table, but I still want it to be readable. For some reason, putting the whole thing in a transaction causes a deadlock in some of the stuff I do to the temp table,

Does MySQL auto lock rows when updating?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 23:40:58
问题 I'm new to PHP development and I want to create a PHP-MySQL web-app. In this app I have the case where it is possible that at the same time I might have one "UPDATE" statement and one "SELECT" query for the same row. And here is where my question appears. Does MySQL automatically lock the row when updating so the select queries will wait for it? If yes, will the select queries wait until the update query is ready ( transaction finished) and then execute? Or will they fail and I will have to

Loopback: Atomic read and update

送分小仙女□ 提交于 2019-12-10 23:08:41
问题 is there a way to implement something like this in loopback? LOCK READ INCREMENT UNLOCK I would like to keep counters as database values, each key is a counter (or a setting), and they shouldn't accessed my multiple requests at the same time. Also this should work for local requests too (no remoteHooks) Thanks 回答1: If you are using the mongoDB connector, this is supported by extended operators. MyModel.updateAll( { id: 123' }, { '$inc': { myproperty: 1 }}, // increment myproperty by 1 {

Thread safety in multithreaded access to LinkedList

℡╲_俬逩灬. 提交于 2019-12-10 22:25:20
问题 My application needs to keep an access log of requests to a certain resource and multiple threads will be recording log entries. The only pertinent piece of information is the timestamp of the request and the stats being retrieved will be how many requests occurred in the last X seconds. The method that returns the stats for a given number of seconds also needs to support multiple threads. I was thinking of approaching the concurrency handling using the Locks framework, with which I am not

what happens with lock when thread gets closed while the lock is set

落花浮王杯 提交于 2019-12-10 20:56:00
问题 I was wondering, If in a thread i have a lock statement and if that specific thread is closed while the lock is set, what happens with the lock ? Are the other threads going to have access to the critical zone(does my specific lock variable get unlocked) or does the lock remain active and bricks my app ? If so, What solutions do i have to avoid the brick? 回答1: A lock statement: lock (x) { ... } is interpreted by the compiler in the resulting IL to: Monitor.Enter(x); try { ... } finally {