从新线程C调用方法 [英] calling method from a new thread C
问题描述
我正在研究一个项目(不是家庭作业),正在C语言中构建一个多线程数独解决方案验证器.我是C语言的新手,所以请原谅不良的代码质量,因为我还在不断改进.
I'm working on a project (NOT HOMEWORK), building a multi-thread sudoku solution validator in C. I'm new to C so excuse the bad code quality as I'm still improving.
我想从9个单独的线程中调用方法row_check
9次.对于方法作为参数,我传递行号(arg
)和数组名(arr
).我已经创建了线程,但是不确定如何将参数正确地传递给方法.有人可以帮我吗?
I want to call the method row_check
9 times from 9 separate threads. For the method as parameters I pass the row number (arg
) and array name (arr
). I have created the thread but I'm unsure how to pass the parameters properly to the method. Can anyone help me with this?
#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;
}
推荐答案
线程输入函数的格式必须为void *(*start_routine) (void *)
,这意味着它仅接收一个参数-指向您喜欢的任何内容的指针.
The thread entry function has to be of the format void *(*start_routine) (void *)
, which means it receives only one parameter - pointer to anything you like.
最常用的技术是使用要传递给线程输入函数的值定义struct
.创建该类型的变量,对其进行初始化,然后将其地址传递给线程输入函数.
The most used technique is to define a struct
with the values you want to pass to the thread entry function. Create a variable of that type, initialize it and pass its address to the thread entry function.
示例:
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);
}
您的输入函数应如下所示:
And your entry function should look something like this:
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;
}
这篇关于从新线程C调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!