Open main menu

CDOT Wiki β

Changes

DPS921/Team team

2,080 bytes added, 17:52, 4 December 2020
no edit summary
Mutex’s are the lock mechanism mentioned above. The purpose of a mutex is to make an area of memory accessible only to the thread that has accessed it. Once a thread is done with its changes to the memory, the thread will unlock it, move on, and allow the next thread to make its changes.<br>
Mutex’s in C++ are introduced with the <Mutex> header file from the std::mutex class. A mutex has two main functions, std::mutex::lock() and std::mutex::unlock().<br>
<br>
Mutex code example:
<pre>
// mutex example
#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex
#include <vector>
 
// THIS IS THE MUTEX LOCK EXAMPLE
std::mutex mtx; // mutex for critical section
 
void print_block(int n, char c) {
// critical section (exclusive access to std::cout signaled by locking mtx):
 
mtx.lock();//comment out these lines to remove mutex
 
for (int i = 0; i < n; ++i) { std::cout << c; }
std::cout << '\n';
 
mtx.unlock();//comment out these lines to remove mutex
}
 
int main()
{
std::thread th1(print_block, 50, '*');
std::thread th2(print_block, 50, '$');
<pre> th1.join(); th2.join();
return 0;
}
</pre>
ResultResults with the mutexes in place:
<pre>
Money in Wallet is: 5000$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**************************************************
</pre>
<b>If the locks are removed , the output will change every run, but . The results will look similar to:</b>
<pre>
results$$$$$$$**************************************************$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
</pre>
 
=== Atomicity ===
The Atomic operations library throws out the concept of using locks and allows for lockless concurrent programming. The atomic class handles everything for you when it comes to making sure the memory space does not encounter bugs when running between multiple threads.
<br>
<br>
Atomic code example:
<pre>
#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex
#include <vector>
#include <atomic>
 
 
//THIS IS THE ATOMIC CODE //Result is the same as if you were to use mutex locks. Wallet will contain 5000
class Wallet
{
std::atomic<int> mMoney;
public:
Wallet() :mMoney(0) {}
int getMoney() { return mMoney; }
void addMoney(int money)
{
for (int i = 0; i < money; ++i)
{
mMoney++;
}
}
};
 
 
int testMultithreadedWallet()
{
Wallet walletObject;
std::vector<std::thread> threads;
for (int i = 0; i < 5; ++i) {
threads.push_back(std::thread(&Wallet::addMoney, &walletObject, 1000));
}
for (int i = 0; i < threads.size(); i++)
{
threads.at(i).join();
}
return walletObject.getMoney();
}
 
int main()
{
 
int val = 0;
for (int k = 0; k < 1000; k++)
{
if ((val = testMultithreadedWallet()) != 5000)
{
std::cout << "Error at count = " << k << " Money in Wallet = " << val << std::endl;
//break;
}
}
 
std::cout << "Money in Wallet is: " << val << std::endl;
 
return 0;
}
</pre>
Results:
<pre>
Money in Wallet is: 5000
</pre>
== Intel Threading Building Blocks ==
== Case Studies between STL & TBB ==
<u>Comparison of TBB parallel sort, STL sort</u>
<pre>
#include <stddef.h>
STL parallel sort performed similarly to TBB parallel sort<br>
<br>
<u>Comparison of TBB inclusive scan, STL inclusive scan</u>
<pre>
</pre>
Results:
18
edits