设为首页 加入收藏

TOP

c++多线程与POSIX多线程性能比较
2015-07-20 17:15:21 来源: 作者: 【 】 浏览:2
Tags:线程 POSIX 性能 比较

一 代码结构

二 代码详解

1. test.cpp

?

/*************************************************************************
    > File Name: test.cpp
    > Author: wangzhicheng
    > Mail: 2363702560@qq.com 
    > Created Time: Thu 26 Feb 2015 09:35:49 PM WST
 ************************************************************************/
#include 
  
     
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             using namespace std; void* fun(void *arg) { return NULL; } void g() { } int main() { int i; pid_t pid; pthread_t tid; struct timeva l tv1, tv2; unsigned long long elapse; /* * create processes * */ /* gettimeofday(&tv1, NULL); for(i = 0;i < 1000;i++) { pid = fork(); if(pid < 0) { perror(fork error...! ); exit(EXIT_FAILURE); } else if(!pid) { exit(0); } else { wait(NULL); } } gettimeofday(&tv2, NULL); elapse = (tv2.tv_sec - tv1.tv_sec) * 1e6 +(tv2.tv_usec - tv1.tv_usec); cout << elapse << endl; // 2.7026s */ /* * create POSIX threads */ gettimeofday(&tv1, NULL); for(i = 0;i < 1000;i++) { if(pthread_create(&tid, NULL, fun, NULL)) { perror(threads create error..! ); exit(EXIT_FAILURE); } pthread_join(tid, NULL); } gettimeofday(&tv2, NULL); elapse = (tv2.tv_sec - tv1.tv_sec) * 1e6 +(tv2.tv_usec - tv1.tv_usec); cout << elapse << endl; // 1.591s /*www.2cto.com * create c++11 threads */ /* gettimeofday(&tv1, NULL); for(i = 0;i < 1000;i++) { thread mythread(g); mythread.join(); } gettimeofday(&tv2, NULL); elapse = (tv2.tv_sec - tv1.tv_sec) * 1e6 +(tv2.tv_usec - tv1.tv_usec); cout << elapse << endl; // 1.848s */ return 0; }
           
          
         
        
       
      
     
    
   
  

2. makfile

?

?

CC=g++
all:
	$(CC) -std=c++0x -g -o test test.cpp -pthread -lpthread


?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu4267---A Simple Problem with.. 下一篇hdu 1847 Good Luck in CET-4 Eve..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)