C程序在没有任何输出的情况下退出

C程序在没有任何输出的情况下退出,c,multithreading,pthreads,C,Multithreading,Pthreads,这有一个关于多线程问题的问题。现在的问题是程序在没有任何输入的情况下退出。该程序从一个文本文件中获取输入,该文本文件以参数形式给出并执行。它应该只包含由空格分隔的数字,如果有任何其他字符,它应该给出一个错误,就像在行检查函数中所做的那样。有人能提出为什么它会毫无错误地退出吗 #include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<

这有一个关于多线程问题的问题。现在的问题是程序在没有任何输入的情况下退出。该程序从一个文本文件中获取输入,该文本文件以参数形式给出并执行。它应该只包含由空格分隔的数字,如果有任何其他字符,它应该给出一个错误,就像在行检查函数中所做的那样。有人能提出为什么它会毫无错误地退出吗

#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;

}
#包括
#包括
#包括
#包括
#包括
const unsigned int NUM OF_线程=9;
typedef结构线程数据{
char*ptr;
int row_num;
}线程数据;
无效报告(常量字符*s、整数w、整数q);
作废*行检查(作废*数据)
{
线程\数据\ t*我的\数据\ ptr=数据;
int j,旗帜;
flag=0x0000;
对于(j=0;j<9;j++)
{
标志|=1u ptr)[j]-1);
如果(标志!=0x01FF){
报告(“行”,我的数据->行数,j-1);
}
}   
返回NULL;
}
无效报告(常量字符*s、整数w、整数q)
{
printf(“\n数独不正确”);
printf(“\n在%s中。行:%d,列:%d”,s,w+1,q+1);
getchar();
出口(0);
}
int main(int argc,char*argv[])
{
int i,j;
字符arr1[9][9];
FILE*FILE=fopen(argv[1],“r”);
如果(文件==0)
{
fprintf(标准“失败”);
出口(1);
}
int col=0,row=0;
int-num;
而(fscanf(文件“%c”,&num)==1){
arr1[row][col]=num;
col++;
如果(列==9)
{
行++;
col=0;
}
}
fclose(文件);
int n;
线程数据[线程数];
pthread_t tid;
pthread_attr_t attr;
对于(n=0;n
以下是代码中的一个问题,它将解释为什么应用程序会这么快就存在

下面的代码没有连接它创建的所有线程(因此应用程序在线程完成运行之前退出并终止线程):

这样,应用程序将等待所有线程完成任务(并报告任何错误),然后再返回。

有人能建议为什么它会在没有任何错误的情况下退出吗

#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;

}

当拼图结果的9行都正确时,发布的代码没有任何动作,它只是优雅地退出

进一步混淆逻辑

发布的代码只检查创建的最后一个线程,当该线程退出时,程序退出,这并不意味着其他线程已经退出


还有一个严肃的细节。调用
pthread_create()
传递的是
attr
变量的地址,但该变量包含声明该变量的堆栈上的垃圾。由于代码没有为线程设置任何特定属性,强烈建议删除该变量,只需在第二个参数中使用NULL即可
pthread\u create()

更新:文件读取工作正常,也会创建线程。问题似乎是在调用行检查时?建议
fscanf(文件,“%c”,&num)==1
(移动空格)是否尝试调试它?取决于您在哪个操作系统和编译器上使用哪个调试器以便于可读性和理解:1)一致地缩进代码。在每个左大括号“{”之后缩进。在每个右大括号“}”之前取消缩进。建议每个缩进级别使用4个空格。2) 遵循公理:每行只有一条语句,每条语句(最多)有一个变量声明。将程序的各个元素命名为同一个名称(只有大小写差异)是一种糟糕的编程实践。即
文件
文件
thread_data_t data[NUM_OF_THREADS];
pthread_t tid[NUM_OF_THREADS];

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


for(n=0; n < NUM_OF_THREADS; n++)
{
     pthread_join(tid[n], NULL);
}