小编Jun*_*aid的帖子

需要关于Linux上C语言中多线程分析的思考

我的应用场景是这样的:我想评估一个可以在四核机器上实现的性能增益,以处理相同数量的数据.我有以下两种配置:

i)1-Process:没有任何线程的程序,处理来自1M .. 1G的数据,而系统假设只运行其4核的单核.

ii)4线程 - 进程:具有4个线程的程序(所有线程执行相同的操作)但处理25%的输入数据.

在我创建4线程的程序中,我使用了pthread的默认选项(即没有任何特定的pthread_attr_t).我认为,与1-Process配置相比,4线程配置的性能提升应该接近400%(或介于350%和400%之间).

我分析了创建线程所花费的时间,如下所示:

timer_start(&threadCreationTimer); 
pthread_create( &thread0, NULL, fun0, NULL );
pthread_create( &thread1, NULL, fun1, NULL );
pthread_create( &thread2, NULL, fun2, NULL );
pthread_create( &thread3, NULL, fun3, NULL );
threadCreationTime = timer_stop(&threadCreationTimer);

pthread_join(&thread0, NULL);
pthread_join(&thread1, NULL);
pthread_join(&thread2, NULL);
pthread_join(&thread3, NULL);    
Run Code Online (Sandbox Code Playgroud)

由于输入数据大小的增加也可能在每个线程的内存需求中增加,因此预先加载所有数据绝对不是一个可行的选择.因此,为了确保不增加每个线程的内存需求,每个线程以小块的形式读取数据,处理它并读取下一个块处理它等等.因此,由线程运行的函数代码的结构如下:

timer_start(&threadTimer[i]);
while(!dataFinished[i])
{
    threadTime[i] += timer_stop(&threadTimer[i]);
    data_source();
    timer_start(&threadTimer[i]);
    process();
}
threadTime[i] += timer_stop(&threadTimer[i]);
Run Code Online (Sandbox Code Playgroud)

变量在收到并处理所有需要的数据时由进程dataFinished[i]标记true.Process()知道什么时候这样做:-)

在main函数中,我正在计算4线程配置所需的时间,如下所示:

execTime4Thread = max(threadTime[0], threadTime[1], threadTime[2], threadTime[3]) + threadCreationTime.

并且简单地计算性能增益

gain = execTime1process …

linux performance multithreading multicore pthreads

7
推荐指数
1
解决办法
568
查看次数

检测处理器在编译时是否具有RDTSCP

一些新的英特尔处理器既有RDTSCRDTSCP指令,而最年长的处理器只有RDTSC指令.

在C/C++中编码时,如何在编译时检测所使用的体系结构是否有RDTSCP指令?

我知道我们可以通过浏览CPU信息(例如,cat/proc/cpuinfo)然后调整我们的代码来手动检查.但是在编译时获取此信息(作为宏或标志值)将真正省略手动检查和编辑代码的需要.

c c++ linux x86 intel

6
推荐指数
1
解决办法
1728
查看次数

标签 统计

linux ×2

c ×1

c++ ×1

intel ×1

multicore ×1

multithreading ×1

performance ×1

pthreads ×1

x86 ×1