Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Threads and C++

1 view
Skip to first unread message

Srinivasa T.N.

unread,
Jan 31, 2002, 12:19:02 AM1/31/02
to
Hi All,
I want to determine whether a thread is running or not? (This is
because, I have encapsulated thread in a calss. When the object is
destoryed, the thread can also exit, i.e., by putting pthread_cancel
and pthread_join in destructor of the object. But, even when the
object is there, the corresponding thread may not be existing. So I
want to determine whether the thread is there or not)..
I am using Redhat Linux 7.1 on i386 machine..

Regards,

Seenu.

David Schwartz

unread,
Jan 31, 2002, 2:04:00 AM1/31/02
to
"Srinivasa T.N." wrote:

> I want to determine whether a thread is running or not?

So keep track of it.

> (This is
> because, I have encapsulated thread in a calss. When the object is
> destoryed, the thread can also exit, i.e., by putting pthread_cancel
> and pthread_join in destructor of the object. But, even when the
> object is there, the corresponding thread may not be existing. So I
> want to determine whether the thread is there or not)..

Okay, so when you create a thread, set a flag and in your 'end the
thread' function, clear a flag.

DS

Alexander Terekhov

unread,
Jan 31, 2002, 11:06:13 AM1/31/02
to
"Srinivasa T.N." wrote:
>
> Hi All,
> I want to determine whether a thread is running or not? (This is
> because, I have encapsulated thread in a calss. When the object is
> destoryed, the thread can also exit, i.e., by putting pthread_cancel
> and pthread_join in destructor of the object. But, even when the
> object is there, the corresponding thread may not be existing.

For a joinable thread, its thread object exists
until you call pthread_join. For a detached thread,
implementation does finalization/reclamation of
thread object for you automatically on thread
termination. You might want to have a look at
http://www.terekhov.de/mythread.c These are
reference-counted threads which perform detach
automatically and allow multiple/concurrent
join operations (*non-mandatory*), similar to
Java's threading model. I still have no time
to finish the C++ front-end, so perhaps someone
could submit a patch? ;-)

> So I
> want to determine whether the thread is there or not)..
> I am using Redhat Linux 7.1 on i386 machine..

Beware that unless you have an implementation which
performs C++-stack-unwinding on thread cancellation
and thread exit (pthread_exit), invoking these operations
*COULD RESULT IN UNDEFINED (BROKEN) C++ BEHAVIOR*.

regards,
alexander.

josh

unread,
Feb 1, 2002, 4:39:17 AM2/1/02
to
On 30 Jan 2002 21:19:02 -0800, see...@cdotb.ernet.in (Srinivasa T.N.) wrote:
> Hi All,
> I want to determine whether a thread is running or not?
Is that a question or a statement or what?

> (This is
> because, I have encapsulated thread in a calss. When the object is
> destoryed, the thread can also exit, i.e., by putting pthread_cancel
> and pthread_join in destructor of the object. But, even when the
> object is there, the corresponding thread may not be existing. So I
> want to determine whether the thread is there or not)..

I don't think you know what you're doing. OK, how exactly have you "encapsulated
thread in a class"? What does that mean, can you post a code fragment showing
that? Who runs your destructors? Which thread? Is it a separate thread?

Srinivasa T.N.

unread,
Feb 2, 2002, 2:48:47 AM2/2/02
to
josh <jo...@xtreme.net> wrote in message news:<1107_1012556357@js3owsj2-2j2-2j>...

> I don't think you know what you're doing. OK, how exactly have you "encapsulated
> thread in a class"? What does that mean, can you post a code fragment showing
> that? Who runs your destructors? Which thread? Is it a separate thread?

I am a lament in C++.. I took the concept from

http://www.geocities.com/SiliconValley/Heights/6038/dthreads.html

According to Thread class mentioned there, if I have something like

class MyThread : public Thread
{
public:
MyThread ();
~MyThread ();

Start (void *Arg) ;
}

In MyThread, I can start the thread, but there is no way to exit the
thread. Even if I call pthread_exit, the destructor for the thread
"~MyThread () is not called. So, what should I do??

Regards,
Seenu.

David Schwartz

unread,
Feb 2, 2002, 3:22:48 AM2/2/02
to
"Srinivasa T.N." wrote:

> class MyThread : public Thread
> {
> public:
> MyThread ();
> ~MyThread ();
>
> Start (void *Arg) ;
> }
>
> In MyThread, I can start the thread, but there is no way to exit the
> thread. Even if I call pthread_exit, the destructor for the thread
> "~MyThread () is not called. So, what should I do??

Definitely not that! What exactly are you trying to do? Who do you want
to call the destructor and what do you want to happen when the
destructor is called?

