Alexander
Alexander

Reputation: 981

PPL. How do i schedule several tasks on same worker thread?

I need to know how i can control on which worker thread Concurrency::task will run.

Imagine if i have following code:

#include <ppltasks.h>
#include <iostream>
#include <mutex>

int main()
{
    std::mutex mtx;

    Concurrency::create_task([&mtx]
    {
        mtx.lock();
        std::cout << "Task 1. Thread id: " << std::this_thread::get_id() << std::endl;
        mtx.unlock();
    });

    Concurrency::create_task([&mtx]
    {
        mtx.lock();
        std::cout << "Task 2. Thread id: " << std::this_thread::get_id() << std::endl;
        mtx.unlock();
    });

    std::getchar();

    return 0;
}

How do i make sure that both tasks will run on same worker thread?

Upvotes: 0

Views: 187

Answers (0)

Related Questions