49
edits
Changes
→Case B - Using the Parallel Stacks and the Parallel Watch Window
==Case B - Using the Parallel Stacks and the Parallel Watch Window==
```
#include <iostream>
#include <thread>
#include <vector>
#include<omp.h>
#include <chrono>
#include <string>
using namespace std;
void print(int n, const std::string& str) {
std::string msg = std::to_string(n) + " : " + str;
std::cout << msg << std::endl;
}
int main() {
std::vector<std::string> s = {
"Hello World",
"Hello",
"This",
"is",
"a testing",
"code",
"for",
"multithread",
"debugging"
};
std::vector<std::thread> threads;
for (int i = 0; i < s.size(); i++) {
threads.push_back(std::thread(print, i, s[i]));
}
for (auto& th : threads) {
th.join();
}
return 0;
}
```
The main program calls the print function for the size of the string vector.
Threads are open when it is called and join back on line 33.
'''Setup:'''
Set breakpoints at the definition of the function, and when join() is called.
Debug using F5 or by clicking debug from the top menu bar.
Here is the output of the program:
```
0 : Hello World
3 : is
6 : for
2 : This
5 : code
7 : multithread
8 : debugging
1 : Hello
4 : a testing
```
There are 9 different threads that were being created and used every time print() is called.
Using Parallel Stacks Windows, we are able to see the call stack information through a UI.
'''Walkthrough:'''
First call
[[File:parallelstackswalkthrough1]]
It is being called at the definition of the function "print()"
Forked threads are being joined after their usage.
https://en.wikipedia.org/wiki/Help:Cheatsheet