Talk:Lock (computer science)

From Wikipedia, the free encyclopedia
Jump to: navigation, search
WikiProject Computing / CompSci (Rated Start-class, Low-importance)
WikiProject icon This article is within the scope of WikiProject Computing, a collaborative effort to improve the coverage of computers, computing, and information technology on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
Start-Class article Start  This article has been rated as Start-Class on the project's quality scale.
 Low  This article has been rated as Low-importance on the project's importance scale.
Taskforce icon
This article is supported by WikiProject Computer science (marked as Mid-importance).

The code example in the article doesn't guarantee mutual exclusion[edit]

I removed the following code example from the article as it doesn't solve the atomicity problem:

 while (1)
 { /* loop until lock is obtained */
     if (lock == 0)
     { /* lock not set */
         lock = myPID; /* set lock with my process ID */
         sleep(1); /* wait for a short time */
         if (lock == myPID) break; /* we still have the lock */
     /* we still don't have the lock */
     sleep(someTime); /* wait for some period of time */
 //Critical Section

Let's assume Process A and B are executing. A executes, checks the if guard that lock==0, proceeds then does a context switch. B then checks the lock, confirms it's open, proceeds and does a context switch. A then sets lock to its PID, and sleeps. There's no guarantee that B will wake up next and overwrite the lock variable. A wakes up 1s later, checks that it still does have the lock, breaks and enters the critical section. At some point it does a context switch. B then proceeds as normal; it overwrites the lock variable, sleeps, rechecks it has the lock, then enters the critical section as well.

--Chozan 02:23, 12 November 2005 (UTC)

Lock Modality?[edit]

Along with the ideas of granularity, perhaps there could be some discussion of the modality of locking schemes (shared locks, exclusive locks, intent locks) and the obligatory table of the compatibility of different locks. I don't feel competent enough to write this section myself, but if anyone with familiarity in the subject would be interested, perhaps [1], [2], or [3] could serve as useful references?

--Kevinoid 14:32, 28 November 2006 (UTC)

This subject should be given greater importance in the foregoing graphic schematic and implied editorial perception / projection.[edit]

Modern computer systems and applications CANNOT FUNCTION PROPERLY without correct locking mechanisms, including the so-called "lock-free" solutions. The art has advanced beyond local ad hoc stand-alone programs. All professional systems designers and programmers must consider asynchronous concurrency, coherency, serialization, multiprocessing environments and application integrity.

Although this particular article does not necessarily, and probably is not intended to, nor needs to, cover the entire ground, it is nonetheless a particularly significant entry point into the Wikipedia literature, and deserves the suggested upgrade.

Lor 04:11, 7 November 2007 (UTC)

"They cause blocking" as a disadvantage?[edit]

In section The problems with locks, it lists the following as one of the disadvantages:

They cause blocking, which means some threads/processes have to wait until a lock (or a whole set of locks) is released.

Well, that's pretty much the purpose of locks (to block threads/processes until some conditions are met), isn't it? I say we remove this, but I'll wait for opinions.

--PoisonedQuill (talk) 10:09, 13 June 2008 (UTC)

Not really, as I understand it, locks are just for mutual exclusion, not condition synchronization (well, I guess we could argue about mutual exclusion being the same as having a condition that says that only one process at a time can execute in a given critical section). Nevertheless, I agree with you in that having to wait is not a problem caused by using locks, but by using a solution that has a critical section. I think the second item,

Lock handling adds overhead for all access to a resource, even when the chances for collision are very rare. (However, any chance for such collisions is a race condition).

is trying to say pretty much the same thing. I'd add "Because threads have to wait to obtain a lock," before "lock handling adds overhead for all...", remove the first item, and leave it at that. Facuq (talk) 02:49, 14 May 2009 (UTC)

Locking mechanisms are an advantage because they enable the implementation of "critical sections". Critical sections are inevitable in any environment where resources (memory locations, devices, files, etc.) must be shared asynchronously, for example, the printer on your network. Critical sections are not a problem either, since they manage shared resources. The blocking of threads is an unavoidable consequence, not a "problem" - if you consider a problem as something that might have a solution.

There is a problem, though, and that is there must be sufficient resources - exclusively dedicated memory, real or virtual - available for the locking mechanisms to maintain their state information in response to a varying number of contemporaneous invocations, without which the mechanisms will fail, or "crash" bringing down everything depending on them and bringing down the operating region in which they reside. "Failure" is better than crashing, which means a proper locking mechanism ought to be able to return an "unable to obtain lock for <whatever> reason" status to the critical section in the application, which ought to be able to handle that situation gracefully.

The logical design of an application requires these considerations from the very root of conception.

Lor (talk) 16:05, 10 October 2009 (UTC)

@PoisonedQuill, you are right. That's what locks are for if you want to understand them at the lowest possible level. At a higher level though, we use locks to implement mutual exclusion, and we use mutual exclusion to guarantee certain invariants in a program. If the program can be re-designed to solve the same problem, but with invariants that require fewer mutexes or no mutexes at all, then it will out-perform the original program. (talk) 15:14, 22 May 2012 (UTC)

I combined the "blocking" bullet with the "wait forever" bullet, as they are just different aspects of the same issue. On the bullet reading: "Lock handling adds overhead for each access to a resource, even when the chances for collision are very rare. (However, any chance for such collisions is a race condition.)"... The parenthetical statement makes no sense. Locks help avoid race conditions, so a chance for such a collision would be a race condition if you failed to use the lock. Yes, deadlock can be a problem, but that is what lock-dependency checkers are for. Lots of people talk about composability problems with locking, but just you try composing a pair of transactions with (say) and RPC operation between them. Some work required to achieve NPV here! PaulMcKenney (talk) 17:03, 17 November 2013 (UTC)


"Ada is probably worth looking at too for a comprehensive overview, with its protected objects[12][13] and rendez-vouses."

rendez-vouses? really? I'm not a french expert, but shouldn't this just be rendez-vous?

Maybe someone who is more confident in french can confirm this?

Chaos.squirrel (talk) 06:29, 23 June 2010 (UTC)

I'm French but I've also checked the Oxford dictionary. "rendezvous" doesn't have a hyphen and doesn't take "es" in the plural form.

I've made the change.

Togamaru (talk) 23:51, 26 October 2011 (UTC)

There's a lot of redundancy in Section 4: The problems with Locks[edit]

I don't have time to re-write it, but almost every one of the bullet points under that heading seems to overlap with several of the others. E.g.;

  • They cause blocking, which means...
  • If one thread holding a lock dies, stalls/blocks or goes into any sort of infinite loop,...
  • Convoying..., if a thread holding a lock is descheduled,... — Preceding unsigned comment added by (talk) 15:02, 22 May 2012 (UTC)

Citation needed???[edit]

It's weird that the following statement needs a citation: "This subtlety can increase the chance that a programmer will unknowingly introduce a deadlock". If an application's locking logic is complex, it may have deadlocks that in some cases can not be avoided (and must be detected). If the logic is simple, only bugs can lead to deadlocks. I didn't write that statement, but I can't beilive that someone is waiting for a citation. — Preceding unsigned comment added by (talk) 17:56, 1 June 2013 (UTC)