Subj : Re: Linking without -lpthread doesn't fail?? To : comp.programming.threads From : Eric Sosman Date : Wed Oct 05 2005 02:31 pm dmarkh@cfl.rr.com wrote On 10/05/05 13:00,: > When I link a program that has pthread_* calls in it yet I forget to > link with -lpthread the program links. It does not work and there is no > indication other that the program not working that I did anything > wrong. Why is that? You didn't mention the operating system you're using. Some systems provide dummy versions of pthread_* functions that single-threaded programs can use. This allows you to write a function that can be used in either a single- or multi-threaded program without needing to maintain separate versions for each. Your function can call pthread_mutex_lock(), for example, and "the right thing happens" no matter whether the function is being used by a single- or multi-threaded program. If the program was linked with -lpthread you'll get the real pthread_mutex_lock() implementation, but if it was linked without -lpthread you'll get the dummy version. Most of the dummy implementations are no-ops: the dummy pthread_mutex_lock() will do nothing and just return, claiming that it has locked the mutex -- the important question is whether it's now safe to access the protected data, and in a single-threaded program thread-safety is guaranteed. The dummy versions of more complicated functions usually return a failure indication of some kinds -- for example, pthread_create() will probably say it was unable to create a new thread. If your program checks the returned values of the pthread_* functions, you may find that many of them report failure if linked without -lpthread. This was the way Solaris worked, up through Solaris 9. In Solaris 10 there is no longer a distinction between single- and multi-threaded processes: all processes are potentially multi-threaded. A "single-threaded process" is really a multi- threaded process that happens to have started only one thread so far; it might perfectly well start another thread in a moment or two. This change of viewpoint means that the dummy implementations are gone, and that you get the real pthread_* functions even if you don't link with -lpthread. That's Solaris; whatever system you're using may act in a similar way. -- Eric.Sosman@sun.com .