C线程和连接 [英] C threads and joining

查看:99
本文介绍了C线程和连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C线程及其返回值有疑问.我的目标是找到开始数字和结束数字之间的所有素数.我将有4个线程,每个线程占该范围的四分之一.

I have a question about C threads and their return value. My goal is to find all the prime numbers between a beginning number and an ending number. I will have 4 threads, and each thread does a quarter of the range.

例如,素数在1到100之间.

So for example, the primes between 1 and 100.

  • 线程1找到介于1到25之间的质数
  • 线程2 26-50
  • 线程3 51-75
  • 线程4 76-100

所有素数都将存储在数组中,并且会有一个计算素数的函数.

All the prime numbers will be stored in an array and there will be a function that computes the primes.

我的问题是,当我加入线程时

My question is, when I join the threads

pthread_join(tids[i], ptr);

ptr是指向所有质数1到100的组合数组的指针吗?

will ptr be a pointer to a combined array of ALL primes, 1 - 100?

表示如果我使用for循环来打印值

Meaning if I use a for loop to print the values

printf("%d", ptr[i]); 

它将1至100中的所有素数打印为一个大数组吗?

will it print all the primes from 1 - 100 as one big array?

我要加入4个单独的阵列吗?

Am I joining the 4 separate arrays?

谢谢

推荐答案

phtread_join()将通过ptr返回相应线程传递给 pthread_exit()的内容.每个线程都是独立工作的,并计算自己的素数集,因此,每个线程都应创建自己的数组,并且在所有线程加入后,您将打印每个数组的结果.为了能够返回素数及其计数的结果集,我们必须使用自己的结构类型:

phtread_join() will return through ptr what corresponding thread passes to pthread_exit(). Each thread is working independently and computes own set of primes, so, each thread should create own array and after all threads are joined you will print results of each array. To be able to return result set of primes and its count, we have to use own struct type:

struct comp_result {
     unsigned *p_arr;
     unsigned count;
};

我将说明不锁定的方式:

I will illustrate the way Without locking:

compute_thread (...) {
    ...
    struct comp_result *p_res = malloc(sizeof(struct comp_result));        
    p_res->p_arr = NULL;
    p_res->count = 0;
    for (num = start; num < end; num++) {
         if (is_prime(num)) { 
              p_res->count++; /// increment number of primes and expand our storage
              p_res->p_arr = realloc(p_res->p_arr, p_res->count*sizeof(int));
              p_res->p_arr[p_res->count-1] = num; // save prime in result array
         }
    }  

    // pass pointer to per-thread result data
    pthread_exit(p_res);
}


 main () {
      .... // create threads
      ....
      for (i = 0; i < num_of_threads; i++) {
           struct comp_result *p_res;
           // retrieve and print array from i-thread
           pthread_join(tids[i], &p_res);
           for (num = 0; num < p_res->count; num++) {
                printf(" %d ", p_res->p_arr[num]);
           }
           free(p_res->p_arr);
           free(p_res);
      }
 } 

带锁的插图还需要一种结构类型,因为我们将把结果共享数据的指针传递给每个线程:

The illustration with locking needs one more struct type since we will pass pointer to result shared data into each thread:

struct control {
    unsigned start;
    unsigned end;
    struct comp_result *p_res;
}

compute_thread (ptr) {
    struct control *p_cont = (struct control*)ptr;
    // retrieve the pointer to shared, but be accurate with it!
    struct comp_result *p_res = p_cont->p_res;

    // initialize lock
    pthread_mutex_init(&p_res->lock, NULL);

    ...
    for (num = p_cont->start; num < p_cont->end; num++) {
         if (is_prime(num)) {
              pthread_mutex_lock(&p_control->lock);
              // modify shared data with locking 
              p_res->count++; /// increment number of primes and expand our storage
              p_res->p_arr = realloc(p_res->p_arr, p_res->count*sizeof(int));
              p_res->p_arr[p_res->count-1] = num; // save prime in result array
              pthread_mutex_unlock(&p_control->lock);
         }
    }  

    pthread_exit(NULL);
}


 main () {
      //create one shared data and initialize it:
      struct comp_result *p_res = malloc(sizeof(struct comp_result));        

      p_res->p_arr = NULL;
      p_res->count = 0;

      for (i = 0; i < num_of_threads; i++) {
            // create per-thread control data:
            struct control *p_control = malloc(sizeof(struct control));        
            p_control->start = 
            p_control->end =
            p_control->p_res = p_res;
            pthread_crate(&tids[i], NULL, compute_thread, p_control);
      }
      ....
      for (i = 0; i < num_of_threads; i+++) {
           pthread_join(tids[i], NULL);
      }
      // now all threads are completed and we are ready to read result:
      for (num = 0; num < p_res->count; num++) {
           printf(" %d ", p_res->p_arr[num]);
      }
 } 

这篇关于C线程和连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