Async communication between two tasks..

Technical discussions around xCORE processors (e.g. xcore-200 & xcore.ai).
Post Reply
kcfresher
Junior Member
Posts: 4
Joined: Wed Oct 19, 2016 4:15 am

Async communication between two tasks..

Post by kcfresher »

Is there a way to communicate between two tasks in complete async. mode. I read from the documentation that interface and channel are synch. and streaming chan is async.
But streaming chan are not complete async. as reading/writing from an empty channel blocks the control to execute rest of the statements.
I mean when I execute below code, If I don't write to channel, in task1() rest of code followed by read_chan() will not get executed.
Is there a way to achieve complete async. I mean if the channel empty, it should simply ignore and move to the other statements. Or any other way without channels we can do, like with global variables and etc..? Basically, what i want is one task() produces and another one consumes.

Code: Select all

void task1(streaming chanend c){
while(1){
read_chan(c,buf);
//some logic
}
}
void task2(streaming chanend c){
while(1){
//some logic
if(flag){
    write_chan(c,buf);
}
//some logic
}
}


peter
XCore Addict
Posts: 230
Joined: Wed Mar 10, 2010 12:46 pm

Post by peter »

Hi kcfresher,

Yes, you can run in a completely asynchronous manner by doing a select statement with a default:

Code: Select all

#include <print.h>
#include <xs1.h>

void receiver(streaming chanend c) {
  while (1) {
    int x;
    select {
      case c :> x:
        printintln(x);
        break;
      default:
        break;
    }
  }
}

void sender(streaming chanend c) {
  for (int i = 0; i < 10; i++) {
    delay_milliseconds(100);
    c <: i;
  }
}

int main() {
  streaming chan c;
  par {
    receiver(c);
    sender(c);
  }
  return 0;
}
Hope that helps,

Peter
User avatar
infiniteimprobability
XCore Legend
Posts: 1126
Joined: Thu May 27, 2010 10:08 am
Contact:

Post by infiniteimprobability »

Also, some other discussions on the subject as it's not the first time it has come up:

https://www.xcore.com/forum/viewtopic.php?f=26&t=3464

http://www.xcore.com/forum/viewtopic.php?f=47&t=4400
Post Reply