编写Pthreads程序实现直方图统计

编写一个Pthreads程序实现直方图统计,选择使用忙等待、互斥量和信号量来保证临界区的互斥,并分析方法的优点和缺点。

互斥量

#include
#include
#include
#include "time.h"
#define fer(i,a,b) for(int i=a;i<b;i++)
//#define int long long
using namespace std;

int thread_count;
pthread_mutex_t mutex[100];
const int N=1e6;
//生成数据,生成1e6个数据,分布在0~100中
void generate(int n){
    srand(0);
    ofstream outfile("data.xls");
    int x;
    fer(i,0,n){
        x=rand()%100;
        outfile<<x<<"\n";
    }
    outfile.close();
}
int res[100],Data[N],flag=0;
void* Thread_mutex (void* rank) {
	long my_rank = (long long) rank;
	int my_n = N / thread_count;//每个线程处理N/thread_count个数据
	int my_first_i = my_n * my_rank;
	int my_last_i = my_first_i + my_n;
    int i;
	for (i = my_first_i; i < my_last_i; i++) {
		pthread_mutex_lock (&mutex[Data[i]]);
		res[Data[i]]++;
		pthread_mutex_unlock (&mutex[Data[i]]);
	}
    cout<<"Thread"<<my_rank<<" has computed:"<<my_first_i<<"~"<<my_last_i<<endl;

	return NULL;
}

int main(int argc, char* argv[]){
    //2020030186
    //generate(N);
    clock_t start,end;

    ifstream infile("data.xls");
    fer(i,0,N){
        infile>>Data[i];
    }
    infile.close();
    int thread;
    pthread_t *thread_handles;
    thread_count = strtol(argv[1], NULL, 10);//从命令行读取线程数
    thread_handles = new pthread_t[thread_count];//分配长度
    
    fer(i,0,100){
        pthread_mutex_init(&mutex[i],NULL);
    }
    //创建进程
    start=clock();
    for(thread = 0; thread < thread_count; thread++){
		pthread_create(&thread_handles[thread], NULL, Thread_mutex, (void*)thread);
	}
    printf("Success\n");
    //合并线程
    for(thread = 0; thread < thread_count; thread++){
		pthread_join(thread_handles[thread], NULL);//合并线程
    }
    end=clock();
   	cout<<"\nruntime:"<< end-start;
    fer(i,0,100){
        pthread_mutex_destroy(&mutex[i]);
    }
    
    free(thread_handles);
    return 0;
}

忙等待

#include
#include
#include
#include "time.h"
#define fer(i,a,b) for(int i=a;i<b;i++)
//#define int long long
using namespace std;

int thread_count;
//生成数据,生成1e6个数据,分布在0~100中
const int N=1e6;
void generate(int n){
    srand(0);
    ofstream outfile("data.xls");
    int x;
    fer(i,0,n){
        x=rand()%100;
        outfile<<x<<"\n";
    }
    outfile.close();
}
int res[100],Data[N],flag=0;
void* Thread_busy_waiting (void* rank) {
	long my_rank = (long long) rank;
	int my_n = N / thread_count;//每个线程处理N/thread_count个数据
	int my_first_i = my_n * my_rank;
	int my_last_i = my_first_i + my_n;
    int i;
	for (i = my_first_i; i < my_last_i; i++) {
		while (flag != my_rank );
		res[Data[i]]++;
		flag = (flag+1) % thread_count;
	}
    cout<<"Thread"<<my_rank<<" has computed:"<<my_first_i<<"~"<<my_last_i<<endl;

	return NULL;
}

int main(int argc, char* argv[]){
    //2020030186 zyq
    //generate(N);
    clock_t start,end;
    ifstream infile("data.xls");
    fer(i,0,N){
        infile>>Data[i];
    }
    infile.close();
    int thread;
    pthread_t *thread_handles;
    thread_count = strtol(argv[1], NULL, 10);//从命令行读取线程数
    thread_handles = new pthread_t[thread_count];//分配长度
    start=clock();
    //创建进程
    for(thread = 0; thread < thread_count; thread++){
		pthread_create(&thread_handles[thread], NULL, Thread_busy_waiting, (void*)thread);
	}
    printf("Success\n");
    //合并线程
    for(thread = 0; thread < thread_count; thread++){
		pthread_join(thread_handles[thread], NULL);//合并线程
    }
    end=clock();
   	cout<<"\nruntime:"<< end-start;
    free(thread_handles);
    return 0;
}

二元信号量

#include
#include
#include
#include "time.h"
#include 
#define fer(i,a,b) for(int i=a;i<b;i++)
//#define int long long
using namespace std;

int thread_count;
sem_t sem[100];
const int N=1e6;
//生成数据,生成1e6个数据,分布在0~100中
void generate(int n){
    srand(0);
    ofstream outfile("data.xls");
    int x;
    fer(i,0,n){
        x=rand()%100;
        outfile<<x<<"\n";
    }
    outfile.close();
}
int res[100],Data[N];
void* Thread_sem (void* rank) {
	long my_rank = (long long) rank;
	int my_n = N / thread_count;//每个线程处理N/thread_count个数据
	int my_first_i = my_n * my_rank;
	int my_last_i = my_first_i + my_n;
    int i;
	for (i = my_first_i; i < my_last_i; i++) {
		sem_wait(&sem[Data[i]]);
		res[Data[i]]++;
		sem_post(&sem[Data[i]]);
	}
    
    cout<<"Thread"<<my_rank<<" has computed:"<<my_first_i<<"~"<<my_last_i<<endl;

	return NULL;
}

int main(int argc, char* argv[]){
    //2020030186 zyq
    //generate(N);
    clock_t start,end;

    ifstream infile("data.xls");
    fer(i,0,N){
        infile>>Data[i];
    }
    infile.close();
    int thread;
    pthread_t *thread_handles;
    thread_count = strtol(argv[1], NULL, 10);//从命令行读取线程数
    thread_handles = new pthread_t[thread_count];//分配长度
    
    fer(i,0,100){
        sem_init(&sem[i], 0, 1); 
    }
    //创建进程
    start=clock();
    for(thread = 0; thread < thread_count; thread++){
		pthread_create(&thread_handles[thread], NULL, Thread_sem, (void*)thread);
	}
    printf("Success\n");
    //合并线程
    for(thread = 0; thread < thread_count; thread++){
		pthread_join(thread_handles[thread], NULL);//合并线程
    }
    end=clock();
   	cout<<"\nruntime:"<< end-start;
    fer(i,0,100){
        sem_destroy(&sem[i]);
    }
    free(thread_handles);
    return 0;
}

你可能感兴趣的:(并行程序设计,pthreads,并行程序设计)