Как никто не заблокирован присоединиться к потоку STD - PullRequest
0 голосов
/ 01 января 2019

Я хочу сохранить свой код в чистоте и делать все правильно, к любому std::thread Мне нужно сделать присоединение или отсоединение, но как я могу ждать (в основном потоке) другого потока, не блокируя выполнение основногонить?

void do_computation()
{
    //  Calculate 1000 digits of Pi.
}


int main()
{
    std::thread td1(&do_computation);

    while (running)
    {
        //  Check if thread td1 finish and if yes print a message

        //  Here are some stuff of the main to do...
        //  Print to UI, update timer etc..

    }

    //  If the thread has not finished yet here, just kill it.
}

Ответы [ 2 ]

0 голосов
/ 01 января 2019

Вы можете использовать std :: обещание и std :: future .Подробнее здесь и здесь .

#include <vector>
#include <thread>
#include <future>
#include <numeric>
#include <iostream>
#include <chrono>

void accumulate(std::vector<int>::iterator first,
                std::vector<int>::iterator last,
                std::promise<int> accumulate_promise)
{
    int sum = std::accumulate(first, last, 0);
    accumulate_promise.set_value(sum);  // Notify future
}

void do_work(std::promise<void> barrier)
{
    std::this_thread::sleep_for(std::chrono::seconds(1));
    barrier.set_value();
}

int main()
{
    // Demonstrate using promise<int> to transmit a result between threads.
    std::vector<int> numbers = { 1, 2, 3, 4, 5, 6 };
    std::promise<int> accumulate_promise;
    std::future<int> accumulate_future = accumulate_promise.get_future();
    std::thread work_thread(accumulate, numbers.begin(), numbers.end(),
                            std::move(accumulate_promise));
    accumulate_future.wait();  // wait for result
    std::cout << "result=" << accumulate_future.get() << '\n';
    work_thread.join();  // wait for thread completion

    // Demonstrate using promise<void> to signal state between threads.
    std::promise<void> barrier;
    std::future<void> barrier_future = barrier.get_future();
    std::thread new_work_thread(do_work, std::move(barrier));
    barrier_future.wait();
    new_work_thread.join();
}
0 голосов
/ 01 января 2019

Ответ семафор .Вы можете использовать двоичный семафор для синхронизации ваших потоков.

Вы можете использовать System V семафоры или pthread мьютексы, но они как-то унаследованы в C ++.Используя ответ Tsuneo Yoshioka , мы могли бы реализовать способ семафора на C ++.

#include <mutex>
#include <condition_variable>

class Semaphore {
public:
    Semaphore (int count_ = 0)
        : count(count_) {}

    inline void notify()
    {
        std::unique_lock<std::mutex> lock(mtx);
        count++;
        cv.notify_one();
    }

    inline void wait()
    {
        std::unique_lock<std::mutex> lock(mtx);

        while(count == 0){
            cv.wait(lock);
        }
        count--;
    }

private:
    std::mutex mtx;
    std::condition_variable cv;
    int count;
};

Ваша реализация может использовать класс Semaphore, например, так.

void do_computation()
{
    //calculate 1000 digits of Pi.

    semaphore.notify();
}


int main()
{
    Semaphore semaphore(0);
    std::thread td1(&do_computation);

    semaphore.wait();
}
...