Solved

Threads

Posted on 2003-11-11
11
432 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to line 1 45
init script for deployment on linux machine 5 52
install command in linux 1 44
What is the timeframe of this cron job? 2 131
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now