44
edits
Changes
no edit summary
{{GPU621/DPS921 Index | 20187}}
<!-- How Threads Works -->
<h4>Implicit Barrier</h4>
<pre class="code">// OpenMP - Parallel Construct
// omp_parallel.cpp
<p>Output:</p>
<pre class="code">Hello
Hello
Hello
Hello
Hello
Hello
Fin
</pre>
<!-- C++11 Threads -->
<p>Unlike OpenMP, C++11 does <i>not</i> use parallel regions as barriers for its threading. When a thread is run using the C++11 thread library, we must consider the scope of the parent thread. If the parent thread would exit before the child thread can return, it can crash the program if not handled correctly.</p>
<p>When using the join function on the child thread, the parent thread will be blocked until the child thread returns.</p>
<pre class="code"> t2
____________________
/ \
__________/\___________________|/\__________
t1 t1 t2.join() | t1
</pre>
<h4>Creating a Thread</h4><p>The following is the template used for the overloaded thread constructor. The thread begins to run on initialization.<br>f is the function, functor, or lambda expression to be executed in the thread. args are the arguements to pass to f.</p><pre class==== Implicit Barrier ===="code">template<class Function, class... Args>explicit thread(Function&& f, Args&&... args);</pre>
<!-- How Multithreading Works -->
<pre class= Threading in C++11 ="code">#include <iostream>#include <omp.h>
int main() {
#pragma omp parallel
{
int tid = omp_get_thread_num();
std::cout << "Hi from thread "<< tid << '\n';
}
return 0;
}
</pre>
<p>Essentially what is happening in the code above is that the threads are intermingling creating a jumbled output. All threads are trying to access the cout stream at the same time. As one thread is in the stream another may interfere with it because they are all trying to access the stream at the same time. </p>
<h3>Threading with C++11</h3>
<p>Unlike OpenMP, C++11 threads are created by the programmer instead of the compiler.</p>
<p>std::this_thread::get_id() is similar to OpenMP's omp_get_thread_num() but instead of an int, it returns a </p>
std::cout << "Creating threads...\n";
for (int i === Creating a Thread ===0; i < numThreads; i++) threads.push_back(std::thread(func1, i));
std::cout << "All threads have launched!\n";
std::cout << "Syncronizing...\n";
return 0;
}
</pre>
<p>Since all threads are using the std::cout stream, the output can appear jumbled and out of order. The thread can take in a function, functor, or lambda expression as its first argument, followed by 0 or more arguments solution to this problem will be passed into presented in the functionnext section.</p>
<h2>How Syncronization Works</h2>
<pre class="code">#include <iostream>
#include <omp.h>
int main()
{
#pragma omp parallel
{
int tid = omp_get_thread_num();
#pragma omp critical
std::cout << "Hi from thread "<< tid << '\n';
}
return 0;
}
</pre>
<pre class="code">Hi from thread 0
Hi from Thread 1
Hi from thread 2
Hi from thread 3
</pre>
<h4>parallel for</h4>
<h3>Syncronization with C++11</h3>
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
int main() {
int numThreads = 10;
for (int i = 0; i < numThreads; i++)
threads.push_back(std::thread(func1, i));
for (auto& thread : threads)
thread.join();
<p>Using mutex, we're able to place a lock on the data used by the threads to allow for mutual exclusion. This is similar to OpenMP's critical in that it only allows one thread to execute a block of code at a time.</p>
<pre class==== Using Atomic ===="code">Creating threads...Index: 0 - ID: 0x70000aa29000Index: 4 - ID: 0x70000ac35000Index: 5 - ID: 0x70000acb8000Index: 1 - ID: 0x70000aaac000Index: 6 - ID: 0x70000ad3b000Index: 7 - ID: 0x70000adbe000Index: 8 - ID: 0x70000ae41000Index: 3 - ID: 0x70000abb2000All threads have launched!Syncronizing...Index: 9 - ID: 0x70000aec4000Index: 2 - ID: 0x70000ab2f000All threads have syncronized!</pre>