Files
MultiThreading/main.cpp
2025-04-08 16:52:41 -04:00

52 lines
1.9 KiB
C++

#include <MultiThreading/Task.h>
#include <MultiThreading/Thread.h>
#include <MultiThreading/ThreadPool.h>
#include <iostream>
using namespace MultiThreading;
void some_test_func(int32_t hello) {
for (unsigned int i = 0; i < 4000000000; i++)
std::cout << "test" << 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<void>::Create([] { return some_test_func(4); });
// 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_task);
// Some work running concurrently with the worker thread.
for (unsigned int i = 0; i < 1000000000; 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()) {}
// At this point, the thread is sleeping and a new task can be pushed on.
auto some_other_task = Task<void>::Create(std::bind(some_test_func, 5));
if (!some_thread.SetTask(some_other_task))
throw std::runtime_error("Error while pushing the task on-to the thread");
std::cout << some_thread.Busy() << std::endl;
// Because the thread exists already and is waiting for jobs, We don't get the penalty of the thread start-up time.
while (!some_other_task->Complete()) { std::cout << some_thread.Busy() << std::endl; }
std::cout << a << std::endl;
}
*/
int main() {
ThreadPool thread_pool(1);
auto some_task_1 = Task<void>::Create(([] { return some_test_func(1); }));
thread_pool.Enqueue(some_task_1);
std::cout << thread_pool.ThreadCount() << std::endl;
//delete thread_pool;
}