Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread Working Directory

By definition, multiple threads of a single program share their working directory. Do you know if there is a way to have each thread in a dedicated working dir ? Maybe a specific library ?

Note : target language is c++

like image 659
leJon Avatar asked Jun 30 '14 13:06

leJon


1 Answers

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, connecting or binding a Unix domain socket.

like image 93
Christopher Monsanto Avatar answered Sep 28 '22 07:09

Christopher Monsanto