Skip to content

Commit

Permalink
Made the global mutex construction for the get_current_dir() and set_…
Browse files Browse the repository at this point in the history
…current_dir()

routines more robust.
  • Loading branch information
davisking committed Jun 19, 2011
1 parent d9cd30a commit 927743a
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions dlib/misc_api/misc_api_kernel_1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,30 @@ namespace dlib

namespace
{
mutex cwd_mutex;
mutex& cwd_mutex()
{
static mutex m;
return m;
}
// Make sure the above mutex gets constructed before main()
// starts. This way we can be pretty sure it will be constructed
// before any threads could possibly call set_current_dir() or
// get_current_dir() simultaneously.
struct construct_cwd_mutex
{
construct_cwd_mutex()
{
cwd_mutex();
}
} oaimvweoinvwe;
}

std::string get_current_dir (
)
{
// need to lock a mutex here because getting and setting the
// current working directory is not thread safe on windows.
auto_mutex lock(cwd_mutex);
auto_mutex lock(cwd_mutex());
char buf[1024];
if (GetCurrentDirectoryA(sizeof(buf),buf) == 0)
{
Expand All @@ -61,7 +76,7 @@ namespace dlib
{
// need to lock a mutex here because getting and setting the
// current working directory is not thread safe on windows.
auto_mutex lock(cwd_mutex);
auto_mutex lock(cwd_mutex());
if (SetCurrentDirectory(new_dir.c_str()) == 0)
{
throw set_current_dir_error("Error changing current dir to '" + new_dir + "'");
Expand Down

0 comments on commit 927743a

Please sign in to comment.