As I have multiple threads which can potentially write at the same time I need a mutex to prevent writes to collide.
I have seen that WinCC-OA has a “synchronized” function which is basically a mutex. I have tested this, and it works with no problem as long as I use this on a standard variable like int or bool.
As I have multiple devices and one socket per device I would rather have one mutex per device and this seams not too work as passing a mapping or a vector gives syntax error.
I have recreated the problem in a small example here:
Code: Select all
mapping mutexes; // map containing one mutex per device
bool commonMutex; // mutex shared across all devices
void foo(string deviceName)
{
while(true)
{
synchronized(mutexes[deviceName]) // Why this cannot be done???
//synchronized(commonMutex) // this works but it is too constraining
{
DebugN("foo critical part");
}
delay(0,1);
}
}
void bar(string deviceName)
{
while(true)
{
synchronized(mutexes[deviceName]) // Why this cannot be done???
//synchronized(commonMutex) // this works but it is too constraining
{
DebugN("bar critical part");
}
delay(0,1);
}
}
void main()
{
dyn_string deviceList = makeDynString("device1","device2","device3");
for (int i = 1; i