Basic structure of main.c:

int main ()
{
    Open the file data.txt and obtain the file handler fh;

    Create a thread my_thread using pthread_create; pass fh to my_thread;

    Wait until my_thread terminates, using pthread_join;

    Print out how many lines exist in data.txt.
}

Basic structure of thread_function.c:

void *count_lines(void *arg)
{
    Obtain fh from arg;


    Return num_lines
}
--------------------------------------------------------------

How can i pass file handler from main.c to thread_function.c

Recommended Answers

All 3 Replies

Well you have

void *count_lines(void *arg)

I would use the void *arg to pass the file handle

like

count_lines(&fh)

one .c file will contain the above main.c decleration other file will contain only the thread.c function .....when i wll compile them using gcc ....in main.c file it will show number of lines ...which it will take from thread.c ......main.c file send file handler to thread.c file

You could declare some global variable so you could exchange handle in it but you must do it carefully.

IMHO I think you don't need to make a thread for a line-counting program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.