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;
}
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;
}



