LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using the same object in two actors at the same time

Hello,

 

I'm in doubt.  In my application, for example i have three Hardware classes - Database, DAQ1, DAQ2.

 

Database class has one main method (except initialization methods) - Log Measure.

DAQ1 class also has one method - Get Voltage, and DAQ2 class has one method - Get Current.

 

Then i define two HAL (Hardware Abstraction Layer) classes - Measurement1 and Measurement2.

Each of those classes have two main methods, respectively - Measure Voltage and Measure Curent. They also have some initilziation methods.

 

In code, i create three objects of Hardware classes - Database, DAQ1 and DAQ2 and initiaze it. For example in Database private cluster i have DB RefNum, in DAQ1&2 i have device addresses etc.

 

Next I initialzie Measurement1 with two hardware objects - Database and DAQ1, and in the same way - Measurement2 with Database, and DAQ2 objects.

 

What Measure Voltage and Measure Curent (methods of Measurement1 and Measurement1 objects) do?

 

Measure Voltage method call DAQ1::Get Voltage method, then Database::Log Measure.

Similarly, Measure Current method call DAQ2::Get Voltage method, then Database::Log Measure.

 

Till now it's clear i think. It should work properly.

 

But now i incorporate Actor Framework, and define Measurement1 and Measurement2 as an actors.

Now i can run Measurement1 and Measurement2 at the same time, simultaneously, but this two actors share one object - Database. So my question - what is when actors want to use Database::Log measure method at the same time and this method is time cosuming (for example large pack of data).

Is one actor waits for second to stop executing this method of one shared object? I think yes because reentrancy setting (http://zone.ni.com/reference/en-XX/help/371361J-01/lvconcepts/reentrancy/)

 

But what, for example if i share one Hardware object of device (ADC converter) beetwen two HAL classes (Measurement1 and Measurement2). This Hardware object has two methods, for ex. Get Voltage At channel 1, and Get Voltage At Channel 2. Both of this methods cannot by call at the same time, because in physical device i can measure only at one channel in the same time.

 

Let the Get Voltage At channel X meausere take 10 seconds. Now i have huge chance, that Measurement 1 actor call Get Voltage At channel 1 method, and Measurement 2 wants to call Get Voltage At channel 2, when Get Voltage At channel 1 is in process. How to inform actor to wait till second actor release device resources?

 

Regards,

Kacper

 

 

0 Kudos
Message 1 of 3
(2,524 Views)

Dear Kacper,

 

the way I understood, there are two separate questions in this post, namely:

 

  1. I'm using a shared resource in multiple actors, and the code accessing the resource can take quite some time to execute. How can can I ensure this does not mess up the timing of one or all accessors?
  2. I'm using a shared resource in multiple actors, and the code accessing the resource can generate errors if I try to access it from multiple places. How can I ensure synchronization between all accessors?

As for the first question, the optimal solution to separate the resource prone to timing issues in a different thread/actor. Put anything that is not strictly timed (file access, network comunication) in a separate loop. For each actor, you will get queue references to communicate with, so instead of logging the measurements in the actors where they are made, just queue them up for a different loop to process.

 

For the second part, the same logic still applies. Ideally, every singular resource should be handled by its own thread and nowhere else, so if anything else needs data from/to said resource, it can send a request to the dedicated thread or actor.

If, for some reason, this is not sufficient, you have to handle synchronization in some other way. There are a lot of techniques here, for example:

 

  • Create a named semaphore or lock as a part of the class. Have class functions use the semaphore before accessing the resource.
  • Use actor messages. Have a "resource in use" or "resource released" messages sent to all users whenever obtaining or releasing said resource.
  • Have a separate actor handle all resources, awarding them to threads needing them. This method also allows setting priorities between requests.

These are just a few examples, there are many other options.

 

Please let me know if this was helpful. 

 

Kind regards:

Andrew Valko
National Instruments Hungary
Message 2 of 3
(2,462 Views)

Dear Andrew,

 

Thank you for your answer, it was very helpful. I tried various solutions, and now i understand AF much more. Today my app works very well.

 

Thank You,

Kacper

0 Kudos
Message 3 of 3
(2,360 Views)