Question about threads (I don't understand how they work)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Question about threads (I don't understand how they work)
I have in a file some words separated by a space on each line. What I'm trying to do is to get the first word from each line and create a thread for each word. In the thread function all I want for now is to print that word.
I tried to do it like this:
This is the argument structure, which I use to send the parameters to the thread:
Code:
typedef struct
{
char *nume_user;
} argument;
This is the thread function:
Code:
void
f (argument * a)
{
printf ("I am %s\n", a->nume_user);
}
This is the main() function:
Code:
FILE *fd;
char *nume_fis;
nume_fis = (char *) malloc (10 * sizeof (char));
printf ("The name of the file: ");
scanf ("%s", nume_fis);
int nr = nr_linii (nume_fis); // this returns the number of lines in the file, it works
argument arg[nr];
pthread_t th[nr]; // vectorul thread-urilor
char *linie, *s;
linie = (char *) malloc (100 * sizeof (char));
s = (char *) malloc (10 * sizeof (char));
fd = fopen (nume_fis, "r");
int i = 0;
while (!feof (fd))
if (fgets (linie, 100, fd))
{
sscanf (linie, "%s", s);
arg[i].nume_user = s;
pthread_create ((pthread_t *) & th[i], NULL, (void *) f, (void *) &arg[i]);
i++;
}
fclose (fd);
// astept sa se termine thread-urile
for (i = 0; i < nr; i++)
pthread_join (th[i], NULL);
The problem is I get the message in the thread function only for the last line in the file, x times (where x is the number of lines in the file).
Thank you for any suggestions.
The problem is that you have only one buffer that you are reading the strings into: "s" . Before the threads have a chance to get going, the file read loop continues and the contents of s are replaced by every line of the file, finally finishing with "s" containing the last line of the file.
A way to fix this with minimal changes is to put the malloc that allocates "s" inside the loop, right before the sscanf.
The problem is that you have only one buffer that you are reading the strings into: "s" . Before the threads have a chance to get going, the file read loop continues and the contents of s are replaced by every line of the file, finally finishing with "s" containing the last line of the file.
Can you please explain me why is this happening? Because I have the pthread_create() function in the same loop with the fgets, it looks fine to me. Thank you.
Keep in mind that the call to pthread_create() returns immediately but the function f() may or may not start right away. In your particular case, the loop continued and the contents of "s" were overwritten with the next line in the file before f() had a chance to get going. Three threads are scheduled to run and call function f() but none of them get to the point of actually calling f() before the loop completes. Because they each are getting the same "char *s" pointer, they each end up with the same data. This is the string that "s" points to when the file read loop finishes.
It might make things clearer if you put a printf just after the read loop. (If you do this, don't forget that the printf itself might change the timing/behavior of the program).
Threaded programs in general are tricky to write and can be trickier to debug.
Thank you very much. Now I have understood. I still have one more question: have you read any tutorial about threads or it's just experience? If you know a good tutorial about threads, a link would be very appreciated. Thank you again.
The rest of the paper will try to convince you that while there is a limited class of problems for which threads are a good solution, your problem is almost certainly not among them -- no matter what your problem is.
Is this actually true? Because it sounds to me a little bit like a joke... Thank you, it was nice reading.
Is this actually true? Because it sounds to me a little bit like a joke... Thank you, it was nice reading.
well in the situation of:
Quote:
I have in a file some words separated by a space on each line. What I'm trying to do is to get the first word from each line and create a thread for each word. In the thread function all I want for now is to print that word.
I can see no reason to use threads. why not just use the main thread?
<edit>
and is printf thread safe?</edit>
have you read any tutorial about threads or it's just experience?
Mostly experience. I don't know of any good online tutorials ( I'm sure there are some out there ). The best book that I know of on pthreads is the O'Reilly "Pthreads Programming" book by Nichols, Buttlar & Farrell .
If c++ is an option, the boost.org threading library provides a nice interface to threading primitives that both helps simplify your code and will make it more portable.
Quote:
The rest of the paper will try to convince you that while there is a limited class of problems for which threads are a good solution, your problem is almost certainly not among them -- no matter what your problem is.
I think this is true as far as it goes. However, it does not apply if:
You have to understand/debug/fix existing code that someone else wrote that uses threads.
You want to (or have to) learn how to write threaded programs.
Quote:
I have in a file some words separated by a space on each line. What I'm trying to do is to get the first word from each line and create a thread for each word. In the thread function all I want for now is to print that word.
Well, dmail is right that there's no reason to use threads to print out a file. My guess is that this is a simplification of something more complicated which is always a good idea when you're trying to understand (or create) that more complicated thing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.