【发布时间】:2017-04-17 00:19:44
【问题描述】:
这有一个关于多线程问题Here 的先前问题。现在的问题是程序在没有任何输入的情况下退出。该程序从作为参数给出的文本文件中获取输入,并执行。它应该只包含用空格分隔的数字,如果有任何其他字符,它应该给出一个错误,就像在 row_check 函数中所做的那样。谁能建议它为什么会退出而没有任何错误?。
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<ncurses.h>
const unsigned int NUM_OF_THREADS = 9;
typedef struct thread_data_s {
char *ptr;
int row_num;
} thread_data_t;
void report(const char *s,int w,int q);
void* row_check(void* data)
{
thread_data_t *my_data_ptr = data;
int j, flag;
flag=0x0000;
for(j = 0; j < 9; j++)
{
flag |= 1u << ( (my_data_ptr->ptr)[j] - 1 );
if (flag != 0x01FF){
report("row", my_data_ptr->row_num, j-1);
}
}
return NULL;
}
void report(const char *s,int w,int q)
{
printf("\nThe sudoku is INCORRECT");
printf("\nin %s. Row:%d,Column:%d",s,w+1,q+1);
getchar();
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, "%c ", &num) ==1) {
arr1[row][col] = num;
col++;
if(col ==9)
{
row++;
col = 0;
}
}
fclose(file);
int n;
thread_data_t data[NUM_OF_THREADS];
pthread_t tid;
pthread_attr_t attr;
for(n=0; n < NUM_OF_THREADS; n++)
{
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++)
{
pthread_join(tid, NULL);
}
return 0;
}
【问题讨论】:
-
更新:文件读取工作,线程也被创建。问题似乎是何时调用行检查?
-
建议
fscanf(file, " %c", &num) ==1(移动空间) -
你试过调试吗?取决于您在哪个调试器上使用哪个操作系统和编译器
-
为了便于阅读和理解:1) 一致地缩进代码。在每个左大括号 '{' 后缩进。在每个右大括号 '}' 之前不缩进。建议每个缩进级别使用 4 个空格。 2) 遵循公理:每行只有一个语句,并且(最多)每条语句有一个变量声明。
-
将程序的各种元素命名为相同的名称是一种糟糕的编程习惯,只有大小写不同。 IE。
FILE和file
标签: c multithreading pthreads