Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
91 lines (61 sloc) 2.5 KB

TinyThread++

TinyThread++ is a minimalist, portable, threading library for C++, intended to make it easy to create multi threaded C++ applications. The library is closesly modeled after the C++11 standard, but only a subset is implemented at the moment. Need portable threads for your C++ app? Use TinyThread++!

The main block is here, which is generated from this github repo.

Open and build the examples

This example is already in biicode. It is very simple to build it, by just opening the block.

$ bii init tinythread
$ cd tinythread
$ bii open examples/tinythread
$ bii build

Simple Hello World with a thread

Now, run the hello example.

$ bin/examples_tinythread_hello

You should see how a thread say "Hello world!"

This Hello World just defines the thread method

void HelloThread(void * aArg)
{
  cout << "Hello world!" << endl;
}

and then, launch the thread and wait for it.

thread t(HelloThread, 0);
t.join();

You can see all the code here.

Draw a fractal

If you run the fractal example, a set of threads will be launched to generate a fractal image. You can see how to launch diferent threads to do a distributed task.

// Start calculation threads (we run one thread on each processor core)
cout << "Running " << numThreads << " calculation thread(s)..." << flush;
list<thread *> threadList;
for(int i = 0; i < numThreads; ++ i)
{
  thread * t = new thread(CalcThread, (void *) &dispatcher);
  threadList.push_back(t);
}

// Wait for the threads to complete...
for(list<thread *>::iterator i = threadList.begin(); i != threadList.end(); ++ i)
{
  thread * t = *i;
  t->join();
  delete t;
}
cout << "done!" << endl;

// Write the final image to a file
cout << "Writing final image..." << flush;
img.WriteToTGAFile("fractal.tga");
cout << "done!" << endl;

You can see all the code here.

$ bin/examples_tinythread_fractal

../../_static/img/c++/examples/fractal.jpg