Advertisement
Guest User

Untitled

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