Instead of using "Hanging by a thread" in the context of someone walking endlessly in the desert and being barely able to continue, what is a better choice of phrase?
There are loads of common expressions to describe someone whose mortality is "hanging by a thread". Off the top of my head:
Barely clinging to life, Barely holding on, On his/her last legs, Running out of time...
Or, if you're writing fiction, consider describing the actual symptoms that one might experience when suffering and/or dying in the desert rather than simply stating outright that he/she is not doing so well: show; don't tell.
Think about how dry and sandpapery someone's tongue might feel, how each breath made his/her lungs burn, how tired eyes struggled to cope with being blasted by sand and so on.
"Hanging on by the skin of one's teeth" is a biblical reference:
My bone cleaveth to my skin and to my flesh, and I am escaped with the skin of my teeth (Job 19:20).
The allusion is to narrowness.