-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
90 lines (71 loc) · 1.86 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
*DESCRIPTION : IITH STM transactional barrier.
Example application to verify the transactional barriers.
g++ -std=gnu++0x -ltbb
compatible to c++11 and further c++ versions
*AUTHOR : AJAY SINGH
*COMPANY : IIT Hyderabad
*/
#include<iostream>
#include "txBarrier.h"
#include <thread>
#include <sstream> //for stringstream
//default values
const int NUM_THREADS = 4;
using namespace std;
int arr[NUM_THREADS] = {0}; //test array to test the functionality of the transactional barrier.
void transaction(int i)
{
barrier(0);
for(int indx =0; indx< 100; indx++)
{
arr[i]++;
}
barrier(1);
for(int indx =0; indx< 100; indx++)
{
arr[i]--;
}
barrier(2);
for(int indx =0; indx< 100; indx++)
{
arr[i]++;
}
barrier(3);
for(int indx =0; indx< 100; indx++)
{
arr[i]--;
}
}
int main()
{
//init the tx barrier initially
tx_barrier_init(NUM_THREADS);
/*The test array state prior to the execution of barrier based transaction function*/
cout<<"test array prior to barrier based execution:"<<endl;
for (int i = 0; i < NUM_THREADS; ++i)
{
cout<<arr[i]<<" ";
}
cout<<endl;
/*Spawn threads to execute the test application named transcation()*/
thread *t = new thread [NUM_THREADS];
t = new thread [NUM_THREADS];
for (int i = 0; i < NUM_THREADS; ++i)
{
t[i] = thread(&transaction, i);
}
//cout << "main thread\n";
for (int i = 0; i < NUM_THREADS; ++i)
{
t[i].join();
}
/*The test array state post execution of barrier based transaction function*/
cout<<"test array post to barrier based execution:"<<endl;
for (int i = 0; i < NUM_THREADS; ++i)
{
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}