多线程计算----pthread
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h> #define NUM_THREADS 10
#define buffer_size 6000000 void *thread_function(void *agr);
int buffer[buffer_size];
int result[NUM_THREADS];
int result1[NUM_THREADS]; int main() {
int res;
pthread_t a_thread[NUM_THREADS];
void *thread_result;
int lots_of_threads;
//static int buffer[60];
int i, m;
int tmp1;
int flag = 1;
struct timeval tv_start, tv_end;
//static int result[10]; for(i = 0; i < buffer_size; i++) {
buffer[i] = (int)rand() % 10000;
} gettimeofday(&tv_start, NULL);
for(i = 0; i < NUM_THREADS; i++) {
tmp1 = 0;
for(m = 0; m < buffer_size / NUM_THREADS; m++){
if(tmp1 < buffer[i * 10 + m])
tmp1 = buffer[i * 10 + m];
}
result1[i] = tmp1;
}
gettimeofday(&tv_end, NULL);
printf("Cost Time: %0.10fms\n", (float)((1000000 * (tv_end.tv_sec - tv_start.tv_sec) + tv_end.tv_usec - tv_start.tv_usec)/1000)); for(lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads++) {
res = pthread_create(&(a_thread[lots_of_threads]), NULL, thread_function, (void*)lots_of_threads);
if(res != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
//sleep(1);
}
printf("Waiting for threads to finish.....\n");
for(lots_of_threads = NUM_THREADS - 1; lots_of_threads >= 0; lots_of_threads--) {
res = pthread_join(a_thread[lots_of_threads], &thread_result);
if(res == 0) {
printf("Picked up a thread\n");
} else {
perror("Pthread_join failed");
}
} for(m = 0; m < NUM_THREADS; m++) {
if(result[m] != result1[m])
flag = 0;
} if(!flag)
printf("Compute wrong~~~\n");
else
printf("Successful~~~\n"); printf("All done\n");
} void *thread_function(void *arg) {
//int my_number = *(int*) arg;
int my_number = (int)arg;
int rand_num;
int k;
int tmp = 0;
struct timeval tv_start, tv_end; gettimeofday(&tv_start, NULL);
for(k = 0; k < buffer_size / NUM_THREADS; k++) {
if(tmp < buffer[my_number * (buffer_size / NUM_THREADS) + k])
tmp = buffer[my_number * (buffer_size / NUM_THREADS) + k];
} result[my_number] = tmp; gettimeofday(&tv_end, NULL);
printf("Thread %d Cost Time: %0.10fms\n", my_number, (float)((1000000 * (tv_end.tv_sec - tv_start.tv_sec) + tv_end.tv_usec - tv_start.tv_usec)/1000)); printf("thread_function is running. Argument was %d\n", my_number);
//rand_num = 1 + (int)(9.0 * rand() / (RAND_MAX + 1.0));
//sleep(rand_num);
//printf("Rand number %d\n", rand_num);
//printf("Byte from %d\n", my_number);
pthread_exit(NULL);
}
多线程计算----pthread的更多相关文章
- 转载~kxcfzyk:Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解
Linux C语言多线程库Pthread中条件变量的的正确用法逐步详解 多线程c语言linuxsemaphore条件变量 (本文的读者定位是了解Pthread常用多线程API和Pthread互斥锁 ...
- R中的apply族函数和多线程计算
一.apply族函数 1.apply 应用于矩阵和数组 # apply # 1代表行,2代表列 # create a matrix of 10 rows x 2 columns m <- ma ...
- 多线程相关(pthread 、NSThread 、GCD、NSOperation)
进程 进程是指在系统中正在运行的一个应用程序 线程 1个进程要想执行任务,必须得有线程(每1个进程至少要有1条线程) 1个线程中任务的执行是串行的(执行完上一个才能执行下一个) 多线程 1个进程中可以 ...
- 多线程编程-pthread 未定义的引用
多线程编程时用到函数库 pthread.h ,但是该函数库不是linux默认的函数库,所以编译c文件时,需要在目标代码文件后加上 -lpthread参数. 1.未加上 -lpthread 编译时,报错 ...
- 并发编程~~~多线程~~~计算密集型 / IO密集型的效率, 多线程实现socket通信
一 验证计算密集型 / IO密集型的效率 IO密集型: IO密集型: 单个进程的多线程的并发效率高. 计算密集型: 计算密集型: 多进程的并发并行效率高. 二 多线程实现socket通信 服务器端: ...
- java多线程计算和
如题:如何利用多线程实现1~1000000000的和 本文利用Callable可以返回值的特性,并将执行结果用CompletionService进行存储,最后将分步值累加. import java.u ...
- pthread 多线程基础
本文主要介绍如何通过 pthread 库进行多线程编程,并通过以下例子进行说明. 基于莱布尼兹级数计算 \(\pi\) . 多线程归并排序 参考文章: [1] https://computing.ll ...
- pthread和semaphore的简单应用以及四个典型的多线程问题
pthread和semaphore的简单应用以及四个典型的多线程问题 pthread常用函数简单介绍 创建线程 int pthread_create(pthread_t * thread, pt ...
- 多线程并行计算数据总和 —— 优化计算思想(多线程去计算)—— C语言demo
多线程计算整型数组数据总和: #include <stdio.h> #include <stdlib.h> #include <Windows.h> #includ ...
随机推荐
- oracle 创建用户,授权用户,创建表,查询表
原文:oracle 创建用户,授权用户,创建表,查询表 oracle 创建用户,授权用户,创建表,查询表 假设oracle10g所有的都已经安装和配置好 第一步:win+R,进入运行,cmd; 第二步 ...
- Codeforces#277 C,E
C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...
- 织梦不仅是链接到其他调用next
//打开系统文件织梦 /include/arc.archives.class.php 找到GetPreNext函数 function GetPreNext($gtype='') { $rs = ''; ...
- leetcode[94] Unique Binary Search Trees
给定n,那么从1,2,3...n总共可以构成多少种二叉查找数呢.例如给定3 Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ ...
- 将DataTable内容导出到Excel表格的两种方法
方法一:循环DataTable单元格内容拼接字符串,利用StreamWriter的Write方法将字符串写入Excel文件中 这种方法很实现很简单.拼接字符串时,每个单元格之间添加'\t'(表示一个占 ...
- mysql存储过程及常用函数
原文:mysql存储过程及常用函数 一.函数 1.数学函数 CEIL()进一取整 SELECT CEIL(1.2);2 FLOOR()舍一取整 SELECT FLOOR(1.9);9 MOD取余数(取 ...
- 百度Web App在线生成平台Site App体验
最近收到百度开发者中心邮件,告知之前的百度移动建站服务已经升级为Site App了,Site App顾名思义是可以创建APP的站点,之前想建立一个APP要么是自己制作,要么是选用国外的在线Web A ...
- 【转】 Android用于提示等待的ProgressDialog
原文地址:http://blog.csdn.net/wleing/article/details/6086321 为了安抚用户等待的焦急心情,我们用ProgressDialog.它的用法书上有例子,就 ...
- MVC为什么不再需要注册通配符(*.*)了?
MVC为什么不再需要注册通配符(*.*)了? 文章内容 很多教程里都提到了,在部署MVC程序的时候要配置通配符映射(或者是*.mvc)到aspnet_ISPAI.dll上,在.NET4.0之前确实应该 ...
- Bootstrap 图像
一般的样式 在我们讨论 Bootstrap 3 提供的定义图像样式的特殊的 class 之前,我们将看到 Bootstrap 3 提供的定义图像的一般的样式. img { border: 0; } 这 ...