繁体   English   中英

c 中的 Posix 线程

[英]Posix thread in c

我正在阅读 Andrew 和 Bracy 的一本名为“All of programming”的书。 目前,我在并发,遇到了这个 function:

void smoothParallel(image_t * src, image_t * dst, int nThreads){
   int perThread = src->height/ nThreads + 1;
   int extras = src->height/nThreads;
   ...
   pthread_t * threads = malloc(nThreads * sizeof(*threads));
   for(int i = 0; i < nThreads; i++){
      ...
      perThread--;
   }
   thr_arg * arg = malloc(sizeof(*arg));                     
   arg->src = src;
   arg->dst = dst;
   arg->startY = curr;
   arg->endY = curr + perThread;
   curr += perThread; 
   pthread_create(&threads[i], NULL, smoothThread, arg);
   for (int i = 0; i < nThreads ; i++) {
       pthread_join(threads[i], NULL);
   }
}

所以,我无法理解的是int perThread = src->height/nThreads + 1; 为什么它nThreads + 1不仅仅是nThreads ,我知道要问的问题很幼稚,但我无法弄清楚背后的原因。

我发现 nThreads + 1 是主线程,因为有 N+1 个主线程产生线程并等待只有 N 个的工作线程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM