Skip to content

Commit

Permalink
Core: Generate a static random number in Application::applicationPid()
Browse files Browse the repository at this point in the history
Using Qt's QCoreApplication::applicationPid() doesn't always give a unique ID so that multiple FreeCAD instances cannot be executed at the same time.
This fixes FreeCAD#17678
  • Loading branch information
wwmayer committed Nov 6, 2024
1 parent fd71fc3 commit 9af0ccf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/App/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
# endif
# include <boost/program_options.hpp>
# include <boost/date_time/posix_time/posix_time.hpp>
# include <chrono>
# include <random>
#endif

#ifdef FC_OS_WIN32
Expand Down Expand Up @@ -1117,7 +1119,17 @@ Application::TransactionSignaller::~TransactionSignaller() {

int64_t Application::applicationPid()
{
return QCoreApplication::applicationPid();
static int64_t randomNumber = []() {
auto tp = std::chrono::high_resolution_clock::now();
auto dur = tp.time_since_epoch();
auto seed = dur.count();
std::mt19937 generator(static_cast<unsigned>(seed));
constexpr int64_t minValue {1};
constexpr int64_t maxValue {1000000};
std::uniform_int_distribution<int64_t> distribution(minValue, maxValue);
return distribution(generator);
}();
return randomNumber;
}

std::string Application::getHomePath()
Expand Down
1 change: 1 addition & 0 deletions src/App/PreCompiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

// STL
#include <bitset>
#include <chrono>
#include <exception>
#include <functional>
#include <iterator>
Expand Down

0 comments on commit 9af0ccf

Please sign in to comment.