LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Does Race conditions occur in Queued state machine

Solved!
Go to solution

hI 

I'am developing a vi in which i use a producer consumer design pattern.
the producer enqueue's the states for the consumer. but at some point of excecution i wish to enqueue the next state of the consumer statemachine, from within the consumer state itself. does this cause any problem??.
 in other words can i enqueue data within the consumer loop also. so thet the next state of consumer will depend on the data enqueued

Thanks

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 1 of 14
(3,838 Views)

Hey,

this is no problem you can enqueue the states from both loops (or even a third.....)

Regards,

CMW..

0 Kudos
Message 2 of 14
(3,815 Views)

Yes this approch may cause some unexpected order of execution.

you should use only producer loop to send commands to consumer loop.

make consumer loop as simple state machine.

If you want to trigger other state which can occur in consumer ,use user events to send command from consumer to producer , and producer in return will give you next state.

0 Kudos
Message 3 of 14
(3,814 Views)

The problem is when in a single queue you start enqueuing from multiple places then next queue element out depends on timing when they got inserted.

so somtimes it may have different order of execution than expected.

0 Kudos
Message 4 of 14
(3,808 Views)

A queued state machine consumer will act as a synchronous process if there is one 'sender', and an asynchronous process if there are many 'senders'. If you are worried about unexpected states, then maybe you could split your QSM into two? It depends on what you are doing, of course.

_____________________________
- Cheers, Ed
0 Kudos
Message 5 of 14
(3,806 Views)

Of course it depends on application , still i am talking about one definite entry point for individual queue.

So that the next state dequeued will be know to user already.

0 Kudos
Message 6 of 14
(3,784 Views)

Neos, it doesn't always matter when a message gets processed. A QSM pattern is an implementation of an Actor pattern, in which a message gets sent from one or many origins, knowing that - at some point - it will be processed. If you want anything to be determinate, then you would want to use another structure, maybe a timed while loop.

_____________________________
- Cheers, Ed
0 Kudos
Message 7 of 14
(3,775 Views)

Actually let me be clear, what i am saying is if user has a pattern where he is send commands from producer to consumer using a queue , in consumer case states are being dequed and processed , after that in consumer loop only we are enqueing next state.

so basically now in queue 2 entry points are there.

suppose consumer has states A, B, C, D in which A is executing right now at the end of its completion it enques another state B.

But at the same time user has pressed a button on the front panel which wants to execute state C.

at this point somtimes order of execution can be A, B, C or A. C, B.

I have not worked on Actor framework till now , so have a very less idea about it.

 

0 Kudos
Message 8 of 14
(3,771 Views)

Thank you all for the suggestions.Smiley Happy

-----------------------------------------------------------------------------------------------------------------------------
Waiting For the inner calling 🙂


0 Kudos
Message 9 of 14
(3,764 Views)
Solution
Accepted by topic author djac91

I make extensive use of Queued Message Handleres where both the "Producer" and consumer enqueue to the same queue. 

A "Firinstance" from work today: here are two captures of a part of the same code

Capture.PNG

Capture1.PNG

The top loop controls the State of a semi autonomous process where data is gathered.  

  1. a event is detected triggering start of measurement cycle
  2. Data begins to be gathered
  3. Another event tells the top loop that it is time to stop gathering data and "Analize" is sent to the consumer At Opposite end
  4. Analize flushes the buffer, formats the data and continues to the "Log" state

The thing to remember is to use a priority queue.  Enqueue to self and flush as appropriate, enqueue opposite end from the controler.  See also "A Trip To Grandma's House"

 

It is a very powerful technique for large applications where you want to abstract out the "what" and "When" from the how and just let a loop "Do"

 

And Yes, If I had chosen a unrestricted datatype (string, var) rather than an enum and built the lower loop as a child class you have an "Actor"


"Should be" isn't "Is" -Jay
Message 10 of 14
(3,729 Views)