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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
03-07-2009, 09:20 AM
|
#1
|
LQ Newbie
Registered: Mar 2008
Posts: 5
Rep:
|
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?);
|
|
|
03-07-2009, 09:33 AM
|
#2
|
Member
Registered: Apr 2008
Location: Russia
Distribution: Fedora Core
Posts: 30
Rep:
|
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?);
|
|
|
03-07-2009, 11:52 AM
|
#3
|
Member
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811
Rep:
|
The following quotations contain trivial edits.
Quote:
Originally Posted by embesil
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
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. - 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.
- 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.
- Don't define an am_i_done_yet field. Don't define a child_result field either.
- 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.
- 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.
- 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. */
}
- 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.
- 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.
|
|
|
03-07-2009, 01:38 PM
|
#4
|
LQ Newbie
Registered: Mar 2008
Posts: 5
Original Poster
Rep:
|
@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
|
|
|
03-07-2009, 02:03 PM
|
#5
|
Member
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811
Rep:
|
The following quotation contains trivial edits.
Quote:
Originally Posted by embesil
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.
|
|
|
All times are GMT -5. The time now is 12:56 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|