Solved

Threads

Posted on 2003-11-11
11
436 Views
Last Modified: 2010-04-22
Hi!!

I have a few doubts regarding threads in linux.

How can i retrieve the status of a thread in linux? Using pthread_join( ), calling thread waits for the termination of the specified thread. I want to know if there is any function/way that my calling thread doesn't have to wait  and comes to know that a particular thread has not terminated and is still active.

Threads terminate as soon as my main thread exits. Can i make my multithreaded application run even after main() exits and let other threads continue (without using pthread_join)?

Thanx
-Anu
0
Comment
Question by:linux_newb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 9728315
>How can i retrieve the status of a thread in linux?
what do you mean by status ? exit status can be collected by pthread_join() ... look at the prototype carefully

>I want to know if there is any function/way that my calling thread doesn't have to wait  and comes to know that
>a particular thread has not terminated and is still active.
No

>Threads terminate as soon as my main thread exits. Can i make my multithreaded application run even after main
>() exits and let other threads continue (without using pthread_join)?
If main() finishes before the threads it has created, and exits with pthread_exit(), only then the other threads will continue to execute. Otherwise, they will be automatically terminated when main() finishes. I would recommend you to use pthread_exit() to exit from all threads...especially main().

you can find the entire API and details here
http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#PthreadsAPI
0
 

Author Comment

by:linux_newb
ID: 9728913
By status i meant if the thread is still active or not terminated. But you already told, that isn't possible without using pthread_join().

Is there any way of  returning a structure from a thread? I am able to return an integer from thread but not a structure.

Thanks for the link. It helped.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9728941
from pthread_exit man page
The pthread_exit subroutine terminates the calling thread safely, and stores a
termination status for any thread that may join the calling thread. The
termination status is always a void pointer; it can reference any kind of data.

so create a struct e.g. strucy my * mystruct; (this is a pointer, you need to malloc memory for the struct)
fill it up and and return it as
pthread_exit( (void *) mystruct );
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:linux_newb
ID: 9729137
I did the same thing.
Problem is, since mystruct is created & allocated memory inside thread, the memory is deallocated as soon as thread terminates. And main() which join() this thread prints junk values.
I'll post the code:

struct structure{
      int a;
      char c;
};

void* thread_function (void* arg){
      struct structure *obj;
      obj = new struct structure;
      obj->a = 88;
      obj->c = 'c';
      
        cout<<"Exiting thread"<<endl;
      pthread_exit((void*)obj);
}  

int main(){
      struct structure *object;
  pthread_t thread_id;

  pthread_create(&thread_id,NULL,&thread_function,NULL);

  pthread_join(thread_id,(void**)object);
  cout << "object.a = "<< object->a<<endl;
  cout << "object.c = "<< object->c<<endl;
 
  return 0;
}
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9729192
>you need to malloc memory for the struct
this way it will be on the heap and not on the stack.... you can free the memory later in the main() after join
0
 

Author Comment

by:linux_newb
ID: 9729386
Well! i used to think new too allocates memory from heap.
I tried using malloc, it too does same thing.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9729612
sorry my mistake ... I did not see the code .... just read "the memory is deallocated as soon as thread terminates." and posted ... well that is not the problem ... problem is

 struct structure *obj;
 pthread_join(thread_id,(void**)object);
 cout << "object.a = "<< object->a<<endl;
 cout << "object.c = "<< object->c<<endl;

notice the levels of indirection !!! object is given the "address" of obj variable and not the address of the struct that it points to
0
 

Author Comment

by:linux_newb
ID: 9736492
I get your point. But still don't know how to get the address of structure. Get confused when it comes to lots of indirections.
0
 

Author Comment

by:linux_newb
ID: 9736549
Well! i tried this and it worked. But still not sure if it correct way.

  pthread_join(thread_id,(void**)object);

  unsigned int addr = (unsigned int) object;
  unsigned int addr2 = *(unsigned int*) addr;
  object = (struct structure*)addr2;
  cout << "object.a = "<< object->a<<endl;
  cout << "object.c = "<< object->c<<endl;

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9737674
correct way would be

struct structure{
    int a;
    char c;
};

struct structure * a;

a = new struct structure;

pthread_join(thread_id,(void**)&a);

if you feel uncomfortable with this, try

struct structure{
    int a;
    char c;
};

struct structure * a;
struct structure ** b;

a = new struct structure;

b = &a;

pthread_join(thread_id,(void**)b);
0
 

Author Comment

by:linux_newb
ID: 9761668
Thanx for the help!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question