Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. // lab1mmyller.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <thread>
  7. #include <string>
  8. #include <mutex>
  9. #include <vector>
  10. #include <chrono>
  11. #include <atomic>
  12.  
  13.  
  14. using namespace std;
  15. using namespace std::chrono;
  16.  
  17. std::mutex my_mutex;
  18. unsigned long long int my_int = 0;
  19. std::atomic_int atomic_ = 0;
  20.  
  21. void thread_function(string i) {
  22. cout << "Thread function no. "<< i << endl;
  23. }
  24.  
  25. void printing(string text) {
  26. for (unsigned int i = 0; i < 50; i++) {
  27. lock_guard<mutex> guard(my_mutex);
  28. cout << text << endl;
  29. }
  30. }
  31.  
  32. void incrementing() {
  33. for (unsigned long long int i = 0; i < 10000000; i++) {
  34. lock_guard<mutex> guard(my_mutex);
  35. my_int++;
  36. }
  37. cout << my_int << endl;
  38. }
  39.  
  40. void incrementing_atomic() {
  41. for (unsigned long long int i = 0; i < 10000000; i++) {
  42. atomic_++;
  43. }
  44. cout << atomic_ << endl;
  45. }
  46.  
  47. class FuncObject {
  48. public :
  49. void operator()(string i) const {
  50. thread_function(i);
  51. }
  52. };
  53.  
  54. class MyClass {
  55. public:
  56. void threading(string i) {
  57. thread_function(i);
  58. }
  59. };
  60.  
  61. int main()
  62. {
  63. FuncObject obj;
  64. thread my_thread(thread_function, "1");
  65. my_thread.join();
  66. thread my_thread3(obj, "3");
  67. my_thread3.join();
  68. MyClass my_class;
  69. thread my_thread4(&MyClass::threading, my_class, "4");
  70. my_thread4.join();
  71. thread my_thread5([](string i) {
  72. cout << "Thread function no. " << i << endl;
  73. }, "5");
  74.  
  75. my_thread5.join();
  76.  
  77. auto start = high_resolution_clock::now();
  78. thread my_thread6(incrementing);
  79. my_thread6.join();
  80. auto stop = high_resolution_clock::now();
  81. auto duration = duration_cast<milliseconds>(stop - start);
  82. cout << "Single incrementation with mutex: " << duration.count() << endl;
  83.  
  84. // 2
  85. for (int i = 0; i < 20; i++) {
  86. thread th(printing, "elo");
  87. th.join();
  88. }
  89. // With mutex
  90. std::vector<thread> my_threads;
  91. auto start_with_mutex = high_resolution_clock::now();
  92. for (int i = 0; i < 10; i++) {
  93. my_threads.emplace_back(incrementing);
  94. }
  95. for (int i = 0; i < 10; i++) {
  96. my_threads[i].join();
  97. }
  98. auto end_with_mutex = high_resolution_clock::now();
  99. cout << "Ten incrementattions with mutex: " << duration_cast<milliseconds>(end_with_mutex - start_with_mutex).count() << endl;
  100. std::vector<thread> my_threads2;
  101.  
  102. // With atomic
  103. auto start_with_atomic = high_resolution_clock::now();
  104. for (int i = 0; i < 10; i++) {
  105. my_threads2.emplace_back(incrementing_atomic);
  106. }
  107. for (int i = 0; i < 10; i++) {
  108. my_threads2[i].join();
  109. }
  110. auto end_with_atomic = high_resolution_clock::now();
  111. cout << "Ten incrementations with atomic: " << duration_cast<milliseconds>(end_with_atomic - start_with_atomic).count() << endl;
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement