locking

Threaded execution speed of LOCK CMPXCHG

I wrote a multi-threaded app to benchmark the speed of running LOCK CMPXCHG (x86 ASM). On my machine (dual Core - Core 2), with 2 threads running and accessing the same variable, I can perform about 40M ops/second. Then I gave each thread a unique variable to operate on. Obviously this means there's no locking contention between the th...

What's the easiest way to acquire a lock on a shared variable for a UNIX C program?

I am writing a C UNIX program that is threaded and shares one variable across all threads. What would be the easiest way to acquire a lock on this variable? I can't find any small libraries just for locking in UNIX. Any suggestions how to do this? Thanks, Boda Cydo. ...

Locking ConcurrentDictionary

I have a class that has ConcurrentDictionary inside. From the class, I expose only few methods. I just realized that method TryGetValue doesn't expose copies of object from dictionary but rather the objects themselves. So I want to deepcopy them :) Let's say I have a method like that: public IList<MyClass> TryGetMyClass(long someId)...

Locking with S3

What's the recommended way to implement a simple locking mechanism to be used in conjunction with S3? Example of what I want to do: acquire lock by object id read object from S3 modify data write object to S3 release lock Ideally looking for a cloud based locking mechanism. I could use memcached locally, but then I have to deal wit...

Should this class use data locking for multi threading?

I have a class that contains some data and there are many threads use it: class MyClass { static Dictionary<Key, Value> MyData; static IEnumerable<Data> Data { get { return MyData.Values; } } static void Reset() { MyData = GetMyData(); } } Sometime (say once in...

Why lock when reading from a dictionary

I am confused by a code listing in a book i am reading, C# 3 in a Nutshell, on threading. In the topic on Thread Safety in Application Servers, below code is given as an example of a UserCache: static class UserCache { static Dictionary< int,User> _users = new Dictionary< int, User>(); internal static User GetUser(int id) {...

Temporary file, single instance lock, and process kill (Java)

I'm currently using a temp folder for my Java application to create a lock so that only one instance can be running at a time. This lock file MUST be deleted when the program exits. This is fine, except for the case when the process is killed (not allowed to shutdown normally, e.g. from Windows Task Manager). If that happens, the user ...

How can I create a non-file lock in PHP?

I have a maintenance script in PHP that updates and repairs the database. In theory, running two scripts simultaneously shouldn't be a problem, but I want to be extra safe by putting a lock in a variable in PHP. The problem, of course, is that I can't store it in a $_SESSION variable because sessions only apply to one user. Is there any...

JVM consumes all CPU, most threads as BLOCKED. JVM bug?

Last night, a server (JBoss 5.1GA, Java(TM) SE Runtime Environment (build 1.6.0_20-b02), Java HotSpot(TM) 64-Bit Server VM, running in a Linux VM on VMWare) suddenly started to use 100% CPU. The app is a fairly typical J2EE business app running Seam, nothing particular about it. The load was extremely low at that time. I managed to get...

Threading: Locking Under the hood of

What happens when we use the lock object? I am aware that it the runtime makes use of the monitor.Enter and Exit methods. But what really happens under the hood? Why only reference types to be used for locking? Even though the object used for accomplishing the locking is changed, how come it still provides thread safety? In the curren...

Acquiring lock - C# SQL server

Database : SQL server 2005 Programming language : C# I have a method that does some processing with the User object passed to it. I want to control the way this method works when it is called by multiple threads with the same user object. I have implemented a simple locking which make use of database. I can't use the C#'s lock statemen...

Can a lock ever get stuck locked due to an unexpected thread death or similar

I have an ASP.NET site that I am maintaining. Currently it has code that on first access (and other times too) goes and does a bunch of data caching by writing it to files. Currently it has no locking so we cna get problems with multiple people accessing the site and multiple threads trying to write to the files at the same time. This is...

Ordering in ReaderWriterLock

When I use lock(){...}, I cannot garantee which thread will enter the lock first. What about ReaderWriterLock? Does it works like a FIFO for the writers or not? ...

python: elegant way to deal with lock on a variable?

I have code that looks like something like this: def startSearching(self): self.searchingLock.acquire() searching = self.searching if self.searching: self.searchingLock.release() self.logger.error("Already searching!") return False self.searching = True self.searchingLock.release() #some...

What is problem on this lock ?

Hello Highly there is a problem in this lock but i couldn't understand what is that. I have strong suspicious that below example doesn't lock enough well. So what can be problem ? class example { object locker = new object(); void start() { for (int i = 0; i < 1000; i++) { (new Thread(dostuff)).S...

Why is table-level locking better than row-level locking for large tables?

According to the MySQL manual: For large tables, table locking is often better than row locking, Why is this? I would presume that row-level locking is better because when you lock on a larger table, you're locking more data. ...

Unordered threads problem

I had asked question about lock in here and people responded there is no problem in my lock implementation. But i catched problem. Here is same lock implementation and i am getting weird result. I expect to see numbers starts from 1 but it starts from 5.Example is at below. class Program { static object locker = new object(); st...

Ordered execute threads

Hello everybody I have an console application which interact another user interface. Interface sends commands and my application should to process them. It is important to keep listening my console application while processing and execute commands in ordered. So i listen interface on main thread and execute commands in another thread. ...

Pthreads- 1 lock, 2 unlocks

If I understand correctly, then foo1() fails to unlock &private_value_. As a result, foo2()'s thread_mutex_lock does not work since foo1() never released it. What are the other consequences? int main ( ... ) foo1(); foo2(); return 0; } foo1() { pthread_mutex_lock(&private_value_); do something // no unlock! } foo2() { pthread_...

Lock or synchronized

Hi, I have main thread which calls another thread. timeout period of second one is 15 seconds. Current implementaion is main thread check second one for every 2 seconds. What I need to do is wait for only as long as second thread finish, upto a maximum of 15 seconds. I thought of trying wait and notify, but it will require synchronized....