Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

Threads

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
linux_newb
Asked:
linux_newb
  • 6
  • 5
1 Solution
 
sunnycoderCommented:
>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
 
linux_newbAuthor Commented:
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
 
sunnycoderCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
linux_newbAuthor Commented:
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
 
sunnycoderCommented:
>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
 
linux_newbAuthor Commented:
Well! i used to think new too allocates memory from heap.
I tried using malloc, it too does same thing.
0
 
sunnycoderCommented:
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
 
linux_newbAuthor Commented:
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
 
linux_newbAuthor Commented:
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
 
sunnycoderCommented:
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
 
linux_newbAuthor Commented:
Thanx for the help!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now