Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <chrono>
- #include "linkedlist.h"
- #include <mutex>
- // Kompilieren g++ -Wall -std=c++11 -pthread main.cpp -o main
- using namespace std;
- mutex p;
- void work(LinkedList<int>& l, uint how_many){
- for(uint i = 0; i < how_many; i++){
- p.lock();
- size_t size = l.size();
- int assignment = l.remove(size-1);
- cout << "Thread #" << this_thread::get_id() << " works on assignment " << assignment << endl;
- p.unlock();
- }
- }
- constexpr bool threaded = true;
- int main(){
- LinkedList<int> l;
- for(uint i = 0; i < 1000; i++){
- l.push_back(i);
- }
- if(threaded){
- thread t1{work, ref(l), 500};
- thread t2{work, ref(l), 500};
- t1.join();
- t2.join();
- } else {
- work(l, 1000);
- }
- 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