Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Threads

Posted on 2003-11-11
11
Medium Priority
?
438 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 200 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

730 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