Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <chrono>
  4. #include "linkedlist.h"
  5. #include <mutex>
  6.  
  7. // Kompilieren g++ -Wall -std=c++11 -pthread main.cpp -o main
  8.  
  9. using namespace std;
  10.  
  11. mutex p;
  12.  
  13. void work(LinkedList<int>& l, uint how_many){
  14. for(uint i = 0; i < how_many; i++){
  15. p.lock();
  16. size_t size = l.size();
  17. int assignment = l.remove(size-1);
  18. cout << "Thread #" << this_thread::get_id() << " works on assignment " << assignment << endl;
  19. p.unlock();
  20. }
  21. }
  22.  
  23. constexpr bool threaded = true;
  24. int main(){
  25. LinkedList<int> l;
  26. for(uint i = 0; i < 1000; i++){
  27. l.push_back(i);
  28. }
  29.  
  30. if(threaded){
  31. thread t1{work, ref(l), 500};
  32. thread t2{work, ref(l), 500};
  33. t1.join();
  34. t2.join();
  35. } else {
  36. work(l, 1000);
  37. }
  38.  
  39. if(l.size() == 0)
  40. cout << "Success, all work done!" << endl;
  41. else
  42. cout << "Failure, some work left!" << endl;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement