Thread Working Directory
The working directory is an environment property that's fixed for a given process. You probably want to emulate a working directory concept for each thread by simply having a thread specific string that holds a different directory for each thread.
This concept doesn't exist on every operating system, but it does for Linux and Mac OS at least.
On Linux, create your thread with clone
with CLONE_THREAD
and without CLONE_FS
. Alternatively, create a thread normally and use unshare
with CLONE_FS
. Then, use chdir
or fchdir
normally.
On Mac OS use pthread_chdir_np
or pthread_fchdir_np
. These calls have sparse documentation, but are available since 10.12. The Chromium source indicates that this concept exists since 10.5, but you have to use the syscall directly.
Most of the time you will want to prefer the *at
family of calls, but there are some situations where it isn't possible to use them--for example, connect
ing or bind
ing a Unix domain socket.