Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pthread_mutex_lock return not tested

I'm really wondering why all source codes that implement a pthread_mutex_lock never test its return value as defined :

documentation of pthread

even in books the examples don't test if the lock is in error, codes just do the lock.

Is there any reason I missed to let it untested ?

like image 779
dlewin Avatar asked Jun 09 '11 09:06

dlewin


2 Answers

Basically, the only “interesting” error is EINVAL, which in most programs will only happen because of memory corruption, or, as I know from my own painful experience, during program shutdown after destructors have already destroyed some mutexes. The way I see it, the only reasonable response to such an error is to abort the program, which on the other hand is very inconvenient if the errors occur precisely because the program is already shutting down. Of course, this can be solved, but it’s not at all that simple, and not much is gained by it for most programs.

like image 124
Ringding Avatar answered Sep 28 '22 16:09

Ringding


First off, I think "all source code" and "never test" are too strong. I think "some" and "often" would be more accurate.

In books, error checking code is often omitted for clarity of exposition.

As to real-world code, I guess the answer has to be that it is perceived that the likelihood of failure is very low. Whether this is a good assumption is debatable.

like image 29
NPE Avatar answered Sep 28 '22 16:09

NPE