【发布时间】:2020-07-05 22:13:03
【问题描述】:
我有一个示例代码要优化
#include <iostream>
#include <vector>
#include <memory>
#include <fstream>
#include <assert.h>
#include <pthread.h>
#include <unistd.h>
template<typename NumType=float>
void RenderSphereOnScreen(const NumType* const _coords)
{
std::cout<<_coords[0]<<" "<<_coords[1]<<" "<<_coords[2]<<std::endl; //assume
}
template<typename NumType>
class SphereRenderTask
{
NumType _coords[3];
public:
SphereRenderTask(const NumType& x, const NumType& y, const NumType& z)
{
if(std::is_same<NumType, int>::value || std::is_same<NumType, float>::value || std::is_same<NumType, double>::value)
{
_coords[0] = x;
_coords[1] = y;
_coords[2] = z;
}
else
{
std::cout << "Error.unknown class type!!\n";
assert(0);
}
}
void operator()() const
{
RenderSphereOnScreen(_coords);
}
};
std::vector<SphereRenderTask<double>*> taskList;
const int THREADS = 3;
void *renderThread(void *index)
{
int tid = *((int *)index);
int max = -1;
int i = tid*(taskList.size()/THREADS);
max = (tid+1)*(taskList.size()/THREADS);
for( std::vector<SphereRenderTask<double>*>::iterator iter=taskList.begin() + i; i<max;)
{
(**iter)();
i++;
iter++;
}
}
void StartRendering(const char* const inputFilePath)
{
void sequential_render();
void multithreaded_render();
taskList.clear(); //init
std::ifstream inputfile(inputFilePath);
double x,y,z;
while (inputfile >> x >> y>> z)
{
taskList.push_back(new SphereRenderTask<double>(x,y,z));
}
int y1 = taskList.size();
inputfile.close();
if(!THREADS)
sequential_render();
else
multithreaded_render();
}
void sequential_render()
{
for(std::vector<SphereRenderTask<double>*>::iterator iter=taskList.begin(); iter!=taskList.end(); ++iter)
{
(**iter)();
}
}
void multithreaded_render()
{
pthread_t tid[THREADS];
for(int i=0;i<THREADS;i++)
{
int tmp = i;
pthread_create(&tid[i], NULL, renderThread, &tmp);
pthread_join(tid[i], NULL);
}
}
int main(int argc, const char* argv[])
{
StartRendering("sphere.txt");
return 0;
}
这是做什么的:
-
读取包含 100k 行 (x,y,z) 的文件名“sphere.txt”,可以是浮点数
例子:
136 562 293 629 399 497 682 642 995 739 351 869 607 968 7 262 788 863 ....最多 100k 行
保存在数据结构中,taskList指针向量
- 然后使用sequential_render 或multithreaded_render 在屏幕上显示
对此我有两个问题:
【问题讨论】:
-
你的“多线程”版本启动一个线程并等待它完成,然后它启动下一个线程并等待它完成,然后它启动下一个线程...
-
您主要测量在控制台中打印内容所需的时间,而 Windows 的 cmd 速度非常慢。
-
我怀疑从多线程打印到
std::cout会有用。不同线程的输出很可能会混淆。除非你同步它们,否则任何时候只有一个线程可以工作,所以你实际上回到了单个线程。 -
当您实际同时运行多个线程时会遇到的一个问题是您将
&tmp传递给线程,而tmp的生命周期可能在线程启动时已经结束。 -
在 Windows 中,一些终端进程非常慢。我记得一个 Meteor 项目。在 Virtual Box 中启动 Linux 并在 Virtual Box 中构建比在 Windows 中构建而不在后台运行 Virtual Box 更快。现在我了解到我可能也可以使用 Git Bash Shell 而不是 PowerShell 或 Windows Cmd
标签: c++ c multithreading algorithm operating-system