From ac9ddad225d8b8bf035a0f5e03130cfdf9c10b98 Mon Sep 17 00:00:00 2001 From: tidely <43219534+tidely@users.noreply.github.com> Date: Tue, 11 Nov 2025 18:37:13 +0200 Subject: [PATCH 1/2] sharedMem: Fix race condition --- TrafficCommunication/useful/sharedMem.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/TrafficCommunication/useful/sharedMem.py b/TrafficCommunication/useful/sharedMem.py index ed7ea51..b60fab9 100755 --- a/TrafficCommunication/useful/sharedMem.py +++ b/TrafficCommunication/useful/sharedMem.py @@ -32,7 +32,7 @@ class sharedMem: def __init__(self, mem_size=20): self.lock = Lock() # Create a lock for thread synchronization - + # Define the shape of the shared memory shared_memory_shape = np.dtype( [ @@ -43,7 +43,7 @@ def __init__(self, mem_size=20): ("finishflag", np.bool_), # Finish flag ] ) - + # Create a raw array for shared memory array = RawArray("c", mem_size * shared_memory_shape.itemsize) shared_memory = np.frombuffer(array, dtype=shared_memory_shape) @@ -73,9 +73,9 @@ def insert(self, msg, values): if len(values) > 2: self.shared_memory[self.lastMem]["value3"] = values[2] # Set the third value self.shared_memory[self.lastMem]["finishflag"] = True # Set the finish flag - self.lastMem += 1 # Increment the index of the last memory slot used - if self.lastMem == self.mem_size: - self.lastMem = 0 # Wrap around if the index exceeds the memory size + self.lastMem += 1 # Increment the index of the last memory slot used + if self.lastMem == self.mem_size: + self.lastMem = 0 # Wrap around if the index exceeds the memory size # Method to retrieve data from shared memory def get(self): From 66594235c3a12989171a86d20dbe1f5fa48f1343 Mon Sep 17 00:00:00 2001 From: tidely <43219534+tidely@users.noreply.github.com> Date: Tue, 11 Nov 2025 18:41:06 +0200 Subject: [PATCH 2/2] sharedMem: Fix omitting fields with uninitialized values --- TrafficCommunication/useful/sharedMem.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/TrafficCommunication/useful/sharedMem.py b/TrafficCommunication/useful/sharedMem.py index b60fab9..cd17f71 100755 --- a/TrafficCommunication/useful/sharedMem.py +++ b/TrafficCommunication/useful/sharedMem.py @@ -28,6 +28,9 @@ from multiprocessing import RawArray, Lock import numpy as np +# Default value to indicate an uninitialized field +DEFAULT_VALUE: float = -99.9 + # Define a class for shared memory class sharedMem: def __init__(self, mem_size=20): @@ -57,9 +60,9 @@ def __init__(self, mem_size=20): for mem in self.shared_memory: with self.lock: # Acquire the lock using get_lock() mem["Command"] = "Command_" # Default command string - mem["value1"] = -99.9 # Default value for first value - mem["value2"] = -99.9 # Default value for second value - mem["value3"] = -99.9 # Default value for third value + mem["value1"] = DEFAULT_VALUE + mem["value2"] = DEFAULT_VALUE + mem["value3"] = DEFAULT_VALUE mem["finishflag"] = False # Default finish flag # Method to insert data into shared memory @@ -84,11 +87,11 @@ def get(self): for mem in self.shared_memory: if mem["finishflag"]: msg = {"reqORinfo": "info", "type": mem["Command"]} # Create a message dictionary - if mem["value1"] != 99.9: + if mem["value1"] != DEFAULT_VALUE: msg["value1"] = float(mem["value1"]) # Add the first value to the message - if mem["value2"] != 99.9: + if mem["value2"] != DEFAULT_VALUE: msg["value2"] = float(mem["value2"]) # Add the second value to the message - if mem["value3"] != 99.9: + if mem["value3"] != DEFAULT_VALUE: msg["value3"] = float(mem["value3"]) # Add the third value to the message mem["finishflag"] = False # Reset the finish flag vals.append(msg) # Append the message to the list of retrieved values