dev-func-drop
Brett 2025-01-13 19:43:41 -05:00
parent 99b784835b
commit fca412ea29
4 changed files with 113 additions and 1 deletions

View File

@ -27,7 +27,7 @@ macro(compile_options target_name)
sanitizers(${target_name})
endmacro()
project(blt-gp VERSION 0.3.6)
project(blt-gp VERSION 0.3.7)
include(CTest)

View File

@ -52,6 +52,7 @@
#include <blt/gp/stack.h>
#include <blt/gp/config.h>
#include <blt/gp/random.h>
#include <blt/gp/threading.h>
#include "blt/format/format.h"
namespace blt::gp

105
include/blt/gp/threading.h Normal file
View File

@ -0,0 +1,105 @@
#pragma once
/*
* Copyright (C) 2024 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_GP_THREADING_H
#define BLT_GP_THREADING_H
#include <blt/std/types.h>
#include <blt/std/thread.h>
#include <thread>
#include <functional>
#include <atomic>
namespace blt::gp
{
template <typename Parallel, typename Single = void>
class task_t
{
public:
task_t(Parallel parallel, Single single): parallel(parallel), single(single), requires_single_sync(true)
{
}
explicit task_t(Parallel parallel): parallel(parallel), requires_single_sync(false)
{
}
void call_parallel(size_t thread_index)
{
parallel(thread_index);
}
void call_single()
{
single();
}
private:
Parallel parallel;
Single single;
bool explicit_sync_begin : 1 = true;
bool explicit_sync_end : 1 = true;
bool requires_single_sync : 1 = false;
};
template <typename... Tasks>
class task_storage_t
{
};
class thread_manager_t
{
public:
explicit thread_manager_t(const size_t thread_count, const bool will_main_block = true): barrier(thread_count),
will_main_block(will_main_block)
{
for (size_t i = 0; i < will_main_block ? thread_count - 1 : thread_count; ++i)
{
threads.emplace_back([i, this]()
{
while (should_run)
{
}
});
}
}
~thread_manager()
{
should_run = false;
for (auto& thread : threads)
{
if (thread.joinable())
thread.join();
}
}
private:
[[nodiscard]] size_t thread_count() const
{
return will_main_block ? threads.size() + 1 : threads.size();
}
barrier barrier;
std::atomic_bool should_run = true;
bool will_main_block;
std::vector<size_t> tasks;
std::vector<std::thread> threads;
};
}
#endif //BLT_GP_THREADING_H

View File

@ -165,6 +165,12 @@ void do_run()
std::cout << std::endl;
}
template<typename What, typename What2>
auto what(What addr, What2 addr2) -> decltype(addr + addr2)
{
return addr + addr2;
}
int main()
{
for (int i = 0; i < 1; i++)