Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 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. void add(LinkedList<int>& l, uint how_many){
  12. for(uint i = 0; i < how_many; i++){
  13. uint j = rand() % 10;
  14. l.push_back(i);
  15. this_thread::sleep_for(std::chrono::milliseconds(j));
  16. cout << "Thread #" << this_thread::get_id() << " adds on assignment " << i << " and slept for " << j << endl;
  17. }
  18. }
  19.  
  20. void del(LinkedList<int>& l, uint how_many){
  21. for(uint i = 0; i < how_many; i++){
  22. uint j = rand() % 10;
  23. if(l.size() == 0){
  24. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  25. }
  26. int assignment = l.remove(0);
  27. this_thread::sleep_for(std::chrono::milliseconds(j));
  28. cout << "Thread #" << this_thread::get_id() << " del on assignment " << assignment << " and slept for " << j << endl;
  29. }
  30. }
  31.  
  32. constexpr bool threaded = true;
  33. int main(){
  34. LinkedList<int> l;
  35.  
  36. thread t1{add, ref(l), 1000};
  37. thread t2{del, ref(l), 1000};
  38. t1.join();
  39. t2.join();
  40.  
  41. if(l.size() == 0)
  42. cout << "Success, all work done!" << endl;
  43. else
  44. cout << "Failure, some work left!" << endl;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement