【发布时间】:2012-04-25 13:03:32
【问题描述】:
我一直在使用 pthreads,但我意识到如果我使用 1 个线程或者我将任务分成 N 个线程的 1/N,我的代码独立地花费相同的时间。为了举例说明,我将我的代码简化为这个例子:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <boost/progress.hpp>
#define SIZEEXEC 200000000
using namespace boost;
using std::cout;
using std::endl;
typedef struct t_d{
int intArg;
} Thread_data;
void* function(void *threadarg)
{
Thread_data *my_data= (Thread_data *) threadarg;
int size= my_data->intArg;
int i=0;
unsigned rand_state = 0;
for(i=0; i<size; i++) rand_r(&rand_state);
return 0;
}
void withOutThreads(void)
{
Thread_data* t1= new Thread_data();
t1->intArg= SIZEEXEC/3;
function((void *) t1);
Thread_data* t2= new Thread_data();
t2->intArg= SIZEEXEC/3;
function((void *) t2);
Thread_data* t3= new Thread_data();
t3->intArg= SIZEEXEC/3;
function((void *) t3);
}
void withThreads(void)
{
pthread_t* h1 = new pthread_t;
pthread_t* h2 = new pthread_t;
pthread_t* h3 = new pthread_t;
pthread_attr_t* atr = new pthread_attr_t;
pthread_attr_init(atr);
pthread_attr_setscope(atr,PTHREAD_SCOPE_SYSTEM);
Thread_data* t1= new Thread_data();
t1->intArg= SIZEEXEC/3;
pthread_create(h1,atr,function,(void *) t1);
Thread_data* t2= new Thread_data();
t2->intArg= SIZEEXEC/3;
pthread_create(h2,atr,function,(void *) t2);
Thread_data* t3= new Thread_data();
t3->intArg= SIZEEXEC/3;
pthread_create(h3,atr,function,(void *) t3);
pthread_join(*h1,0);
pthread_join(*h2,0);
pthread_join(*h3,0);
pthread_attr_destroy(atr);
delete h1;
delete h2;
delete h3;
delete atr;
}
int main(int argc, char *argv[])
{
bool multThread= bool(atoi(argv[1]));
if(!multThread){
cout << "NO THREADS" << endl;
progress_timer timer;
withOutThreads();
}
else {
cout << "WITH THREADS" << endl;
progress_timer timer;
withThreads();
}
return 0;
}
要么代码错误,要么我的系统上的某些东西不允许并行处理。我在 Ubuntu 11.10 x86_64-linux-gnu、gcc 4.6、Intel® Xeon(R) CPU E5620 @ 2.40GHz × 4 上运行
感谢您的建议!
编辑: 鉴于答案,我已经意识到(1)progress_timer 计时器不允许我测量“实时”时间的差异,以及(2)我在“功能”中给出的任务似乎不足以让我的机器给出不同的有 1 个或 3 个线程的时间(这很奇怪,在这两种情况下我都得到大约 10 秒......)。我试图分配内存并使其更重,是的,我看到了不同。虽然我的其他代码更复杂,但很有可能它仍然运行 +- 同时使用 1 或 3 个线程。谢谢!
【问题讨论】:
-
function真的做什么吗?编译器是否有可能优化所有内容? -
另外,请注意您没有使用一致的
intArg值。 -
你是在构建优化还是不优化?
-
抱歉,intArg 值有错,现已更正
-
我正在运行您的代码 - 我得到 1.953 秒的非线程和 0.735 的线程,计时 ala "time ./split 0" 因为我无法指定提升路径。
标签: c++ pthreads parallel-processing