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

星裕建设网站青岛微网站建设

星裕建设网站,青岛微网站建设,通江移动网站建设,wordpress 如何发布文章这个例程为使用多线程配合互斥量进行点乘计算#xff0c;相关的数据通过全局变量的形式存在#xff0c;因此可以被各个线程访问#xff1b;每个线程会在相关数据的不同区域上进行处理#xff0c;主线程等待子线程完成操作后#xff0c;将最后的结果打印出来。 代码如下 #…        这个例程为使用多线程配合互斥量进行点乘计算相关的数据通过全局变量的形式存在因此可以被各个线程访问每个线程会在相关数据的不同区域上进行处理主线程等待子线程完成操作后将最后的结果打印出来。 代码如下 #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.dnsts.com.cn/news/37952.html

相关文章:

  • 如何做网站?做视频网站利润如何处理
  • 储煤棚网架公司优化培训课程
  • 沈阳制作公司网站和app做视频网站 版权怎么解决
  • 网站项目策划书方案wordpress输出友情链接
  • 做面食网站c 网站建设可视化实现
  • 公司做网站的申请建设企业网站登录
  • 上海做网站优化的公司我想在泉州做网站
  • 根据链接获取网站名称可以找题目做的网站
  • 做网站需要多少钱平邑wordpress电话注册
  • 网站背景自动变色设计网站怎么设计
  • 哪些网站可以做百科来源2017年网站建设工作总结
  • 备案网站多长时间网站开发的就业前景
  • 网站建设维护费摊销做网站游戏总结的例文
  • 网站建设要托管服务器长沙注册公司可以买房吗
  • 汕头h5模板建站网站建设是啥
  • 网站建设需要多少钱费用win2003 wordpress 安装
  • 阜蒙县建设小学校官方网站佛山网络推广seo
  • 网站开发 c展厅展示公司
  • 哪里做网站比较稳定缘魁网站建设
  • 诚聘php网站开发师合肥做兼职网站设计
  • 济南市高新技术官方网站开发区给钱做h事都行的网站名
  • 开源网站源码下载用wordpress二级菜单导航栏
  • 模仿网站页面违法吗中国会出兵吗
  • 婚庆网站建设必要性合肥公司注册地址
  • 买卖信息网站上饶网站建设srsem
  • 卡片形式的网站昌大建设集团是哪里的
  • 网站大全app下载小制作小发明手工初中
  • 百度收录网站中文称Wordpress网站删除多余主题
  • 马鞍山做网站的公司手机在线建网站
  • 杭州四喜做网站建设么科技公司网站首页