From e0693ba1646f7a1cf37f2f6985ea79b4584ea434 Mon Sep 17 00:00:00 2001 From: Arunodoy18 Date: Sun, 16 Nov 2025 22:02:13 +0530 Subject: [PATCH] I have fixed the bug #76 --- WeatherRoutingTool/algorithms/genetic/patcher.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/WeatherRoutingTool/algorithms/genetic/patcher.py b/WeatherRoutingTool/algorithms/genetic/patcher.py index c196a01..38ac39d 100644 --- a/WeatherRoutingTool/algorithms/genetic/patcher.py +++ b/WeatherRoutingTool/algorithms/genetic/patcher.py @@ -2,6 +2,7 @@ import logging import math import os +import threading from datetime import datetime from pathlib import Path @@ -41,18 +42,23 @@ def patch(self, src: tuple, dst: tuple): class SingletonBase(type): """ - TODO: make this thread-safe - Base class for Singleton implementation of patcher methods. + Thread-safe metaclass for Singleton implementation of patcher methods. This is the implementation of a metaclass for those classes for which only a single instance shall be available - during runtime. + during runtime. Uses double-checked locking to ensure thread safety. """ _instances = {} + _lock = threading.Lock() def __call__(cls, *args, **kwargs): + # First check without lock (fast path) if cls not in cls._instances: - instance = super().__call__(*args, **kwargs) - cls._instances[cls] = instance + # Acquire lock for instance creation + with SingletonBase._lock: + # Double-check inside lock (slow path) + if cls not in cls._instances: + instance = super().__call__(*args, **kwargs) + cls._instances[cls] = instance return cls._instances[cls]