简体   繁体   English

Pthread_join Printf

[英]Pthread_join Printf

I have this problem that I'm studying but I'm not understanding one part. 我正在研究这个问题,但我不了解其中的一部分。 The script is not in English so translating would be pretty tedious but the basic problem is to make a thread read a specific text file and find a specific word. 该脚本不是英语的,因此翻译会很繁琐,但是基本问题是使线程读取特定的文本文件并找到特定的单词。 Each file has its own thread and all that. 每个文件都有其自己的线程。 The last 2 problems are making sure that various occurrences on the same file are printed together, like: 最后两个问题是确保将同一文件上的各种匹配项打印在一起,例如:

file1: line 1
file1: line 2
file2: line 1

and so on. 等等。 I could solve this using a global 2d array and creating a structure to pass to the thread its "id" and the name of the txt file it has to search. 我可以使用全局2d数组并创建一个结构以将其“ id”和必须搜索的txt文件的名称传递给线程来解决此问题。 I used pthread_join and it´s pretty intuitive. 我使用了pthread_join ,它非常直观。 The problem is in the next problem, solve the same problem but without pthread_join , and with no busy waiting if possible. 问题出在下一个问题中,解决相同的问题,但没有pthread_join ,并且如果可能的话也没有忙碌的等待。 The problem is, if I don't use pthread_join , I can't print anything on the thread function and I wasn't expecting this? 问题是,如果我不使用pthread_join ,那么我就无法在线程函数上打印任何内容,而且我没想到吗? Is there a reason why this happens? 为什么会发生这种情况?

This is the code I used for solving the problem with pthread_join . 这是我用来解决问题的代码pthread_join Without pthread_join , using a mutex and trying to print the output on the thread function, I got no output. 没有pthread_join ,使用互斥锁并尝试在线程函数上打印输出,我没有输出。

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>
#include<fcntl.h>
#include <pthread.h>
#define k 4
#define l 100

int match_line(int fd, char *str);
void *ocorre(void *);

char string[100];
int b[k][l];
int max;

struct args{
    char str[256];
    int id;
};
int main(int argc, char *argv[]){

    int i=0;
    int j=0;
    max=argc-1;

    struct args arg[max];
    pthread_t a[max];

    strcpy(string,argv[1]); //global

    for(i=0;i<max-1;i++){   //criaçao de threads
        arg[i].id=i;
        strcpy(arg[i].str,argv[i+2]);
        pthread_create(&a[i],NULL,ocorre,&arg[i]);
    }

    for(i=0;i<max-1;i++){ //join
        pthread_join(a[i],NULL);
            for(j=0;b[i][j]!=0;j++){
                printf("%s : %d\n",arg[i].str,b[i][j]);
            }
    }
}
void *ocorre(void *arg) {

    int fd;
    int j=0;
    struct args func;
    func=*(struct args*)arg;

    fd=open(func.str,O_RDONLY);
        while(1){
        b[func.id][j]=match_line(fd,string);
            if(b[func.id][j]==0) 
                break;
        j++;
    }

    return NULL;
}

This is what i did to try to solve without pthread_join. 这是我在没有pthread_join的情况下尝试解决的问题。 To obtain the first output i have to add sleep(1) after creating the thread 为了获得第一个输出,我必须在创建线程之后添加sleep(1)

Returning from main terminates the process because it's roughly equivalent to calling exit . main返回将终止该过程,因为它大致等效于调用exit You have two choices: 您有两种选择:

  1. Make sure main doesn't return or otherwise end until all work is done, or 确保main在完成所有工作之前不返回或以其他方式结束

  2. Call phthread_exit in main rather than returning. main调用phthread_exit而不是返回。

Ideally, you would create all your threads as joinable and some shutdown code, that runs only after all useful work is done, arranges for the threads to terminate and joins them. 理想情况下,应将所有线程创建为可连接的,并创建一些关闭代码,这些代码仅在完成所有有用的工作后运行,安排线程终止并加入它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM