LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-07-2009, 08:20 AM   #1
embesil
LQ Newbie
 
Registered: Mar 2008
Posts: 5

Rep: Reputation: 0
POSIX Threads


Hello there,

I try to use pthread_create function, but my start routine should return a value, so how can I get that return?

for instance how would I create a thread with that function below and how would I call the pthread_create function?


int the_routine ( int number)
{
....
return total;
}



pthread_create( &thread1, NULL, the_routine?, the_parameter?);
 
Old 03-07-2009, 08:33 AM   #2
alex1983-0112
Member
 
Registered: Apr 2008
Location: Russia
Distribution: Fedora Core
Posts: 30

Rep: Reputation: 16
In my opinion you should to do next:

int the_routine ( int number)
{
....
return total;
}

void my_thread(int param)
{
int res = the_routine(param);
///check res value here
}

pthread_create( &thread1, NULL, my_thread, the_parameter?);
 
Old 03-07-2009, 10:52 AM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
The following quotations contain trivial edits.
Quote:
Originally Posted by embesil View Post
Hello there,

I try to use pthread_create function, but my start routine should return a value, so how can I get that return?

for instance how would I create a thread with that function below and how would I call the pthread_create function?
Code:
int the_routine ( int number)
{
      ....
      return total;
}

pthread_create( &thread1, NULL, the_routine?, the_parameter?);
Quote:
Originally Posted by alex1983-0112 View Post
In my opinion you should to do next:
Code:
int the_routine ( int number)
{
....
return total;
}

void my_thread(int param)
{
   int res = the_routine(param);
   ///check res value here
}

pthread_create( &thread1, NULL, my_thread, the_parameter?);
alex1983-0112's answer makes a good point, but it would be well to explore this question:
Quote:
If I have a parent thread which fires off child threads and needs to know their status upon completion so it can coordinate the start of other threads, how does the parent thread receive that status from them?
This is a fairly routine task in designing code using POSIX threads, which provides a fairly easy way to take care of it. Three fairly easy ways, in fact.
  1. The first way is used if the parent thread doesn't want to wait for the child thread to finish, because the parent thread has other things to do, and just wants to check back periodically to see whether the child thread is finished, without waiting for it.

    First, please note that the final parameter to pthread_create() is not of type int; it's of type void *. The same is true of the parameter to the child main function! The parent thread can pass NULL if it wants, but often a programmer will call malloc() in the parent thread to create an instance of some struct that he has defined, and then fill that with values he wants the child thread to use. In this struct, he can even declare one of the fields an int, call it something like am_i_done_yet, and set it to zero. He can declare another field whatever type of value he wants the child thread to return and call it something like child_result. He doesn't need to set it to any particular value. When the child thread is done, it will modify first the child_result field and second the am_i_done_yet field (since there is no guarantee that he won't be time-sliced between setting the two values).

    Then every once in a while the parent thread can check the am_i_done_yet field to see whether the child thread is done. If so, it can then just pick up the returned value from the child_result field.

    Once the parent thread knows that the child thread is done, it can call pthread_join(), confident that it won't have to wait any significant amount of time. It should definitely do so, because otherwise the child thread (even though it's no longer running) retains certain system resources for no good reason.

    If the parent thread doesn't want to call pthread_join() when it knows that the child thread is done, it can create the thread detached in the first place.

    In any event, the parent thread should free() the parameter block when it's done with it, or memory leak will occur.

  2. The second method is used when the parent thread simply wants to wait until the child thread is finished, rather than checking periodically to see whether the child thread is finished, because the parent thread simply has no other work to do or coordination of child threads to perform.

    The second method is like the first, with these changes.

    1. Don't define an am_i_done_yet field. Don't define a child_result field either.

    2. Please note that the type of a child thread's main function is not int, but void *. The child thread's main function can return NULL, or it can malloc() an instance of some struct which it fills with the desired result before finishing.

    3. When it's done, either the child thread passes the pointer to this struct (or NULL) to pthread_exit(), or uses that pointer as the parameter of a return statement from the main function in the child thread.

    4. The parent process, meanwhile, instead of checking am_i_done_yet periodically, simply calls pthread_join() when it's ready to wait for the result. The second parameter to pthread_join() is of type void **. So you use it like this:
      Code:
      void *child_result;
      ...
      if(pthread_join(child_thread_identifier,&child_result)!=0)
      {
        /* Put code to recover from any pthread_join() error here. */
      }
    5. The parent then picks up whatever values it wants from the result fields, and then free()s that area, so that memory leak does not occur. As in the first way, it also needs to free() the original parameter struct it passed to the child thread.

  3. The third way is a blending of the first two. Like the second way, it has the parent thread waiting for the child to finish. But it uses a child_result field to pass back the results, rather than doing a malloc() in the child for a results struct and an additional free() in the parent for this struct. Note, however, that the parent still has to free() the original parameter struct() passed to the child when it started the child.
Hope this helps.
 
Old 03-07-2009, 12:38 PM   #4
embesil
LQ Newbie
 
Registered: Mar 2008
Posts: 5

Original Poster
Rep: Reputation: 0
@wje_lq: well, first of all I should say that I appreciated your answer

I have another question. I have a function gets 2 parameters. So, can I just write
pthread_create(&tid,&attr, function, parameter1, parameter2);

thank you
 
Old 03-07-2009, 01:03 PM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
The following quotation contains trivial edits.
Quote:
Originally Posted by embesil View Post
can I just write
Code:
pthread_create(&tid,&attr, function, parameter1, parameter2);
No, unfortunately. Put 'em in a struct. Use malloc(), and don't forget to use free() in your parent thread when you're all done with that child thread. Or you can do the free() in your child thread if you want, if the parent thread has no more use for that struct.
 
  


Reply



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
POSIX Threads mannahazarika Programming 4 07-16-2006 11:00 PM
POSIX threads policy and priority arunj Programming 1 06-13-2006 06:05 PM
POSIX threads - pthread_cond_timedwait iklinux Programming 5 09-26-2005 10:33 AM
About POSIX threads Ephracis Programming 1 12-03-2004 06:33 AM
I have a problem with POSIX Threads. AndreasA Programming 2 08-11-2003 02:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:19 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration