#include <condition_variable> #include <iostream> #include <mutex> #include <thread> #include <vector> #include <queue> #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <stack> using namespace std; using namespace std::chrono; pthread_mutex_t mutx = PTHREAD_MUTEX_INITIALIZER; class Task{ private: string name; double size; int priority; public: bool operator<(const Task& task) const{ if(this->priority == task.priority){ return this->size > task.size; } else{ return this->priority > task.priority; } } Task(const string &name, double size, int priority) : name(name), size(size), priority(priority) {} const string &getName() const { return name; } void setName(const string &name) { Task::name = name; } double getSize() const { return size; } void setSize(double size) { Task::size = size; } int getPriority() const { return priority; } Task() { this->priority = 0; this->size = 0; this->name = ""; } void setPriority(int prior) { Task::priority = prior; } }; class PrintQueue{ public: void push(Task task){ this->printqueue.push(task); } Task top(){ return this->printqueue.top(); } void pop(){ this->printqueue.pop(); } size_t size(){ return this->printqueue.size(); } private: priority_queue<Task> printqueue; }; PrintQueue printQueue; std::string FormatTimeNow(const char* format) { auto now = system_clock::now(); time_t now_c = system_clock::to_time_t(now); tm* now_tm = localtime(&now_c); char buf[20]; strftime(buf, sizeof(buf), format, now_tm); return string(buf); } class Semaphore { public: explicit Semaphore(int count = 0) : count_(count) {} void Signal() { unique_lock<mutex> lock(mutex_); ++count_; cv_.notify_one(); } void Wait() { unique_lock<std::mutex> lock(mutex_); cv_.wait(lock, [=] { return count_ > 0; }); --count_; } private: std::mutex mutex_; std::condition_variable cv_; int count_; }; Semaphore g_semaphore(4); std::mutex g_io_mtx; void DoWork() { g_semaphore.Wait(); thread::id thread_id = this_thread::get_id(); string now = FormatTimeNow("%H:%M:%S"); { lock_guard<std::mutex> lock(g_io_mtx); cout << "Thread " << thread_id << ": wait succeeded" << " (" << now << ")" << endl; } std::this_thread::sleep_for(seconds(1)); g_semaphore.Signal(); } void InsertTask(Task& task){ if(pthread_mutex_lock(&mutx) != 0) { perror("pthread_mutex_lock"); exit(EXIT_FAILURE); } //加锁成功 printQueue.push(task); if(pthread_mutex_unlock(&mutx) != 0) { perror("pthread_mutex_unlock"); exit(EXIT_FAILURE); } //退锁 } // 删除打印任务,同时必须使其他试图执行打印以及添加任务的线程阻塞 void delete_task(string name){ if(pthread_mutex_lock(&mutx) != 0) { perror("pthread_mutex_lock"); exit(EXIT_FAILURE); } queue<Task> temp_queue; for (size_t i = 0; i < printQueue.size(); ++i) { if(printQueue.top().getName() == name) { printQueue.pop(); break; } temp_queue.push(printQueue.top()); printQueue.pop(); } if(!temp_queue.empty()) { printQueue.push(temp_queue.front()); temp_queue.pop(); } if(pthread_mutex_unlock(&mutx) != 0) { perror("pthread_mutex_unlock"); exit(EXIT_FAILURE); } } void DoPrint(){ if(pthread_mutex_lock(&mutx) != 0) { perror("pthread_mutex_lock"); exit(EXIT_FAILURE); } Task task = printQueue.top(); cout << "正在打印" << task.getName() << "的内容," << "大小为" << task.getSize() << "KB" << endl; if(pthread_mutex_unlock(&mutx) != 0) { perror("pthread_mutex_unlock"); exit(EXIT_FAILURE); } //释放锁 } void Print_Ongoing_Task(){ if(pthread_mutex_lock(&mutx) != 0) { perror("pthread_mutex_lock failed"); exit(EXIT_FAILURE); } PrintQueue temp_queue; size_t size = printQueue.size(); for (int i = 0; i < size; ++i) { printf("第%d个打印任务是%s,大小为%f,优先级为%d",i,printQueue.top().getName().c_str(),printQueue.top().getSize(),printQueue.top().getPriority()); temp_queue.push(printQueue.top()); printQueue.pop(); } printQueue = temp_queue; if(pthread_mutex_unlock(&mutx) != 0) { perror("pthread_mutex_unlock failed"); exit(EXIT_FAILURE); } } int Sem_print() { // int threadNum = 4; // std::vector<std::thread> v; // v.reserve(threadNum); // for (std::size_t i = 0; i < threadNum; ++i) { // v.emplace_back(&DoWork); // } // for (std::thread& t : v) { // t.join(); // } string name;; double size; int prior; Task task; while(true){ cout << "请输入你要进行的操作: 1:新增打印任务; 2: 删除打印任务; 3:查看打印任务完成情况 ;0: 退出答打印程序"<< endl; int cmd; cin >> cmd; switch (cmd) { case 1: cout << "请输入名字"<<endl; cin >>name; cout <<"请输入大小"<<endl; cin >> size; cout << "请输入优先级"<< endl; cin >>prior; task = Task(name,size,prior); InsertTask(task); break; case 2: cout <<"请输入你要删除的打印任务的名称" <<endl; cin >> name; delete_task(name); break; case 3: Print_Ongoing_Task(); break; default: break; } } return 0; }
111111
最新推荐文章于 2024-08-16 23:08:36 发布