Advertisement
Guest User

Untitled

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