这个线程池/多核模拟有什么问题

What's wrong with this threads pool / multi-core simulation

本文关键字:模拟 什么 问题 多核 线程      更新时间:2023-10-16

大家好。我一直在尝试制作一种线程池,用来模拟多核处理器,在那里我有很多线程一直在运行(核心),我稍后会将其调度来处理一个(目前已修复)函数。让线程一直运行的想法是,我没有线程创建/销毁开销。我现在做的事情有三个问题。首先,结果都是错误的。其次,测量时间的函数报告0毫秒第三,程序在退出时调用中止。这是我正在使用的代码:

    auto fakeExpensiveOperation = [](int i) -> float
    {
        Sleep(10);
        return sqrt(i);
    };
    // Performance test
    int size = 4000;
    float* out = new float[size];
#define THREAD_RUNNING 0
#define THREAD_FINISHED (1 << 0)
#define THREAD_EXIT (1 << 1)
#define THREAD_STALL (1 << 2)
    const int coreCount = 8;
    thread cores[coreCount];
    atomic<unsigned int> msgArray[coreCount]; for (auto& msg : msgArray) msg.store(THREAD_STALL);
    auto kernel = [out, &fakeExpensiveOperation](int idx){ out[idx] = fakeExpensiveOperation(idx); };
    for (int i = 0; i < coreCount; i++)
    {
        cores[i] = thread([&msgArray, i, kernel]()
        {
            while (true)
            {
                unsigned int msg = msgArray[i].load();
                if((msg & THREAD_STALL) == THREAD_STALL)
                    continue;
                if ((msg & THREAD_EXIT) == THREAD_EXIT)
                    break;
                if ((msg & THREAD_RUNNING) == THREAD_RUNNING)
                {
                    int idx = (msg >> 3) + i;
                    // Do the function
                    kernel(idx);
                    msgArray[i].store(THREAD_FINISHED);
                }
            }
        });
    }
    auto t2 = time_call([&]()
    {
        for (int i = 0; i < size; i += coreCount)
        {
            for (int n = 0; n < coreCount; n++)
            {
                if((msgArray[n].load() & THREAD_RUNNING) == THREAD_RUNNING) continue; // The core is still working
                unsigned int msg = THREAD_RUNNING;
                msg |= (i << 3);
                msgArray[n].store(msg);
            }
        }
    });
    for (int n = 0; n < coreCount; n++) msgArray[n].store(THREAD_EXIT);
    cout << "sqrt 0 : " << out[0] << endl;
    cout << "sqrt 1 : " << out[1] << endl;
    cout << "sqrt 2 : " << out[2] << endl;
    cout << "sqrt 4 : " << out[4] << endl;
    cout << "sqrt 16 : " << out[16] << endl;
    cout << "Parallel : " << t2 << endl;
    system("pause");
    delete[] out;
    return 0;

我真的没主意了。有人能指出这里出了什么问题吗?

编辑:我做了我提到的更改,但仍然得到了错误的值。我更改了标志的值,并在创建它们之后分离线程。

我可能错了(我对C++11线程的东西不是很熟悉),但看起来你运行了一个线程cores[i] = thread([&msgArray, i, kernel](),然后等待该线程完成。然后创建下一个线程。本质上使其成为单线程。

我也喜欢用这个来计时C++11

std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
// Do Stuff
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds_d1 = end-start;
std::time_t end_time_d1 = std::chrono::system_clock::to_time_t(end);
std::cout << "elapsed time: " << elapsed_seconds_d1.count() << "sn";