c - 如何将网格传递给 "void my_func(void *args)"

标签 c pthreads

我正在使用 pthread_create() 调用标题行为 void * my_func(void *args) 的函数。此函数从数据结构中类型转换它需要的所有参数。

但是,my_func() 还需要访问在别处创建的网格。

我试过将网格作为输入参数与 void *args 一起传递,但这会导致函数 pthread_create() 抛出错误,因为它没有不允许/期待这一点。

我尝试将网格添加到结构中(不确定这是否合法),但这导致网格在 my_func() 中“未声明”,无论如何。

pthreads_create() 代码:

     for (k = 0; k < num_threads; k++)
        pthread_create (&thread_id[k], &attributes, my_func, (void *) &thread_data[k]);

my_func()代码:

/* This function is executed by each thread to compute the overall Gauss-Seidel. */
void *
my_func (void *args)
{

/* Typecast the argument to a pointer to the thread_data_t structure. */
    thread_data_t *thread_data = (thread_data_t *) args;        

  // int num_iter = 0;
    int done = 0;
    int i, j;
    double diff;
    float old, new; 
    float eps = 1e-2; /* Convergence criteria. */
    int num_elements; 

    int num_iter = 0;

  if (thread_data->tid < (thread_data->num_threads - 1)) {
    for (int k = thread_data->offset; k < (thread_data->offset + thread_data->chunk_size); k++)
        while(!done) { /* While we have not converged yet. */
            diff = 0.0;
            num_elements = 0;

            for (i = 1; i < (grid->dim - 1); i++) {
                for (j = 1; j < (grid->dim - 1); j++) {
                    old = grid->element[i * grid->dim + j]; /* Store old value of grid point. */
                    /* Apply the update rule. */    
                    new = 0.25 * (grid->element[(i - 1) * grid->dim + j] +\
                                  grid->element[(i + 1) * grid->dim + j] +\
                                  grid->element[i * grid->dim + (j + 1)] +\
                                  grid->element[i * grid->dim + (j - 1)]);

                    grid->element[i * grid->dim + j] = new; /* Update the grid-point value. */
                    diff = diff + fabs(new - old); /* Calculate the difference in values. */
                    num_elements++;
                }
            }

            /* End of an iteration. Check for convergence. */
            diff = diff/num_elements;
            printf ("Iteration %d. DIFF: %f.\n", num_iter, diff);
            num_iter++;

            if (diff < eps) 
                done = 1;
            } // end while
  } //end if

  else { /* This takes care of the number of elements that the final thread must process. */  
     int done2 = 0;
    for (int k = thread_data->offset; k < thread_data->dim; k++)         
      while(!done2) { /* While we have not converged yet. */
            diff = 0.0;
            num_elements = 0;

            for (i = 1; i < (grid->dim - 1); i++) {
                for (j = 1; j < (grid->dim - 1); j++) {
                    old = grid->element[i * grid->dim + j]; /* Store old value of grid point. */
                    /* Apply the update rule. */    
                    new = 0.25 * (grid->element[(i - 1) * grid->dim + j] +\
                                  grid->element[(i + 1) * grid->dim + j] +\
                                  grid->element[i * grid->dim + (j + 1)] +\
                                  grid->element[i * grid->dim + (j - 1)]);

                    grid->element[i * grid->dim + j] = new; /* Update the grid-point value. */
                    diff = diff + fabs(new - old); /* Calculate the difference in values. */
                    num_elements++;
                }
            }

            /* End of an iteration. Check for convergence. */
            diff = diff/num_elements;
            printf ("Iteration %d. DIFF: %f.\n", num_iter, diff);
            num_iter++;
                  if (diff < eps) 
                done = 1;

        } // end while
  }// end else    

    /* Store num_iter into the num_iter array. */
    thread_data->num_iter[thread_data->tid] = num_iter;    

    pthread_exit (NULL);
}

最佳答案

创建一个头文件(将其命名为 thread_func.h 或您喜欢的任何名称)

#ifndef THREAD_FUNC_H
#define THREAD_FUNC_H
void* my_func(void *args);
#endif

为您的 thread_data_s 做同样的事情结构,但在不同的头文件中,并包含它的定义。

创建一个 .c名为 thread_func.c 的文件, 并定义 my_func(struct thread_data_s *data)那里。确保 #include您定义结构的 header ,但不 #include "thread_func.h"除了你的 main.c .我还强烈建议您不要转换您的指针。

如果您想查看我描述的布局示例,我有一个 on my GitHub .

编辑:
至于将网格传递给my_func , 让它成为 thread_data_s 的一部分.

关于c - 如何将网格传递给 "void my_func(void *args)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55894916/

相关文章:

c++ - 如何为“(1+3 * ( 5/4)) 之类的字符串编写求值器并获得数值结果

c - 在 Linux 中检索网络摄像机流 URL

c - POSIX API 调用以列出进程中运行的所有 pthreads

c++ - 产生线程时死锁?

multithreading - 不同平台的奇怪多线程输出

c - Pthread编程: Segmentation Fault: 11 while using mutex

c - 如何中断选择

c - 这段代码实际上是如何工作的?

C 请求用户txt文件,保存在数组中,并以txt输出

c - 有没有办法在 objective-c 程序中获取执行语句的顺序?