Thread and Channel Example

Archived XCore tutorials.
User avatar
AtomSoft
XCore Addict
Posts: 135
Joined: Mon Dec 14, 2009 3:02 pm

Thread and Channel Example

Post by AtomSoft »

Ok i dont own a XMOS but have simulated this and it works 100% heh... It has tons of comments for the learner heh...

Code: Select all

/*
 ============================================================================
 Author		 : Jason Lopez (AtomSoft)
 Name        : threads.xc
 Description : Thread & Channel Tutorial 
 ============================================================================
 */

#include <xs1.h>
#include <print.h>		//Used For Console Writes
/*
 * Step 1. Create a couple of Fuctions which will use the channels.
 *         Using "chanend" to specify a Channel type as the var type.
 *         At Least 1 function should send data to the other.   
 */
static void ThreadTwo(chanend theChannel) {
  unsigned int MyVariable=0;		//Init our variable as 0
  
  while(!MyVariable)				//Wait for our var to be > 0
	  theChannel :> MyVariable;		//Load Variable(INT) from channel into var
  
  printstr("Recieved: 0x");			//Console Print so we can see clear results
  printhexln(MyVariable);  			//Displays our var and a newline in console
}
static void ThreadOne(chanend theChannel, unsigned int var) {
  printstr("Opening Channel and Sending: 0x"); 	//Console Print for debug use
  printhexln(var);					//Displays our var and a newline in console
  
  theChannel <: var;				//Send our Variable (INT) into the Channel...
}
/*
 * Step 2. Using the "par" statement you can call other functions
 *         simultaneously ... up to 8 per core I believe. These are
 * 		   threads.
 * 
 * Step 3. In this example i will be calling ThreadOne function and 
 * 		   telling it the channel to communicate on is called 
 *         ChanA and the data i want to send if 0xB54F. 
 * 
 *         Then I simultaneously call ThreadTwo which soul purpose is
 *         to wait for data from the channel and tell us it has it 
 *         and what it is.
 * 		   
 */
int main()
{
  chan ChanA;		//Create a Channel named ChanA
  par {
	  ThreadOne(ChanA,0xB54F);	//Call ThreadOne Using ChanA and Variable(0xB54F)
	  ThreadTwo(ChanA);			//Call ThreadTwo Using ChanA also...
  }
  return 0;
}

Stripped down version:

Code: Select all

/*
 ============================================================================
 Author		 : Jason Lopez (AtomSoft)
 Name        : threads.xc
 Description : Thread & Channel Tutorial 
 ============================================================================
 */
#include <xs1.h>

static void ThreadTwo(chanend theChannel) {
  unsigned int MyVariable=0;		
  
  while(!MyVariable)				
	  theChannel :> MyVariable;		
}
static void ThreadOne(chanend theChannel, unsigned int var) {
  theChannel <: var;				
}

int main()
{
  chan ChanA;	
  par {
	  ThreadOne(ChanA,0xB54F);
	  ThreadTwo(ChanA);			
  }
  return 0;
}

User avatar
RemoRitzmann
Member++
Posts: 23
Joined: Wed Feb 03, 2010 10:23 pm

Post by RemoRitzmann »

Hi Atom Soft
Thanks - that helped me understanding channeling.

I now use your tip printhexln(var); :idea: instead of

printhex(var);
printstr("\n");

But now I'd be interested in knowing how long this additional channeling thread is active.
Shouldn't we use asynchronous transactions (Programming XC on XMOS Devices - Chapter 3.3) that won't disrupts the flow of the program?

So long, Remo
User avatar
Berni
Respected Member
Posts: 363
Joined: Thu Dec 10, 2009 10:17 pm

Post by Berni »

Thats a good beginners tutorial. You should try using the print commands to actually show the value.

Do continue these tutorials to also explain how to get strings over channels and how to make a transfer that doesn't block the thread. On the end probably do a simple channel client/server example that is used to make a hardware recurse like a LCD accessible from all cores.