Bash + GNU coreutils, 11 bytes
mkfifo x;<x
Creates a stray FIFO x
in the current directory (so you'll need to not have a file by that name). FIFOs can be deleted the same way as regular files, so it shouldn't be hard to clear up.
A FIFO has a write side and a read side; attempting to open one blocks until another process opens the other, and this seems to have been intentionally designed as a synchronization primitive. Given that there's only one thread here, as soon as we try to open it with <x
, we get stuck. (You can unstick the deadlock via writing to the FIFO in question from another process.)
This is a different kind of deadlock from the one when there are two resources, and two threads each have one and needs the other; rather, in this case, there are zero resources and the process needs one. Based on the other answers, I think this counts, but I can understand how a deadlock purist might want to disallow the answer.
Come to think of it, I can actually think of three deadlock-like situations:
The "traditional" deadlock: two threads are each waiting for a lock to be released, that's held by the other thread.
A single thread is waiting for a lock to be released, but it holds the lock itself (and thus is blocking itself from being able to release it).
A single thread is waiting for a synchronization primitive to be released, but the synchronization primitive starts in a naturally locked state and has to be unlocked externally, and nothing's been programmed to do that.
This is a deadlock of type 3, which is in a way fundamentally different from the other two: you could, in theory, write a program to unlock the synchronization primitive in question, then run it. That said, the same applies to deadlocks of type 1 and 2, given that many languages allow you to release a lock you don't own (you're not supposed to and would have no reason to do so if you had a reason to use the locks in the first place, but it works…). Also, it's worth considering a program like mkfifo x;<x;echo test>x
; that program's sort-of the opposite of a type 2 deadlock (it's trying to open both ends of the FIFO, but it can't open one end until after it opens the other end), but it was made from this one via adding extra code that never runs after this one! I guess the problem is that whether a lock is deadlocked or not depends on the intention behind the use of the lock, so it's hard to define objectively (especially in a case like this where the only purpose of the lock is to produce a deadlock intentionally).
What is a deadlock, for those of us who don't know? – Conor O'Brien – 2015-10-29T15:16:13.500
@ConorO'Brien https://en.wikipedia.org/wiki/Deadlock
– Adám – 2016-11-21T15:10:50.897Does it count as a deadlock if I try to lock the same (non-reentrant) lock twice from the same thread? (sorry I didn't realise this question in the sandbox) – John Dvorak – 2013-11-28T06:49:59.167
@JanDvorak Does that create a situation where the code execution halts because one thread is waiting for something it cannot get (because another is holding it and waiting for what the first thread has)? Or is it one thread? If you can create such a situation with one thread, then it is fine. Read the wikepedia article on deadlock, that is what I expect. – Justin – 2013-11-28T15:43:54.513
2
Code execution must halt
I don't understand. How is it a deadlock if it halts? Do you mean it'll be waiting for something rather than just spinlocking like an asshole? – Cruncher – 2013-11-28T16:17:29.807@Cruncher Take a look at the example that does not work. The code execution doesn't stop because the loop keeps running. Yes I mean waiting rather than spinning. – Justin – 2013-11-28T22:14:00.783
So if you can make the UI thread wait for itself in Dyalog APL, does that mean that there's some hope of getting a javascript answer? Although I suspect that sort of thinking would just open the door to this answer: Javascript 6 "wait()" ... ermmm. no. Related: Is it possible for a thread to Deadlock itself?
– Nathan Cooper – 2013-12-04T11:36:40.137