Advertisement
Guest User

SC lab1

a guest
Mar 22nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <thread>
  4. #include <mutex>
  5. #include <string>
  6. #include <chrono>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <atomic>
  10. using namespace std;
  11.  
  12.  
  13. /////////////////////////////////////////////1
  14. void foo(int Z)
  15. {
  16.     for (int i = 0; i < Z; i++) {
  17.         cout << "Thread - global function\n";
  18.     }
  19. }
  20.  
  21.  
  22. class bar {
  23. public:
  24.  
  25.     void operator()(int x)
  26.     {
  27.         for (int i = 0; i < x; i++)
  28.             cout << "Thread - function object\n";
  29.     }
  30.  
  31.     void foo(int ii) {
  32.         for (int y = 0; y < ii;y++) {
  33.             cout << "Thread - class method\n";
  34.         }
  35.     }
  36. };
  37. ////////////////////////////////////////////1
  38.  
  39.  
  40. ////////////////////////////////////////////2
  41.  
  42. mutex mu;
  43.  
  44. static const int threads_number = 20;
  45.  
  46. void printText(string text) {
  47.     for (int i = 0; i < 50; i++) {
  48.         cout << i << " " << text << endl;
  49.     }
  50. }
  51.  
  52. void func(string msg)
  53. {
  54.     mu.lock();
  55.     printText(msg);
  56.     mu.unlock();
  57. }
  58.  
  59. ////////////////////////////////////////////2
  60.  
  61.  
  62. ////////////////////////////////////////////3
  63.  
  64. int globalIntegerVariable = 0;
  65. void increment() {
  66.     for (int i = 0;i < 10000000;i++) {
  67.         globalIntegerVariable++;
  68.     }
  69. }
  70.  
  71. void funcA() {
  72.  
  73. }
  74.  
  75. void funcB() {
  76.  
  77. }
  78.  
  79.  
  80. ////////////////////////////////////////////3
  81.  
  82.  
  83.  
  84.  
  85. int main()
  86. {
  87.     /*
  88.     Task 1
  89.     */
  90.     thread th1(foo, 50);
  91.  
  92.     thread th2(bar(), 50);
  93.  
  94.     auto f = [](int x) {
  95.         for (int i = 0; i < x; i++)
  96.             cout << "Thread - lambda expression\n";
  97.     };
  98.  
  99.     thread th3(f, 50);
  100.  
  101.  
  102.     thread th4(&bar::foo, bar(), 50);
  103.    
  104.  
  105.     th1.join();
  106.     th2.join();
  107.     th3.join();
  108.     th4.join();
  109.  
  110.  
  111.     /*
  112.     Task 2
  113.     */
  114.  
  115.     thread th[threads_number];
  116.     for (int i = 0; i < threads_number; i++) {
  117.         th[i] = thread(&func, "Text to print");
  118.     }
  119.     for (int i = 0; i < threads_number; ++i) {
  120.         th[i].join();
  121.     }
  122.  
  123.  
  124.     /*
  125.     Task 3
  126.     */
  127.  
  128.     using clock = std::chrono::steady_clock;
  129.  
  130.     vector<thread> workers;
  131.     for (int i = 0; i < 10; i++) {
  132.         workers.push_back(thread([]()
  133.         {
  134.             cout << "thread function\n";
  135.         }));
  136.     }
  137.  
  138.     clock::time_point start = clock::now();
  139.    
  140.  
  141.     clock::time_point end = clock::now();
  142.     clock::duration execution_time = end - start;
  143.  
  144.     cout << endl<< execution_time.count()<<endl;
  145.  
  146.     system("pause");
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement