The random ramblings of a techie

February, 2013

Simple data sharing between theads

Passing parameters by reference

Multi-threading allows you to execute portions of your code concurrently. While on a single CPU the benefits of multi-threading are limited, multi-core systems allow true concurent execution, thus reducing the time it takes for the task to finish.

The basic threading is easy, you just declare a function and run it in thread.

#include <thread>
#include <iostream>
using namespace std;

void complicatedCalculation1()
{//Do stuff here}

void complicatedCalculation2()
{//Do stuff here}

int main()
{
   //Creates a thread and starts it
   thread calc1Thread(complicatedCalculation1);

   //Creates a thread and starts it
   thread calc2Thread(complicatedCalculation1);

   //Wait for each thread to finish
   calc1Thread.join();
   calc2Thread.join();

   cout<<"All done"<<endl;
   return 0;
}

>>

, , , ,