Linux 在多线程程序上解释时间命令输出

Linux 在多线程程序上解释时间命令输出,linux,multithreading,time,pthreads,Linux,Multithreading,Time,Pthreads,我有一个多线程程序,在所有pthread_创建之前和所有pthread_加入之后,我都在分析所花的时间 现在我发现这一次,我们称之为X,它在下面的“在xms中完成”中显示,实际上是时间的user+sys时间输出。在我的应用程序中,a.out的number参数控制要生成多少线程/a.out 1生成1个pthread和/a.out 2生成2个线程,其中每个线程执行相同的工作量 我希望X是实时的,而不是用户+系统时间。有人能告诉我为什么不是这样吗?这就意味着我的应用程序确实是并行运行的,线程之间没有任

我有一个多线程程序,在所有pthread_创建之前和所有pthread_加入之后,我都在分析所花的时间

现在我发现这一次,我们称之为X,它在下面的“在xms中完成”中显示,实际上是时间的user+sys时间输出。在我的应用程序中,a.out的number参数控制要生成多少线程/a.out 1生成1个pthread和/a.out 2生成2个线程,其中每个线程执行相同的工作量

我希望X是实时的,而不是用户+系统时间。有人能告诉我为什么不是这样吗?这就意味着我的应用程序确实是并行运行的,线程之间没有任何锁定

[jithin@whatsoeverclever tests]$ time ./a.out 1
Done in 320ms

real    0m0.347s
user    0m0.300s
sys     0m0.046s
[jithin@whatsoeverclever tests]$ time ./a.out 2
Done in 450ms

real    0m0.266s
user    0m0.383s
sys     0m0.087s
[jithin@whatsoeverclever tests]$ time ./a.out 3
Done in 630ms

real    0m0.310s
user    0m0.532s
sys     0m0.105s
代码

int main(int argc, char **argv) {

  //Read the words
  getWords();

  //Set number of words to use
  int maxWords = words.size();
  if(argc > 1) {
     int numWords = atoi(argv[1]);
     if(numWords > 0 && numWords < maxWords) maxWords = numWords;
  }

  //Init model
  model = new Model(MODEL_PATH);
  pthread_t *threads = new pthread_t[maxWords];
  pthread_attr_t attr;
  void *status;

  // Initialize and set thread joinable
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

  int rc;
clock_t startTime = clock();

  for(unsigned i=0; i<maxWords; i++) {
     //create thread
     rc = pthread_create(&threads[i], NULL, processWord, (void *)&words[i] );
     if (rc){
         cout << "Error:unable to create thread: " << i << "," << rc << endl;
         exit(-1);
      }
  }

  // free attribute and wait for the other threads
  pthread_attr_destroy(&attr);
  for(unsigned i=0; i<maxWords; i++) {
     rc = pthread_join(threads[i], &status);
     if (rc){
         cout << "Error:unable to join thread: " << i << "," << rc << endl;
         exit(-1);
      }
  }

  clock_t endTime = clock();

  float diff = (((float)endTime - (float)startTime) / 1000000.0F ) * 1000;
  cout<<"Done in "<< diff << "ms\n";
  delete[] threads;
  delete model;
}
int main(int argc,char**argv){
//读单词
getWords();
//设置要使用的字数
int maxWords=words.size();
如果(argc>1){
int numWords=atoi(argv[1]);
如果(numWords>0&&numWords对于(无符号i=0;i专门记录了
clock
函数以返回进程使用的处理器时间。如果您想测量所用的时间,它不是正确的函数。

向我们展示您的代码,以便我们可以看到它是如何获得这些数字的。也许您正在调用
clock
,它专门用于返回进程essor进程使用的时间。@DavidSchwartz我已经添加了我的主程序。是的,我正在调用clock()。我的错误。@DavidSchwartz如果你可以添加它作为答案,我可以接受它。