You definitely don't want it such that anyone can call the destructor
at any time and it magically causes the thread to stop. That doesn't
make any sense at all.

DS

josh

unread,
Feb 3, 2002, 6:44:47 PM2/3/02
to
On 1 Feb 2002 23:48:47 -0800, see...@cdotb.ernet.in (Srinivasa T.N.) wrote:
> josh <jo...@xtreme.net> wrote in message news:<1107_1012556357@js3owsj2-2j2-
2j>...
>
> > I don't think you know what you're doing. OK, how exactly have you
"encapsulated
> > thread in a class"? What does that mean, can you post a code fragment
showing
> > that? Who runs your destructors? Which thread? Is it a separate thread?
>
> I am a lament in C++.. I took the concept from
That's OK, everyone is at some point <g>.

> According to Thread class mentioned there, if I have something like
>
> class MyThread : public Thread
> {
> public:
> MyThread ();
> ~MyThread ();
>
> Start (void *Arg) ;
> }
>
> In MyThread, I can start the thread,

Well, something should call the Start on your object. It is important to know
what this something is. And what's more, this excerpt is insufficient, since I
don't see the thread function there. Normally, your thread function would simply
end, or exit, at some point. *That's* "the way to exit thread. Your threaded
function simply ends, boom, that's it. You don't end threads from the outside
(usually.) OK, I looked your page up. Your thread function is Exectute, you need
to supply an implementation of it. Once it's finished, it returns to Run, and
Run then returns to EntryPoint. Once that's over, your thread exits.

but there is no way to exit the
> thread. Even if I call pthread_exit, the destructor for the thread
> "~MyThread () is not called. So, what should I do??

In order to achieve what? Why do you need to call your destructor from within
the thread? Your destructor, or better say, your object's lifetime, got nothing
to do with the lifetime of the thread. It'll be destroyed when it goes out of
scope or explicitly deleted, depending on how you allocate it initially. It's
just a pack of data and therefore is not implicitly related to the thread (the
OS entitiy) in any way.


>
> Regards,
> Seenu.


Daniele Cruciani

unread,
Feb 5, 2002, 10:59:53 AM2/5/02
to
On Sat, 02 Feb 2002 08:48:47 +0100, Srinivasa T.N. wrote:

> josh <jo...@xtreme.net> wrote in message
> news:<1107_1012556357@js3owsj2-2j2-2j>...
>
>> I don't think you know what you're doing. OK, how exactly have you
>> "encapsulated thread in a class"? What does that mean, can you post a
>> code fragment showing that? Who runs your destructors? Which thread? Is
>> it a separate thread?
>
> I am a lament in C++.. I took the concept from
>
> http://www.geocities.com/SiliconValley/Heights/6038/dthreads.html

I'd a look, good. I've a silly question: how many thread can i run i that
class? I want to make 2-3 threads running with access to its private
members (having access to other shared memory too, of course) one of which
is a controller thread. I want to use template, namespace et all ...

(I was programming in C, and I like C, it is sad to say that C++ is actually
better in real life, too much better ... it's sad)

>
> According to Thread class mentioned there, if I have something like
>
> class MyThread : public Thread
> {
> public:
> MyThread ();
> ~MyThread ();
>
> Start (void *Arg) ;
> }
>
> In MyThread, I can start the thread, but there is no way to exit the
> thread. Even if I call pthread_exit, the destructor for the thread
> "~MyThread () is not called. So, what should I do??

I can guess that you can push destructor as clean up handler, but I think
you don't really want it. This kind of things are usefull in more
pure object oriented language, where anything is a class, but actually
anyway there is a main object instantiated by something that eventually
call the destructor ... I think you already get the right answers
from who are more experienced.


regard,
Daniele.

Rufus V. Smith

unread,
Feb 5, 2002, 4:26:18 PM2/5/02
to

> > According to Thread class mentioned there, if I have something like
> >
> > class MyThread : public Thread
> > {
> > public:
> > MyThread ();
> > ~MyThread ();
> >
> > Start (void *Arg) ;
> > }
> >
> > In MyThread, I can start the thread, but there is no way to exit the
> > thread. Even if I call pthread_exit, the destructor for the thread
> > "~MyThread () is not called. So, what should I do??
>
> I can guess that you can push destructor as clean up handler, but I think
> you don't really want it. This kind of things are usefull in more
> pure object oriented language, where anything is a class, but actually
> anyway there is a main object instantiated by something that eventually
> call the destructor ... I think you already get the right answers
> from who are more experienced.
>
>
> regard,
> Daniele.

I think whoever constructed the thread object should call the destructor.

0 new messages