Changes

Jump to: navigation, search

GPU621/CamelCaseTeam

1,383 bytes added, 17:24, 27 July 2021
no edit summary
Our team will do a research on comparison between c++ 11 threads library and openmp. We will code problems with both libraries to demonstrate the coding differences and do the performance analysis on those two approaches. We will highlight the techniques and best practices to achieve the best performance and avoid the pitfalls.
 
== Get start with c++ threading ==
 
C++ Threading is supported after c++11 standard, previously, it used pthread and windows api or boost library to do multithreading.
 
In order to start with threading in c++, we need to include <thread>, this includes thread functions and classes. For protecting shared data, we will need to include other headers like <mutex> <condition_variable>, etc.
 
std::thread work with all callable type, lambda, functor(function object), function pointer. Here are some examples:
 
1. function pointer
<syntaxhighlight lang="cpp">
void hello() {
std::cout << "hello wolrd" << std::endl;
}
std::thread a(hello);
</syntaxhighlight>
 
2. lambda
<syntaxhighlight lang="cpp">
int num1 = 0;
int num2 = 1;
 
std::thread b([]{
return num1 + num2;
});
</syntaxhighlight>
 
We need to decide If we want to wait for it to finish (join) or If we want it to run in the background (detach) before the thread object being deconstructed, because it will call std::terminate in the deconstructor and throw a error in runtime.
 
3. functor
<syntaxhighlight lang="cpp">
class Func {
void operator()() {
std::cout << "print in class f operator" << std::endl;
}
}
Func f();
std::thread c(f);
</syntaxhighlight>
 
t.join() -------- synchronized function called in parent thread to wait for the child thread to finish.
t.detach() ------- let threads to run in the background.
== C++11 Threading ==
10
edits

Navigation menu