How to create a multiple threads application in C++

c++, multithreading

Solution

Here's a simple example:

#include <iostream>
#include <thread>

void f1() { std::cout << "This is function 1.\n"; }
void f2() { std::cout << "This is a different function, let's say 2.\n"; }

int main()
{
    std::thread t1(f1), t2(f2);   // run both functions at once

    // Final synchronisation:
    // All running threads must be either joined or detached
    t1.join();
    t2.join();
}

If your functions need to produce return values, you should combine the above thread objects with `std::packaged_task` runnable objects, available from `<future>`, which give you access to the return value of the thread function.

Problem

Possible Duplicate: Simple example of threading in C++ Can someone please give me an example how to create a simple application in C++ that runs two functions simultaneously? I know that this question have connections with thread management and multi-threading, but I'm basically a php programmer and I'm not really familiar with advanced C++ programming.

Original source

Related problems