Page 1 of 1

Multithreading in Alarms&Conditions

Posted: 13 Feb 2013, 15:37
by smueller
Hello!

In Lesson 6 of the SDK documentation the use of Alarms & Conditions is described. There a callback function is used to check the monitored variable, if its value exceeded the alarms limit. For our system it would be gainful, when can do this kind of checks in another thread. Thus our question is if the SDK functions (e.g. UaServer_Events_FireEvent) can be called safely outside the SDK task or if the use of this functions is only within on thread possible? If not, how can we handle concurrency with the SDK: Are there any functions in SDK, which can help us?

Best regards,
smueller

Re: Multithreading in Alarms&Conditions

Posted: 28 Feb 2013, 13:14
by Support Team
Hello,

the AnsiC SDK is designed for use in singlethreaded mode only, so it is not safe to call SDK functions from another thread.

However, a possibility of handling this is to use some kind of message queue in the function that calls UaServer_DoCom() periodically. Inside this function you can process your message queue and call SDK functions without problems. You only need to synchronize access to the message queue.

As the timeout of the UaServer_DoCom() call is set to 10ms, you might want to minimize the delay if a new message is in the queue. This can be done by calling

Code: Select all

OpcUa_SocketManager_SignalEvent(OpcUa_Null, OPCUA_SOCKET_RENEWLOOP_EVENT, OpcUa_True);
from another thread that fills the message queue. This makes UaServer_DoCom() return immediately so the new message can be handled. It is important to set OPCUA_USE_SYNCHRONISATION to true in this case, otherwise you might run into problems in the stack when calling OpcUa_SocketManager_SignalEvent.

Best regards,
Unified Automation Support Team

Re: Multithreading in Alarms&Conditions

Posted: 20 Jun 2013, 09:17
by jscheib
Hallo.

You state that it is not safe to call SDK functions from another thread and advice to use a message queue to execute the functions in the DoCom-thread. I assume that you mean all functions beginning with “UaServer_” by SDK functions. Can I assume that all stack functions (beginning with “OpcUa_”) are safe to call from another thread when “OPC_USE_SYNCHRONISATION” and “OPCUA_MULTITHREADED” are defined?

Thanks,
jscheib

Re: Multithreading in Alarms&Conditions

Posted: 25 Jun 2013, 09:12
by Support Team
Hello,

generally it is not possible to use the AnsiC SDK with the “OPCUA_MULTITHREADED” define set to true, it is designed for single-thread mode only.

All ...Complete functions like UaServer_ReadComplete and others like UaServer_Events_FireEvent that are processed by the SDK can not be called out of another thread. Utility functions like OpcUa_Variant_Initialize can be called out of another thread, as they don't access resources used by the SDK.

Best regards,
Unified Automation Support Team