This function DOES change the current element. What I want is one that checks (thread safely) for the element's existence without changing the current element (which causes bad read/writes in multi-threaded, problems during ForEach etc.).
I know you can also use the Threaded keyword, but it won't work in an structure-embedded Map(), like I did in my failed web server experiment (ca. 900kB of source-codes).
benubi wrote: Fri Aug 25, 2023 1:21 pm
Thanks for the tip but nope.
This function DOES change the current element. What I want is one that checks (thread safely) for the element's existence without changing the current element (which causes bad read/writes in multi-threaded, problems during ForEach etc.).
As a proof of concept, here is a macro that can do the job and an unproductive demo to test it:
Note: map elements are not in a sorted order
---------------------
two = second
Element five does not exist.
four = fourth
Element six does not exist.
one = first
Element four exists.
three = third
Element five does not exist.
[15:24:48] Warte auf den Start des Executable...
[15:24:48] Executable-Typ: Windows - x64 (64bit, Unicode)
[15:24:48] Executable gestartet.
[15:24:48] [Debug] France
[15:24:48] [Debug] GE? 1
[15:24:48] [Debug] US? 1
[15:24:48] [Debug] XY? 0
[15:24:48] [Debug] France
[15:24:48] Die Programmausführung ist abgeschlossen.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y "Happiness is a pet." | "Never run a changing system!"
Can you explain what problem this solves?
Still not completely reliable without mutex or semaphores.
If 2,3,10 or more threads run findmapelement() on same map?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
This procedure (and also Demivec's macro) return the *element pointer or 0, but does not change the current element of the map() as FindMapelement() does. It is a kind of workaround for the feature request of the thread creator.
And yes, in a multithreaded environment you still have to secure this using mutex or semaphores.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y "Happiness is a pet." | "Never run a changing system!"
Kurzer wrote: Fri Aug 25, 2023 2:26 pm
Or as a Procedure...
There is a drawback to a procedure and that it is usable with only one type of map because of the parameter declaration. It does have the plus of not needing additional variables in the calling scope. A variation using a macro to define a procedure with the specific map element type as a kind of template would solve both those situations. I'll leave that as an exercise to the reader. It wouldn't be needed as often as the original solution, which already may be a uncommon occurrence.
Demivec's macro looks good to me. Am I missing something here? The macro makes no changes, just taking a peek and saying yes or no if an element is there or not. So why the need for thread safety and mutexes?
netmaestro wrote: Sat Aug 26, 2023 5:40 pm
Demivec's macro looks good to me. Am I missing something here? The macro makes no changes, just taking a peek and saying yes or no if an element is there or not. So why the need for thread safety and mutexes?
Suppose two threads are running concurrently but slightly out of step, the map is currently at item 'X' before the threads act.
Thread A Thread B
-------- --------
PushMapPosition (at 'X')
FindMapElement (for 'Y') PushMapPosition(at 'X', 'Y' or invalid if 'Y' is not an element?)
PopMapPosition (at 'X') FindMapElement('Z')
PopMapPosition(Possibly X or Y?)
The code is fine in a single thread but in concurrent threads, because FindMapElement changes the current map pointer and the operation overall isn't performed atomically, the result in thread B and the final position are indeterminate, depending upon the difference in step between the two threads. The more threads that were running concurrently the worse this problem would get.
Threadsafe code is definitely required and a mutex could well be necessary to ensure atomicity.