#include<iostream>
#include<sys/time.h>
#include<pthread.h>
using namespace std;
template<class T>
void quick_sort(T a[], int low, int high)
{
int first = low;
int last = high;
int key = a[first];
if(low >= high)
return;
while(first < last)
{
while(first < last && a[last] >= key)last--;
a[first] = a[last];
while(first < last && a[first] <= key)first++;
a[last] = a[first];
}
a[first] = key;
quick_sort(a, low, first - 1);
quick_sort(a, first + 1, high);
}
#define CONST_ARRAY_SIZE (100000)
template<class T>
void bubble_sort(T a[], int n)
{
for(int j = 0; j < n - 1; j++)
for(int i = 0; i < n - 1 - j; i++)
if(a[i] > a[i + 1])
swap(a[i], a[i + 1]);
}
void* call_bubble_sort_process(void* args)
{
struct timeval start, end;
int *pIntArray = (int*)malloc(CONST_ARRAY_SIZE*sizeof(int));
for(int i = 0; i < CONST_ARRAY_SIZE; i++)
pIntArray[i] = CONST_ARRAY_SIZE - i;
gettimeofday(&start, NULL);
//quick_sort(pIntArray, 0, CONST_ARRAY_SIZE - 1);
bubble_sort(pIntArray, CONST_ARRAY_SIZE);
gettimeofday(&end, NULL);
for(int j = 0; j < 10; j++)
cout<<pIntArray[j]<<" ";
cout<<endl;
cout<<"waste time = "<<(end.tv_sec - start.tv_sec)<<"s"<<(end.tv_usec - start.tv_usec)<<"us"<<endl;
free(pIntArray);
return NULL;
}
#define PROCESS_NUM (8)
void start_thread()
{
pthread_t th[PROCESS_NUM];
for(int i = 0; i < PROCESS_NUM; i++)
pthread_create(&th[i], NULL, call_bubble_sort_process, NULL);
for(int i = 0; i < PROCESS_NUM; i++)
pthread_join(th[i], NULL);
}
int main()
{
start_thread();
return 0;
}
多线程暖手宝
最新推荐文章于 2017-07-18 16:05:56 发布