mercredi 23 décembre 2020

example C++ threadpool is crashing

I am learning C++ and purchased a book on concurrency. In it there are example classes... particularly the threadpool.cpp is of interest to me to practice before I make my own ObjectPool ...however it just crashes continuously on my mac.

Have I stumbled on something that apple clang doesn't support ? I am about to try switching to the target architecture but really want to avoid it and keep iterating on my mac... Thanks for looking.

Output from my run

% ./build/bin/test_cpp_multi
About to create my_threadpool....
thread_pool()... hardware_concurrency() = 2
push_back!push_back!Done!
Done creating my_threadpool....
zsh: illegal hardware instruction  ./build/bin/test_cpp_multi

helloworld.cpp

#include <future>
#include <thread>
#include <vector>
#include <atomic>
#include <iostream>
#include <unistd.h>

#include "threadsafe_queue.hpp"

struct join_threads
{
    join_threads(std::vector<std::thread>&)
    {}
};

class thread_pool
{
    std::atomic_bool done;
    threadsafe_queue<std::function<void()>> work_queue;
    std::vector<std::thread> threads;
    join_threads joiner;

    void worker_thread()
    {
        while(!done)
        {
            std::function<void()> task;
            if(work_queue.try_pop(task))
            {
                task();
            }
            else
            {
                std::this_thread::yield();
            }
        }
    }
public:
    thread_pool(): done(false),joiner(threads) {
        std::cout << "thread_pool()...";
        //unsigned const thread_count=std::thread::hardware_concurrency();
        
        unsigned const thread_count=2;
        
        std::cout << " hardware_concurrency() = " << thread_count << std::endl;
        try
        {
            for(unsigned i=0;i<thread_count;++i)
            {
                threads.push_back(std::thread(&thread_pool::worker_thread,this));
                std::cout << "push_back!";
            }
            std::cout << "Done!\n";
        }
        catch(...)
        {
            done=true;
            throw;
        }
    }

    ~thread_pool()
    {
        done=true;
    }

    template<typename FunctionType>
    void submit(FunctionType f)
    {
        work_queue.push(std::function<void()>(f));
    }
};

void find_the_answer_to_ltuae(){
    std::cout << "About to sleep 10 seconds...";
    sleep(10);
    std::cout << 100 << std::endl;
}

int main()
{
    std::cout << "About to create my_threadpool...." << std::endl;
    thread_pool my_threadpool;
    std::cout << "Done creating my_threadpool...." << std::endl << "Submitting first job now";
    my_threadpool.submit(find_the_answer_to_ltuae);
    std::cout<<"The answer is " << 42 << std::endl;
    sleep(100);
}

the threadsafe_queue.cpp is the same as the reference material found at https://github.com/anthonywilliams/ccia_code_samples/blob/main/listings/listing_4.5.cpp

threadsafe_queue.cpp

#include <mutex>
#include <condition_variable>
#include <queue>
#include <memory>

template<typename T>
class threadsafe_queue
{
private:
    mutable std::mutex mut;
    std::queue<T> data_queue;
    std::condition_variable data_cond;
public:
    threadsafe_queue()
    {}
    threadsafe_queue(threadsafe_queue const& other)
    {
        std::lock_guard<std::mutex> lk(other.mut);
        data_queue=other.data_queue;
    }

    void push(T new_value)
    {
        std::lock_guard<std::mutex> lk(mut);
        data_queue.push(new_value);
        data_cond.notify_one();
    }

    void wait_and_pop(T& value)
    {
        std::unique_lock<std::mutex> lk(mut);
        data_cond.wait(lk,[this]{return !data_queue.empty();});
        value=data_queue.front();
        data_queue.pop();
    }

    std::shared_ptr<T> wait_and_pop()
    {
        std::unique_lock<std::mutex> lk(mut);
        data_cond.wait(lk,[this]{return !data_queue.empty();});
        std::shared_ptr<T> res(std::make_shared<T>(data_queue.front()));
        data_queue.pop();
        return res;
    }

    bool try_pop(T& value)
    {
        std::lock_guard<std::mutex> lk(mut);
        if(data_queue.empty)
            return false;
        value=data_queue.front();
        data_queue.pop();
        return true;
    }

    std::shared_ptr<T> try_pop()
    {
        std::lock_guard<std::mutex> lk(mut);
        if(data_queue.empty())
            return std::shared_ptr<T>();
        std::shared_ptr<T> res(std::make_shared<T>(data_queue.front()));
        data_queue.pop();
        return res;
    }

    bool empty() const
    {
        std::lock_guard<std::mutex> lk(mut);
        return data_queue.empty();
    }
};

template class threadsafe_queue<std::function<void()>>;

Aucun commentaire:

Enregistrer un commentaire