LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-12-2005, 07:41 AM   #1
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Rep: Reputation: 31
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.

Last edited by zahadumy; 12-12-2005 at 07:42 AM.
 
Old 12-12-2005, 10:51 AM   #2
YetAnotherDave
Member
 
Registered: Feb 2005
Posts: 94

Rep: Reputation: 17
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.

Code:
  char *linie, *s;
  linie = (char *) malloc (100 * sizeof (char));
  fd = fopen (nume_fis, "r");
  int i = 0;
  while (!feof (fd))
    if (fgets (linie, 100, fd))
    {
      s = (char *) malloc (10 * sizeof (char));
      sscanf (linie, "%s", s);
      arg[i].nume_user = s;

      pthread_create ((pthread_t *) & th[i], NULL, (void *) f, (void *) &arg[i]);
      i++;
    }
  fclose (fd);
 
Old 12-12-2005, 12:49 PM   #3
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Original Poster
Rep: Reputation: 31
Sometimes I feel so stupid... Thank you my friend, this was my problem.
 
Old 12-12-2005, 01:34 PM   #4
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by YetAnotherDave
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.
 
Old 12-12-2005, 10:23 PM   #5
YetAnotherDave
Member
 
Registered: Feb 2005
Posts: 94

Rep: Reputation: 17
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.

- Dave
 
Old 12-13-2005, 04:22 AM   #6
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Original Poster
Rep: Reputation: 31
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.
 
Old 12-13-2005, 05:29 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
Exclamation threads considered harmful

http://www.kuro5hin.org/?op=displays...1/18/22112/860
 
Old 12-13-2005, 06:23 AM   #8
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Original Poster
Rep: Reputation: 31
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.
Is this actually true? Because it sounds to me a little bit like a joke... Thank you, it was nice reading.
 
Old 12-13-2005, 07:17 AM   #9
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by zahadumy
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>

Last edited by dmail; 12-13-2005 at 07:21 AM.
 
Old 12-13-2005, 07:22 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
the "stevens" book discourages their use too.

I take advice from these until I know better
 
Old 12-13-2005, 12:19 PM   #11
YetAnotherDave
Member
 
Registered: Feb 2005
Posts: 94

Rep: Reputation: 17
Quote:
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:
  1. You have to understand/debug/fix existing code that someone else wrote that uses threads.
  2. 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.

- Dave
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i want others to understand my handy-work rblampain Programming 3 10-31-2004 01:15 AM
question about subscribtion to threads lyceum LQ Suggestions & Feedback 8 07-09-2004 01:15 PM
I know there are lots of threads on this but I REALLY cant work it out Boggit Linux - Newbie 8 03-30-2004 01:53 PM
question about threads Jo_Nak Programming 1 08-23-2003 11:43 AM
maximum # of threads question amar Linux - General 5 06-04-2003 06:30 PM


All times are GMT -5. The time now is 01:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration