这个线程池/多核模拟有什么问题
What's wrong with this threads pool / multi-core simulation
大家好。我一直在尝试制作一种线程池,用来模拟多核处理器,在那里我有很多线程一直在运行(核心),我稍后会将其调度来处理一个(目前已修复)函数。让线程一直运行的想法是,我没有线程创建/销毁开销。我现在做的事情有三个问题。首先,结果都是错误的。其次,测量时间的函数报告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";
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 如何使用Google Mock来模拟gettimeofday()
- 努力将整数转换为链表。不知道我在这里做错了什么
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 拥有来自 C# C++模拟 ContainsKey (TKey) - 什么是最佳实践?
- 模拟概念和约束的推荐方法是什么
- 什么是 C# AutoResetEvent/ManualResetEvent 的 C++ 可移植模拟
- 使用多线程来模拟这种情况的好方法是什么
- 什么模拟框架与CPPUNIT一起使用
- 这个线程池/多核模拟有什么问题
- 我可以用什么UI库或其他东西来模拟用C++移动东西的交通状况
- 有什么方法可以用MSVS2015模拟编译时的双常数吗
- 什么是CTime(有任何)32位模拟
- 对传递给模拟函数的参数调用成员函数的最简单方法是什么?
- PHP 'preg_match_all '函数的Boost::regexp模拟是什么?
- 单元测试-我试图用googlemock模拟一个简单的c++方法有什么问题?
- 什么是WSAAsyncSelect()函数在Apple GCC中的模拟