多线程计算----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 ...
随机推荐
- 使用flex和bison实现的sql引擎解析
因为老师要求,近期在做oceanbase存储过程的实现,在oceanbase 0.4曾经是不支持存储过程的.实现的主要步骤主要包含 1.语法解析 2.词法解析 3.详细运行语法树的步骤 如今先来说说语 ...
- 一个可以直接使用的MsgBox基于form居中API
可直接复制DialogBox项目(文件夹)到需要的项目中然后直接引用和using CodeProject.Dialog 已修正原作者代码错误的地方,可直接使用,VS2010测试成功 具体可以参考案例T ...
- Spring IOC之容器概述
1.SpringIOC容器和beans介绍 IOC的依赖注入是这样的,对象定义他们的依赖也就是他们需要在一起起作用的对象是通过构造器参数以及工厂方法的参数或者是当他们被构建或者是从工厂中返回时设置在对 ...
- 数据库备份还原工具EMS SQL Angel for SQL Server发布1.3版本
EMS公司,是专门从事企业数据库以及内置于多层次客户服务器结构自动化开发.其EMS SQL Angel for SQL Server工具,便是SQL Servers数据库数据备份还原工具,并且还能使用 ...
- rem测试用实现移动端自适应页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 处理程序“svc-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 解决办法:以管理员运行命令:C: ...
- linux 启动oracle报cannot restore segment prot after reloc: Permission denied
error while loading shared libraries: $ORACLE_HOME/lib/libnnz10.so: cannot restore segment prot afte ...
- php表单(2)
学习php表单 主要是想知道 前端通过submit之后 后端是如何进行操作的.现在实现一个效果:点击submit,输入框的信息不会被刷掉:刷新页面,输入框的信息被刷掉(index.php). < ...
- windows下grunt安装提示不成功
在电脑按了node.js之后,在cmd中 输入 npm install -g grunt-cli 注意啦 不是 npm install -g grunt-cli 中间多了一个空格就安装不成功了,这个空 ...
- jQuery在html有效,在jsp无效的原因
最近用jQuery来写下拉框的选项值的左右移动,代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN& ...