Files
MultiThreading/main.cpp

56 lines
1.9 KiB
C++

#include <MultiThreading/Task.h>
#include <MultiThreading/ThreadPool.h>
#include <MultiThreading/CollisionGuard.h>
#include <iostream>
using namespace MultiThreading;
//void cb() { std::cout << "hi" << std::endl; }
void some_test_func() {
for (unsigned int i = 0; i < 1000000000; i++) {}
std::cout << "task finishes." << std::endl;
}
/*
int main() {
// Each task you create can be run by a thread only once. It's marked as complete after.
// If you're running a lambda or std::function directly on the thread, It can be used multiple times.
int32_t a = 0;
auto some_task = Task<int32_t>::Create([] { return some_test_func(4); }, &a);
// You can start threads in place like this, but you have to wait for the amount of time
// it takes for the thread to start up which in many cases is longer than the job. Use thread pool.
Thread some_thread;
//some_thread.SetTaskCompletionCallback(cb);
some_thread.SetTask(some_task);
//while (!some_task->Complete()) {}
//std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::cout << some_thread.Busy() << std::endl;
// Some work running concurrently with the worker thread.
//for (unsigned int i = 0; i < 10; i++) {}
// When we get to the point in our code where we need the result from the task wait until it's finished.
//while (!some_task->Complete()) {}
//std::cout << a << std::endl;
}
*/
int main() {
srand(time(nullptr));
CollisionGuard<int> task_completion_count(0);
auto* thread_pool = new ThreadPool();
for (unsigned int i = 0; i < 128; i++) {
auto some_task = Task<void>::Create([&task_completion_count]() { task_completion_count += 1; }, [&task_completion_count]() { task_completion_count += 1; });
thread_pool->Enqueue(some_task);
}
delete thread_pool;
std::cout << "The number of tasks run was " << (int) task_completion_count << std::endl;
}