In this section, we'll learn how to use std::thread with a basic scenario (create and join) and how to pass and receive parameters to it:
- std::thread: By using the basic thread methods, create and join, write the following code:
#include <iostream>
#include <thread>
void threadFunction1 ();
int main()
{
std::thread t1 {threadFunction1};
t1.join();
return 0;
}
void threadFunction1 ()
{
std::cout << "starting thread 1 ... " << std::endl;
std::cout << "end thread 1 ... " << std::endl;
}
- Compile it with g++ concurrency_01.cpp -lpthread.
The second example is similar to the previous one but in this case, we pass and get parameters:
- std::thread: Create and join a thread, passing a parameter and getting a result. Write the following code:
#include <iostream>
#include <thread>
#include <vector>
#include <algorithm>
void threadFunction (std::vector<int> &speeds, int& res);
int main()
{
std::vector<int> speeds = {1, 2, 3, 4, 5};
int result = 0;
std::thread t1 (threadFunction, std::ref(speeds),
std::ref(result));
t1.join();
std::cout << "Result = " << result << std::endl;
return 0;
}
void threadFunction (std::vector<int> &speeds, int& res)
{
std::cout << "starting thread 1 ... " << std::endl;
for_each(begin(speeds), end(speeds), [](int speed)
{
std::cout << "speed is " << speed << std::endl;
});
res = 10;
std::cout << "end thread 1 ... " << std::endl;
}
- Compile it using g++ concurrency_02.cpp -lpthread.
The third example uses async to create a task, execute it, and get the result, as follows:
- std::async: Here, we can see why async is called task-based threading. Write the following code:
root@b6e74d5cf049:/Chapter2# cat concurrency_03.cpp
#include <iostream>
#include <future>
int asyncFunction ();
int main()
{
std::future<int> fut = std::async(asyncFunction);
std::cout << "max = " << fut.get() << std::endl;
return 0;
}
int asyncFunction()
{
std::cout << "starting asyncFunction ... " << std::endl;
int max = 0;
for (int i = 0; i < 100000; ++i)
{
max += i;
}
std::cout << " Finished asyncFunction ..." << std::endl;
return max;
}
- Now, we need to compile the program. There is a catch here. Since we're using a threading mechanism, the compilers rely on the native implementations, which in our case turn out to be pthread. In order to compile and link without errors (we'd get an undefined reference), we need to include -lpthread:
g++ concurrency_03.cpp -lpthread
In the fourth example, std::async used in conjunction with std::promise and std::future is a good and easy way of making two tasks communicate with each other. Let's take a look:
- std::async: This is another std::async example showing a basic communication mechanism. Let's code it:
#include <iostream>
#include <future>
void asyncProducer(std::promise<int> &prom);
void asyncConsumer(std::future<int> &fut);
int main()
{
std::promise<int> prom;
std::future<int> fut = prom.get_future();
std::async(asyncProducer, std::ref(prom));
std::async(asyncConsumer, std::ref(fut));
std::cout << "Async Producer-Consumer ended!" << std::endl;
return 0;
}
void asyncConsumer(std::future<int> &fut)
{
std::cout << "Got " << fut.get() << " from the producer ... "
<< std::endl;
}
void asyncProducer(std::promise<int> &prom)
{
std::cout << " sending 5 to the consumer ... " << std::endl;
prom.set_value (5);
}
- And finally, compile it: g++ concurrency_04.cpp -lpthread