Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <chrono>
- #include "linkedlist.h"
- // Kompilieren g++ -Wall -std=c++11 -pthread main.cpp -o main
- using namespace std;
- void add(LinkedList<int>& l, uint how_many){
- for(uint i = 0; i < how_many; i++){
- l.push_back(i);
- cout << "Thread #" << this_thread::get_id() << " adds on assignment " << i << endl;
- }
- }
- void del(LinkedList<int>& l, uint how_many){
- for(uint i = 0; i < how_many; i++){
- if(l.size() == 0){
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- int assignment = l.remove(0);
- cout << "Thread #" << this_thread::get_id() << " del on assignment " << assignment << endl;
- }
- }
- constexpr bool threaded = true;
- int main(){
- LinkedList<int> l;
- thread t1{add, ref(l), 1000};
- thread t2{del, ref(l), 1000};
- t1.join();
- t2.join();
- if(l.size() == 0)
- cout << "Success, all work done!" << endl;
- else
- cout << "Failure, some work left!" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement