don't pass a reference to a temporary to physics_thread

This commit is contained in:
2026-03-04 21:31:01 +01:00
parent 4e5ca6be6c
commit 08352dd997

View File

@ -15,7 +15,8 @@
class threaded_physics class threaded_physics
{ {
struct add_mass {}; struct add_mass
{};
struct add_spring struct add_spring
{ {
@ -23,24 +24,25 @@ class threaded_physics
size_t b; size_t b;
}; };
struct clear_graph {}; struct clear_graph
{};
using command = std::variant<add_mass, add_spring, clear_graph>; using command = std::variant<add_mass, add_spring, clear_graph>;
struct physics_state struct physics_state
{ {
#ifdef TRACY #ifdef TRACY
TracyLockable(std::mutex, command_mtx); TracyLockable(std::mutex, command_mtx);
#else #else
std::mutex command_mtx; std::mutex command_mtx;
#endif #endif
std::queue<command> pending_commands; std::queue<command> pending_commands;
#ifdef TRACY #ifdef TRACY
TracyLockable(std::mutex, data_mtx); TracyLockable(std::mutex, data_mtx);
#else #else
std::mutex data_mtx; std::mutex data_mtx;
#endif #endif
std::condition_variable_any data_ready_cnd; std::condition_variable_any data_ready_cnd;
std::condition_variable_any data_consumed_cnd; std::condition_variable_any data_consumed_cnd;
Vector3 mass_center = Vector3Zero(); Vector3 mass_center = Vector3Zero();
@ -55,14 +57,17 @@ class threaded_physics
}; };
private: private:
std::optional<BS::thread_pool<>* const> thread_pool;
std::thread physics; std::thread physics;
public: public:
physics_state state; physics_state state;
public: public:
explicit threaded_physics(const std::optional<BS::thread_pool<>* const> thread_pool = std::nullopt) explicit threaded_physics(
: physics(physics_thread, std::ref(state), std::ref(thread_pool)) {} const std::optional<BS::thread_pool<>* const> _thread_pool = std::nullopt)
: thread_pool(_thread_pool), physics(physics_thread, std::ref(state), std::ref(thread_pool))
{}
threaded_physics(const threaded_physics& copy) = delete; threaded_physics(const threaded_physics& copy) = delete;
auto operator=(const threaded_physics& copy) -> threaded_physics& = delete; auto operator=(const threaded_physics& copy) -> threaded_physics& = delete;
@ -78,17 +83,19 @@ public:
} }
private: private:
#ifdef ASYNC_OCTREE #ifdef ASYNC_OCTREE
static auto set_octree_pool_thread_name(size_t idx) -> void; static auto set_octree_pool_thread_name(size_t idx) -> void;
#endif #endif
static auto physics_thread(physics_state& state, std::optional<BS::thread_pool<>* const> thread_pool) -> void; static auto physics_thread(physics_state& state,
std::optional<BS::thread_pool<>* const> thread_pool) -> void;
public: public:
auto clear_cmd() -> void; auto clear_cmd() -> void;
auto add_mass_cmd() -> void; auto add_mass_cmd() -> void;
auto add_spring_cmd(size_t a, size_t b) -> void; auto add_spring_cmd(size_t a, size_t b) -> void;
auto add_mass_springs_cmd(size_t num_masses, const std::vector<std::pair<size_t, size_t>>& springs) -> void; auto add_mass_springs_cmd(size_t num_masses,
const std::vector<std::pair<size_t, size_t>>& springs) -> void;
}; };
#endif #endif