3

i'm trying to build a simple encapsulation for a vector of std::thread; I want to initialize up to MAX amount of threads. If i reach the max amount, the main thread will run the code (writing to output files). I keep a Vector of a class containing std::thread and a bool flag. instead of running a function directly through std::threads constructor, I'm sending a wrapper function which will alter the boolean flag, so I would be able to check a thread if it has finished, and if not, keep on going (and if it has finished, join and close it). My Code for the wrapper:

#include <thread>

class RvThread {
    bool is_running;
    std:thread this_thread;

    template <typename F, typename ... Args>
    static void function_wrapper(bool* flag, F &&function, Args&& ... args) {
        function(args...);
        *is_running = false;
    }
public:
    template <typename F, typename ... Args>
    RvThread(F &&function, Args&& ... args) : is_running(true), this_thread(RvThread::function_wrapper<F, Args...>,&is_running,function, args...) {}
    ~RvThread(){
        join();
    }
    void join () {
        this_thread.join();
    }
    bool is_done() {
        return !is_running;
    }
};

template <unsigned char max_threads>
class Threads {
    unsigned char num_active;
    std::vector<RvThread> running_threads;
    void join_done(){
        for (auto &it : running_threads) {
            if (it->is_done()) {
                running_threads.remove(it);
                num_active--;
            }
        }
    }
public:
    Threads() : num_active(0) {}
    template <typename F, typename ... Args>
    void run(F&& function, Args&& ... args) {
        join_done();
        if (num_active < max_threads) {
            running_threads.push_back(RvThread(function, args...));
            num_active++;
        } else {
            function(args...);
        }
    }
};

and the function I'm trying to pass is:

template<typename C>
inline void writeFile(const Path &filePath, const C &content, bool append) {
    OutFile file(filePath, append);
    for(auto it=content.begin(); it!=content.end(); it++) {
        file.file << *it;
        file.file << '\n';
    }
}
template<>
inline void writeFile(const Path &filePath, const RvString &content, bool append) {
    OutFile file(filePath, append);
    file.file << content;
}

template<typename C>
void FileManager::writeRawFile(const Path &filePath, const C &content, bool append, bool FileMayBeRead) {
    writeFile(filePath.is_absolute() ? filePath : baseDir/filePath, content, append);
    auto it = sourceFiles.find(filePath.filename().string());
    if (it != sourceFiles.end()) {
        sourceFiles.erase(it);
    }

    if (FileMayBeRead && paths.find(filePath.filename().string())==paths.end()) {
        paths.insert(make_pair(filePath.filename().string(), filePath));
    }
}

template<typename C>
void FileManager::writeRawFile_t(Path filePath, C &&content, bool append, bool FileMayBeRead, bool run_thread) {
    static Threads<8> WritingThreads;
    if (run_thread) {
        WritingThreads.run(writeFile, std::move(filePath.is_absolute() ? filePath : baseDir/filePath), std::move(content), std::move(append));
    } else {
        writeFile(filePath.is_absolute() ? filePath : baseDir/filePath, content, append);
    } ...

I keep on getting errors of unresolved overloaded function type and no type named ‘type’ in ‘class std::result_of

J. Doe
  • 161
  • 2
  • 2
  • 5

0 Answers0