2017-04-15 22 views
0

我正在开发一个项目(而不是HOMEWORK),在C中构建一个多线程数独解决方案验证程序。我是C新手,所以请原谅我的代码质量不好日臻完善。从一个新线程调用方法C

我想从9个独立的线程中调用方法row_check 9次。对于作为参数的方法,我传递行号(arg)和数组名称(arr)。我创建了线程,但我不确定如何正确地将参数传递给方法。谁能帮我这个?

#include <pthread.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 


void* row_check(void* arg, int *arr) 
{ 
    int i = *((int *)arg); //trying to convert row number to int 
    int j, flag; 

    while(i < 9) 
    { 
     flag=0x0000; 

     for(j = 0; j < 9; j++) 
      flag |= 1 << (arr[i][j]-1); 

     if (flag != 0x01FF) 
      report("row", i, j-1); 
    } 

} 

void report(char *s, int i, int j) 
{ 
    printf("\nThe sudoku is INCORRECT"); 
    printf("\nin %s. Row:%d,Column:%d", s, i+1, j+1); 
    getch(); 
    exit(0); 
} 


int main(int argc, char* argv[]) 
{ 
    int i,j; 
    char arr1[9][9]; 
    FILE *file = fopen(argv[1], "r"); 

    if (file == 0) 
    { 
     fprintf(stderr, "failed"); 
     exit(1); 
    } 
     int col=0, row=0; 
     int num; 

     while(fscanf(file, "%d ", &num) == 1) 
     { 
     arr1[row][col] = num; 
     col++; 
     if(col == 9) 
     { 
      row++; 
      col = 0; 
     } 
     } 
     fclose(file); 

     pthread_t tid; 
     pthread_attr_t attr; 
     pthread_attr_init(&attr); 

     int n; 
     for(n=0; n < 9; n++) //creating 9 threads 
     { 
      pthread_create(&tid, &attr, row_check, n); 
      pthread_join(tid, NULL); 
     } 

     return 0; 
} 
+0

您可以使用结构来存储两个变量,即PAS结构线程程序。 你只想传递数组名称,即一个字符串,或者你想用数组的值做些什么? – Gaurav

+1

阅读'pthread_join'手册,因为当你调用它的时候,就像你在'for'循环中调用'row_check'一样。 'pthread_join'等待线程完成,所以这些线程不会同时运行,而是一个接一个地运行。另外,在'row_check'('j-1')中传递给'report'的第三个参数总是8,因为在'for'循环结束之后'j'等于9 – Rogus

+0

我没有很好的感觉代码中的while循环!你能解释一下吗? – Gaurav

回答

0

线程入口函数必须是格式void *(*start_routine) (void *),这意味着它只能接收一个参数 - 指向你喜欢的任何东西。

最常用的技术是用想要传递给线程入口函数的值定义struct。创建一个该类型的变量,初始化它并将其地址传递给线程入口函数。

例子:

typedef thread_data_s 
{ 
    char *ptr; 
    int row_num; // I would prefer to define it as `unsigned int` but I stick to your example 
    // + any other data you want to pass to the thread 
} thread_data_t; 

.... 

thread_data_t data[NUM_OF_THREADS]; 

.... 

for(n=0; n < NUM_OF_THREADS; n++) //creating 9 threads 
{ 
    data[n].ptr = &arr1[n][0]; 
    data[n].row_num = n; 
    pthread_create(&tid, &attr, row_check, &data[n]); 
} 

... 

for(n=0; n < NUM_OF_THREADS; n++) // waiting for all the threads here 
{ 
    pthread_join(tid, NULL); 
} 

和你的入口函数应该是这个样子:

void* row_check(void* data) 
{ 
    //int i = *((int *)arg); //trying to convert row number to int 
    thread_data_t *my_data_ptr = data; 
    int j, flag; 

    while(i < 9) 
    { 
     flag=0x0000; 

     for(j = 0; j < 9; j++) 
      flag |= 1u << ((my_data_ptr->ptr)[my_data_ptr->row_num][j] - 1); 
     // Shouldn't it be under the `for` loop block? If so, please add `{}` to the `for` loop 
     if (flag != 0x01FF) 
      report("row", my_data_ptr->row_num, j-1); 
    } 

    return NULL; 
} 
+0

'thread_data_t * my_data_ptr = data;'需要类型转换。 – Gaurav

+0

@GauravPathak我不确定在* C *中需要对另一个指针类型进行类型'void *'的类型转换。但我会稍后检查并根据需要进行修复。 –

+0

通过您的评论通知我。请。 我也很想知道它。 Thx。 – Gaurav