Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SetCurrentDirectory in multi-threaded application

I understand SetCurrentDirectory shouldn't be used in a multithreaded application since the current directory is shared between all threads in the process.

What is the best approach to setting the directory with this in mind. It can mostly be avoided setting the directory by including the full pathname when opening files instead of first navigating to them with SetCurrentDirectory, but is this the only solution?

like image 308
Callum Avatar asked Jan 24 '23 11:01

Callum


2 Answers

I've encountered this problem before.

Any object that needs the concept of a current directory to support relative paths or searching (e.g. a build tool) has a member property that it maintains with its "current" path, then build the full path to open/create/search.

The initial value for CurrentPath can be retrieved once during the application's load phase, e.g. main(), WinMain(), DllInit(), etc. via GetCurrentDirectory and stored in a global. After that the Win32 version is ignored.

The OPENFILENAME structure has an initial directory member, so file open/save dialogs don't have to use the Win32 current directory.

like image 70
devstuff Avatar answered Jan 26 '23 09:01

devstuff


Each process has a single current directory, so if you want each thread in your process use different current directory I think you should specify the full path in each.

like image 41
Ahmed Avatar answered Jan 26 '23 09:01

Ahmed