arrow_back_ios Back to List

liboffload::constructs::thread

Offload KB - offload-library

Old Content Alert

Please note that this is a old document archive and the will most likely be out-dated or superseded by various other products and is purely here for historical purposes.

When you include <liboffload> you get access to the features within the constructs library.

liboffload::constructs::thread is a scoped thread where you can implement features like deferred function calls into your solutions with ease. What's better is that on PS3 using the <offload> compiler the thread is run on SPU. On other platforms, it is run as a thread using the pthread library.

 liboffload::constructs::thread<RETURN_TYPE, ARGUMENT_TYPE, FUNCTION_NAME>

Limitations:

  • void is not a supported return type or argument
  • Only a single argument is supported
  • Reference arguments or return types are not supported
  • Avoid calling with large objects, as the pthread implementation for safety and compatability copies arguments into a local store.

Example use:


#include <liboffload>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <sys/memory.h>
#include <string.h>
#include <time.h>

int PlusPlus(int v)
{
	printf("PlusPlusn");
	return ++v;
}

int MinusMinus(int v)
{
	printf("MinusMinusn");
	return --v;
}

int Double(int v)
{
	printf("Doublen");
	return v*2;
}

int test()
{
	int ret = 0;
	int r0=0,r1=0,r2=0,r3=0,r4=0,r5=0,r6=0,r7=0;

	int start = 2;

	// Thread Tasks
	liboffload::constructs::thread<int, int, PlusPlus>   tPlus1;
	liboffload::constructs::thread<int, int, PlusPlus>   tPlus2;
	liboffload::constructs::thread<int, int, MinusMinus> tMinus1;
	liboffload::constructs::thread<int, int, Double> 	tDouble1;
	liboffload::constructs::thread<int, int, Double> 	tDouble2;

	tPlus1.spawn(start);
	tMinus1.spawn(start);

	r0 = tPlus1.join();
	r1 = tMinus1.join();

	tPlus1.spawn(r0);
	tPlus2.spawn(r1);
	tDouble1.spawn(r0);
	tDouble2.spawn(r1);

	r2 = tPlus1.join();
	r3 = tPlus2.join();
	r4 = tDouble1.join();
	r5 = tDouble2.join();

	tPlus1.spawn(r4);
	tMinus1.spawn(r5);

	r6 = tPlus1.join();
	r7 = tMinus1.join();

	printf("Results: %d %d %d %d %d %d %d %dn", r0,r1,r2,r3,r4,r5,r6,r7);
	return ret;
}