Multithreading in Alarms&Conditions

Questions regarding the use of the ANSI C SDK for Server or Client development or integration into customer products ...

Moderator: uasdkc

Post Reply
smueller
Full Member
Full Member
Posts: 5
Joined: 08 Feb 2013, 08:11

Multithreading in Alarms&Conditions

Post 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

User avatar
Support Team
Hero Member
Hero Member
Posts: 3064
Joined: 18 Mar 2011, 15:09

Re: Multithreading in Alarms&Conditions

Post 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

jscheib
Jr. Member
Jr. Member
Posts: 1
Joined: 22 Mar 2013, 15:19

Re: Multithreading in Alarms&Conditions

Post 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

User avatar
Support Team
Hero Member
Hero Member
Posts: 3064
Joined: 18 Mar 2011, 15:09

Re: Multithreading in Alarms&Conditions

Post 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

Post Reply