如何将网格传递给“void my_func(void * args)”

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将网格传递给“void my_func(void * args)”相关的知识,希望对你有一定的参考价值。

我正在使用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.
", 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.
", 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结构执行相同的操作,但是在不同的头文件中,并包含它的定义。

创建一个名为.cthread_func.c文件,并将my_func(struct thread_data_s *data)的定义放在那里。确保#include你已经定义了你的结构的标题,但除了你的#include "thread_func.h"之外不要main.c。我也强烈建议你不要指点你的指针。

如果你想看一个我所描述的布局的例子,我有一个on my GitHub

编辑: 至于将网格传递给my_func,只需将其作为thread_data_s的一部分。

以上是关于如何将网格传递给“void my_func(void * args)”的主要内容,如果未能解决你的问题,请参考以下文章

Telerik MVC Extensions Grid - 如何让网格过滤器应用于初始 LINQ 查询或传递给 db?

如何将值传递给paint方法

如何通过传递对象使用 [ngstyle] 将网格行和网格列动态传递到角度 div 选项卡

如何从通过按钮单击呈现的部分视图将recordId传递给模型构造函数

如何将模型用作 Kendo UI 网格的数据源?

当相应的图像视图被触摸时,如何将图像的数据库 ID 传递给方法?