Solved

Threads

Posted on 2003-11-11
11
431 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

20 Experts available now in Live!

Get 1:1 Help Now