Simple data sharing between theads

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

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

Continue reading “Simple data sharing between theads”