1

I have two sensors, that I need to monitor constantly. the inner working of the two sensors are different and I need to check both constantly as shown below.

int sensorA(){
    while (true) {
    // read sensor A
    cout<<"sensorA \n";
  }
}
int sensorB(){
    while (true) {
    // read sensor B
    cout<<"sensorB \n";
  }
}

My question is how can I ensure the two sensors are read in interleaved manner? Like read sensorA then SensorB then Sensor A etc.

kona
  • 143
  • 1
  • 3
  • 8

4 Answers4

7

Read both in the same loop?

int sensorA();
int sensorB();
while(true){
    //read A
    count<<"sensor A \n";
    //read B
    coutn<<"sensor B \n";
}

If you need them to be constantly read and cannot, for some reason, afford the delay between reads on a single sensor, then you'll need to create a thread for each sensor.

xen-0
  • 709
  • 4
  • 12
6

(Euh, so my comment as an answer:)

Just read them one after the other, within the same loop.

while (1) {
    read(sensorA);
    read(sensorB);
}

something like the above pseudo-code will do the trick.

3

You need to run them in separate threads.

Thread 1 would run one loop, while Thread 2 would run the other.

See

void thread1fun()
{
  while (true) {
    // read sensor A
    cout<<"sensorA \n";
  }
}

void thread2fun()
{
  while (true) {
    // read sensor B
    cout<<"sensorB \n";
  }
}

int main()
{
    std::thread t1(thread1fun); 
    std::thread t2(thread2fun); 
    t1.join();
    t2.join();
}

NOTE As pointed out in the comments, this only guarantees that both while loops will run continuously. In a "normal" architecture then the threads will both, on average, run roughly at the same "speed". However if the requirement is that the sensors are read strictly as follows {s1,s2,s1,s2....}, then this is not sufficient. In that case you would need a signalling system (events and waits) to guarantee that order. That would likely be inefficient if your sensors can be read very quickly.

Dennis
  • 3,683
  • 1
  • 21
  • 43
  • 1
    Or just put'em into the same loop, not? –  Jun 12 '13 at 09:39
  • @H2CO3 - Sorry I had assumed that he could not do that for some reason. Of course, if he can then that is much much simpler! – Dennis Jun 12 '13 at 09:43
  • @ValeriAtamaniouk - No, it's not guaranteed as you are of course dependent on the OS scheduling algorithm, but given that both threads are at the same priority, they should over time be given equal CPU shares. Unless the scheduling algorithm is a very strange one that is! – Dennis Jun 12 '13 at 09:44
  • @Dennis Even if the scheduling is RR and completely fair, the interleaving is not guaranteed, as CPU time is given in quantums and you can't enforce sheduler to match that quantum to sensor operation duration. Manual inter-thread synchronization is needed with cond vars or similar. – Valeri Atamaniouk Jun 12 '13 at 09:49
  • 1
    @ValeriAtamaniouk I am not sure that the OP really was asking for guaranteed in-order execution of the sensor reads. I took the question to mean that he wanted "simultaneous" and constant reading of both sensors. However if you are right then he will of course need inter-thread signals as you state. I will add a note to the answer to direct attention to your suggestions. Thanks. – Dennis Jun 12 '13 at 09:55
1

If reading from your sensors is an asynchron operation you may be interested in this example from Boost.Coroutine.

Jan Herrmann
  • 2,717
  • 17
  • 21