-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
25 lines (21 loc) · 718 Bytes
/
Copy pathexample.cpp
File metadata and controls
25 lines (21 loc) · 718 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "threadpool.hpp"
#include <iostream>
#include <memory>
#include <mutex>
#include <vector>
int main() {
tp::ThreadPool threadpool; // Create a threadpool
std::mutex mutex;
std::vector<std::shared_ptr<tp::Task>> tasks; // Vector to store threadpool tasks
for (unsigned int i = 0; i < 10; ++i) {
tasks.push_back(threadpool.schedule([i, &mutex]() {
std::lock_guard<std::mutex> lock(mutex);
std::cout << "Printing from task: " << i << std::endl;
})); // Schedule tasks and add them to the vector
}
for (const auto& task : tasks) { // Wait on every task
task->wait();
}
std::cout << "All tasks done!" << std::endl;
return 0;
}