A code structure
Two code explanation
1. Test.cpp
/************************************************************************* > File Name:test.cpp > Author:wan Gzhicheng > Mail: [email protected] > Created time:thu 09:35:49 PM WST ******************** /#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #include <sys/time.h> #include <sys/types.h> #include <sys/ wait.h> #include <pthread.h> #include <iostream> #include <thread>using namespace std;void* fun ( void *arg) {return NULL;} void G () {}int main () {int i;pid_t pid;pthread_t tid;struct timeval TV1, tv2;unsigned long Long elapse;/* * Create process ES * *//*gettimeofday (&TV1, NULL); for (i = 0;i < 1000;i++) {pid = fork (), if (PID < 0) {perror ("fork error...! \ n "); 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 (&ti D, NULL, fun, NULL) {perror ("Threads Create error..! \ n "); 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 &L t;< Endl; 1.591s/* * Create c++11 threads *//*gettimeofday (&TV1, NULL); for (i = 0;i < 1000;i++) {thread mythread (g); Mythrea D.join ();} Gettimeofday (&TV2, NULL); elapse = (tv2.tv_sec-tv1.tv_sec) * 1e6 + (tv2.tv_usec-tv1.tv_usec); cout << elapse &L t;< Endl; 1.848s*/return 0;}
2. Makfile
cc=g++all:$ (CC)-std=c++0x-g-O test test.cpp-pthread-lpthread
C++11 multi-Threading and POSIX multithreading performance comparison