当前位置: 首页 > news >正文

上海市建交人才网企业网站代码怎么优化

上海市建交人才网,企业网站代码怎么优化,wordpress图片排版乱,河北网站搜索排名优化方案这个例程为使用多线程配合互斥量进行点乘计算,相关的数据通过全局变量的形式存在,因此可以被各个线程访问;每个线程会在相关数据的不同区域上进行处理,主线程等待子线程完成操作后,将最后的结果打印出来。 代码如下 #…

        这个例程为使用多线程配合互斥量进行点乘计算,相关的数据通过全局变量的形式存在,因此可以被各个线程访问;每个线程会在相关数据的不同区域上进行处理,主线程等待子线程完成操作后,将最后的结果打印出来。

        代码如下

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>/* The following structure contains the necessary information* to allow the function "dotprod" to access its input data and* place its output into the structure.*/typedef struct
{double *a;double *b;double sum;int    veclen;
} DOTDATA;/* Define globally accessible variables and a mutex */#define NUMTHRDS 4
#define VECLEN 100DOTDATA dotstr;
pthread_t callThd[NUMTHRDS];
pthread_mutex_t mutexsum;/* The function dotprod is activated when the thread is created.* All input to this routine is obtained from a structure* of type DOTDATA and all output from this function is written into* this structure. The benefit of this approach is apparent for the* multi-threaded program: when a thread is created we pass a single* argument to the activated function - typically this argument* is a thread number. All  the other information required by the* function is accessed from the globally accessible structure.*/void *dotprod(void *arg)
{/* Define and use local variables for convenience */int i, start, end, len;long offset;double mysum, *x, *y;offset = (long)arg;len = dotstr.veclen;start = offset * len;end   = start + len;x = dotstr.a;y = dotstr.b;/*Perform the dot product and assign resultto the appropriate variable in the structure.*/mysum = 0;for (i = start; i < end ; i++) {mysum += (x[i] * y[i]);}/*Lock a mutex prior to updating the value in the sharedstructure, and unlock it upon updating.*/pthread_mutex_lock(&mutexsum);dotstr.sum += mysum;pthread_mutex_unlock(&mutexsum);pthread_exit((void*) 0);
}/* The main program creates threads which do all the work and then* print out result upon completion. Before creating the threads,* the input data is created. Since all threads update a shared structure,* we need a mutex for mutual exclusion. The main thread needs to wait for* all threads to complete, it waits for each one of the threads. We specify* a thread attribute value that allow the main thread to join with the* threads it creates. Note also that we free up handles when they are* no longer needed.*/int main (int argc, char *argv[])
{long i;double *a, *b;void *status;pthread_attr_t attr;/* Assign storage and initialize values */a = (double*) malloc (NUMTHRDS * VECLEN * sizeof(double));b = (double*) malloc (NUMTHRDS * VECLEN * sizeof(double));for (i = 0; i < VECLEN * NUMTHRDS; i++) {a[i] = 1.0;b[i] = a[i];}dotstr.veclen = VECLEN;dotstr.a = a;dotstr.b = b;dotstr.sum = 0;pthread_mutex_init(&mutexsum, NULL);/* Create threads to perform the dot product  */pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);for(i = 0; i < NUMTHRDS; i++) {/* Each thread works on a different set of data. The offset is specified* by 'i'. The size of the data for each thread is indicated by VECLEN.*/pthread_create(&callThd[i], &attr, dotprod, (void *)i);}pthread_attr_destroy(&attr);/* Wait on the other threads */for(i = 0; i < NUMTHRDS; i++) {pthread_join(callThd[i], &status);}/* After joining, print out the results and cleanup */printf("Sum =  %f\n", dotstr.sum);free(a);free(b);pthread_mutex_destroy(&mutexsum);pthread_exit(NULL);
}
http://www.yayakq.cn/news/199539/

相关文章:

  • 东莞专业网站建站设计有谁知道哪个网址
  • 中国建设工程招聘信息网站做企业网站一定要企业邮箱嘛
  • 广告素材网站松江公司做网站
  • 四川省建设工程设备安全协会网站迅速网站
  • 迅雷资源做下载网站工业网站素材
  • 购物小网站建设杭州seo关键字优化
  • 手机网站开发要多久动态asp.net网站开发
  • 企业级网站开发与部署wordpress 设成中文版
  • 做个网站页面多钱提供网站建设服务平台
  • 女人和男人做床上爱网站wordpress实现中英文切换
  • 网站设置的关键词做新媒体的小说网站
  • 珠海建设公司网站立方集团 网站
  • 做悬浮导航的网站磁力宅
  • 兴平网站建设外贸实用工具
  • 做网站有哪些好处创办一个网站多少钱
  • 建设银行临江市支行网站网球新闻最新消息
  • 临沂网站建设求职简历文件外链生成网站
  • 企业互联网网站定位微信哪家公司开发的
  • 安溪县建设局网站wordpress首页文章图片不显示
  • 广州市网站建设制作费用韩国唯美网站设计
  • 福州网站平台建设公司北京网站上排名
  • 济南网站搭建公司网站开发网上宠物店管理系统
  • flash网站后台在电脑上建设个人网站
  • 网站php环境搭建wordpress 添加icp
  • 网站环境配百度爱采购推广怎么收费
  • 免费用手机建立网站网页游戏大全2010
  • 怎么看网站域名眯眯扑克app哪个公司开发
  • 做任务刷王者皮肤网站网站开发php js
  • 与网站建设有关的课程和知识点织梦网站404怎么做
  • 外贸网站分析阿里巴巴国际站运营模式