The following are three implementations of the C library function dirname, which, given a path, returns the parent directory (e.g., dirname("/foo/bar") returns "/foo".
The first implementation, dirname.c, like libc's version, is not thread-safe.
The second, dirname_tsd.c, uses thread-specific data (via the C library functions pthread_key_create, pthread_setspecific, and pthread_getspecific ) to implement a thread-safe version of dirname.
The third implementation, dirname_tls.c, uses a simliar technique called thread-local storage to also implement a thread-safe version of the function.
To run any of these programs, enter:
./PROGRAM PATH1 PATH2
The main function computes the dirname of PATH1, and a second thread computes the dirname of PATH2. Note that the non-thread safe version of dirname prints the same dirname for both paths.
Additional files: