Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在多个线程上缩放当前代码_C++_Multithreading - Fatal编程技术网

C++ 如何在多个线程上缩放当前代码

C++ 如何在多个线程上缩放当前代码,c++,multithreading,C++,Multithreading,我有两个线程的有效代码。 代码的主要思想是:在第一个线程中打印每个偶数,在第二个线程中打印每个奇数 我必须在10个线程上扩展当前代码。例如 第一个线程打印0,10,20 第二条线打印1,11,21 第10个线程打印9、19、29、 我计划使用vector,但我不想写10个函数。 如何解决十个线程的任务? #include <iostream> #include <string> #include <cstdlib> // std::atoi #include

我有两个线程的有效代码。 代码的主要思想是:在第一个线程中打印每个偶数,在第二个线程中打印每个奇数

我必须在10个线程上扩展当前代码。例如

第一个线程打印0,10,20

第二条线打印1,11,21

第10个线程打印9、19、29、

我计划使用
vector
,但我不想写10个函数。 如何解决十个线程的任务?

#include <iostream>
#include <string>
#include <cstdlib> // std::atoi
#include <thread> // std::thread
#include <mutex> // std::mutex
#include <condition_variable>


using namespace std;

class PrintOrder final
{
public:
    PrintOrder(int n) 
        : maxNum_(n)
        , curNum_(0)
    {
        startTime_ = chrono::steady_clock::now();
        threadEven_ = thread(&PrintOrder::even, this);
        threadOdd_ = thread(&PrintOrder::odd, this);
    }

    ~PrintOrder()
    {
        threadEven_.join();
        threadOdd_.join();

        auto endTime = chrono::steady_clock::now();
        auto diff = endTime - startTime_;
        cout << chrono::duration <double, milli> (diff).count() << " ms" << endl;
    }

    void even()
    {
        unique_lock<mutex> lk(m_);
        while (curNum_ <= maxNum_)
        {
            cv_.wait(lk, [this]{ return curNum_ % 2 == 0; });
            if (curNum_ <= maxNum_)
            {
                cout << curNum_ << endl;
                ++curNum_;
            }
            cv_.notify_all();
        }
    }

    void odd()
    {
        unique_lock<mutex> lk(m_);
        while (curNum_ <= maxNum_)
        {
            cv_.wait(lk, [this]{ return curNum_ % 2 != 0; });
            if (curNum_ <= maxNum_)
            {
                cout << curNum_ << endl;
                ++curNum_;
            }
            cv_.notify_all();
        }
    }
private:
    int maxNum_;
    int curNum_;
    mutex m_;
    condition_variable cv_;
    thread threadEven_, threadOdd_;

    std::chrono::time_point<std::chrono::steady_clock> startTime_;
};

int main(int argc, char *argv[])
{
    if (argc == 2)
    {
        int num = stoi(argv[1]);

        PrintOrder printOrder(num);
    } else {
        cout << "ERROR: expected console input" << endl;
    }
    return 0;   
}
#包括
#包括
#include//std::atoi
#include//std::thread
#include//std::mutex
#包括
使用名称空间std;
类打印订单最终版
{
公众:
打印顺序(int n)
:maxNum_un
,curNum_(0)
{
开始时间=计时::稳定时间::现在();
线程偶数=线程(&PrintOrder::偶数,this);
threadOdd=线程(&PrintOrder::odd,this);
}
~PrintOrder()
{
thread偶数连接();
threadOdd_uz.join();
自动结束时间=时钟::稳定时钟::现在();
自动差异=结束时间-开始时间;

cout您可以保留一个
std::vector
std::thread
,而不是两个硬编码的

例如:

#include <condition_variable>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <vector>

class PrintOrder final {
public:
    PrintOrder(unsigned n, unsigned threadcount) : maxNum_(n) {
        threads.reserve(threadcount);
        // create all threads giving each one a different number, x
        std::unique_lock<std::mutex> lk(m_);
        for(unsigned x = 0; x < threadcount; ++x) {
            threads.emplace_back(&PrintOrder::background, this, x);
        }
    }

    ~PrintOrder() {
        // join all
        for(auto&& th : threads) th.join();
    }

    // the thread function, taking x
    void background(unsigned x) {
        while(true) {
            std::unique_lock<std::mutex> lk(m_);
            // wait until it's this thread's turn or curNum_ > maxNum_
            while((curNum_ % threads.size()) != x && curNum_ <= maxNum_)
                cv_.wait(lk);

            if(curNum_ > maxNum_) break; // time to quit

            std::cout << curNum_ << std::endl;
            ++curNum_;
            cv_.notify_all();
        }
    }

private:
    std::mutex m_{};
    std::condition_variable cv_{};
    std::vector<std::thread> threads{};
    unsigned curNum_ = 0;
    unsigned maxNum_;
};

int main() {
    PrintOrder printOrder(100, 10); // count to 100, using 10 threads
}
#包括
#包括
#包括
#包括
#包括
#包括
类打印订单最终版{
公众:
PrintOrder(无符号n,无符号threadcount):maxNum(n){
线程。保留(线程计数);
//创建所有线程,为每个线程指定一个不同的数字x
std::唯一锁lk(m);
用于(无符号x=0;xmaxNum_
while((curNum\u%threads.size())!=x&&curNum\umaxnum\ux)break;//该退出了
标准::cout