nth4
来源:互联网 发布:java源码书籍 编辑:程序博客网 时间:2024/05/18 01:55
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
int thread_num = 1;
long vector_length = 100000;
int total = 0;
int *a = NULL;
void *vel_mul(void *arg);
int main(int argc, char *argv[]){
int res;
void *thread_result;
//scan income
if(argc != 3){
printf("You have not enter enough parameters. So using default parameter\n");
}
else{
thread_num = atoi(argv[1]);
vector_length = atol(argv[2]);
}
printf("Thread number is %d. Vector length is %ld\n",thread_num,vector_length);
//scan finished
//start create threads
a = (int *)calloc(vector_length,sizeof(int));
for(int i = 0; i < vector_length; i++){
switch((i+1)%3){
case 0 :
a[i]=1;
break;
case 1 :
a[i]=-1;
break;
case 2 :
a[i]=0;
break;
}
}
pthread_t *a_thread;
a_thread = (pthread_t *)calloc(thread_num,sizeof(pthread_t));
for(int i = 0; i < thread_num; i++){
res = pthread_create(&a_thread[i],NULL,vel_mul,(void *)&i);
if(res != 0)perror("thread creation failed");
}
printf("Waiting ...\n");
for(int i = thread_num-1; i >=0; i--){
res = pthread_join(a_thread[i],&thread_result);
}
printf("finish \n");
printf("total is %d\n",total);
#include <stdio.h>
#include <pthread.h>
int thread_num = 1;
long vector_length = 100000;
int total = 0;
int *a = NULL;
void *vel_mul(void *arg);
int main(int argc, char *argv[]){
int res;
void *thread_result;
//scan income
if(argc != 3){
printf("You have not enter enough parameters. So using default parameter\n");
}
else{
thread_num = atoi(argv[1]);
vector_length = atol(argv[2]);
}
printf("Thread number is %d. Vector length is %ld\n",thread_num,vector_length);
//scan finished
//start create threads
a = (int *)calloc(vector_length,sizeof(int));
for(int i = 0; i < vector_length; i++){
switch((i+1)%3){
case 0 :
a[i]=1;
break;
case 1 :
a[i]=-1;
break;
case 2 :
a[i]=0;
break;
}
}
pthread_t *a_thread;
a_thread = (pthread_t *)calloc(thread_num,sizeof(pthread_t));
for(int i = 0; i < thread_num; i++){
res = pthread_create(&a_thread[i],NULL,vel_mul,(void *)&i);
if(res != 0)perror("thread creation failed");
}
printf("Waiting ...\n");
for(int i = thread_num-1; i >=0; i--){
res = pthread_join(a_thread[i],&thread_result);
}
printf("finish \n");
printf("total is %d\n",total);
- nth4
- 对大小端的一点理解
- ubuntu/centos环境下玩android系统(一): 从编译android开始!
- 用yum查询想安装的软件
- 从文件中读取数组到内存
- 【转载】Ubuntu下chrome浏览器字体设置
- nth4
- 【2012求职经历】应届生求职经历
- hdu1028
- 使用ADO.NET 建立适应多种数据库的数据访问层接口
- nth5
- Unlocking Temporary Table Instances from Deleted Process Requests
- 《精通Hibernate》学习(4)——映射对象标识符
- android----SQLite数据库
- linux设置特殊权限位