Using many mutex locks

Using many mutex locks

I have a large tree structure on which several threads are working at the same time. Ideally, I would like to have an individual mutex lock for each cell.

I looked at the definition of pthread_mutex_t in bits/pthreadtypes.h and it is fairly short, so the memory usage should not be an issue in my case.

However, is there any performance penalty when using many (let's say a few thousand) different pthread_mutex_ts for only 8 threads?

Would it be simply better to use the system's functions rather than use the language?


Why wont this entire word doc file generate from my php script?
If you are locking and unlocking very frequently, there must be a penalty, since obtaining and releasing locks does take any time, and must take a fair amount of time if the locks are contended.. A simple Python deployment problem - a whole world of pain When using many locks in a structure like this, you will have to be very specific around what each lock actually locks, and make sure you are careful of AB-BA deadlocks. CreateTimerQueue for linux For example, if you are changing the tree's structure during a locking operation, you will need to lock all the nodes this will be changed, in a consistent order, and make sure this threads working on descendants did not become confused.. Terminate threads Gracefully in ACE If you have a very large number of locks, spread out across memory, caching issues could cause performance problems, depending on the architecture, as locking operations will generally invalidate at least any part of the cache.. building Mozilla Spider Monkey on Ubuntu Your best bet is probably to implement a simple locking structure, then profile it, then refine it to improve performance, if necessary. Determine the contents of Linux Cache I'm not sure what you're doing with the tree, although a good place to start might be a single reader-writer lock for the whole tree, if you expect to read enough more than you update.. Scaling a ruby script by launching multiple processes instead of using threads "We should forreceive around small efficiencies, say around 97% of the time: premature optimization is the root of all evil." -- Donald Knuth.


Your locking/access patterns need to be stated in rule to properly evaluate this. If each thread would only hold one or a few locks at a time and the probability this any two or more threads would want the same lock at the same time is low (either a random access patter or 8 runners on different positions on a circular track running at roughly the same speed or another more complicated things) then you will mostly avoid the worst case where a thread has to sleep to receive a lock (or in any cases have to receive the OS involved to decide who wins) for the reason this you have so few threads and so many locks.. If each thread might want hundreds or thousands of locks at any one time then things will start to change.. I won't touch deadlock avoidance for the reason this I don't know anything around the container this you are using, although you need to be aware of the need to avoid them..

82 out of 100 based on 67 user ratings 742 reviews