• 4
    You stop programming
    Because you are a monster
  • 0
    I don't know C sharp, but it looks like the first task runs indefinitely and you never even get to the rest of it. Am I wrong?
  • 2
    Undefined behavior.

    As soon as a task finishes incrementing the variable, it releases the lock, so the second task can take it, but sometimes the second task can't manage to take it, and the lock is re-acquired by the first task.

    Pretty random. I guess based on OS behavior and thread prioritization.
  • 0
    What does lock do? I have never used it
  • 0
    @Mitiko locks a lock :)

    A lock can be any object. A thread can lock that object so that any other thread that wants to lock it, is forced to wait for it to unlock.
    This way you avoid 2 threads accessing and modifying the same stuff at the same time. You can lock a DB connection for example, an open file, a queue, list, or any other shared resource. or if you can't identify that resource, lock a random object shared among threads.

    Lock is a code block, like so:

    lock(someStuff) {
    ... code here...
    } // unlocks here
Add Comment