2017-04-15 44 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