Thread joining

Hi, all:

Is there is a way that a thread (when it exits) can tell if there is another
thread joining on it (pthread_join ())? The reason is that if the thread is
created with PTHREAD_CREATE_JOINABLE, I want it to release system resource
when exiting if there is no thread joining on it.

Thanks.

Xuedong

Xuedong Chen wrote in message <9lscp5$30j$1@inn.qnx.com>…

Hi, all:

Is there is a way that a thread (when it exits) can tell if there is
another
thread joining on it (pthread_join ())? The reason is that if the thread is
created with PTHREAD_CREATE_JOINABLE, I want it to release system resource
when exiting if there is no thread joining on it.

Thanks.

Xuedong

Why not doing something like this ?

struct my_data_t
{
int joined;
int status;
int pid;
int prio;
int sched;
struct pthread_attr_t attr;
struct sched_param param;
// …
void* (release_fn )( void );
}

inline int my_pthread_create( … )
{
my_data_t dat;
memset( &dat, 0, sizeof( my_data_t ) );
dat.joined = ( … == PTHREAD_CREATE_JOINABLE );
return pthread_create( &dat.pid, &dat.attr, run, (void*)&dat );
}

inline int my_pthread_join( … )
{
return pthread_join( … );
}

void* run( void* info )
{
my_data_t dat;
memcpy( &dat, info, sizeof( my_data_t ) );
// …

dat.release_fn( dat, … );
}

int my_pthread_release_resources( my_data_t data, … ) { }

you can alias alll pthread_* to my_pthread_* with inlined functions to do
what you want.

Another way is to do that via a Thread class, if you prefer a C++ approach.

BTW, when you pthread_join() the second param is a pointer to a function
which
is called when you want to release. But I never used it indirectly via
pthread_create,
if I never join my thread implicitly always explicitely…

Sincerely,
Fred.

Thanks for the detailed information. I will give it a try.

But I was thinking that if there is an OS call which can solve my probem,
since the kernel should have the information that a thread is joined by
other thread or not, right?

Xuedong
“Fred” <fprog@nowhere.users.sourceforge.net> wrote in message
news:9lsev2$463$1@inn.qnx.com

Xuedong Chen wrote in message <9lscp5$30j$> 1@inn.qnx.com> >…
Hi, all:

Is there is a way that a thread (when it exits) can tell if there is
another
thread joining on it (pthread_join ())? The reason is that if the thread
is
created with PTHREAD_CREATE_JOINABLE, I want it to release system
resource
when exiting if there is no thread joining on it.

Thanks.

Xuedong


Why not doing something like this ?

struct my_data_t
{
int joined;
int status;
int pid;
int prio;
int sched;
struct pthread_attr_t attr;
struct sched_param param;
// …
void* (release_fn )( void );
}

inline int my_pthread_create( … )
{
my_data_t dat;
memset( &dat, 0, sizeof( my_data_t ) );
dat.joined = ( … == PTHREAD_CREATE_JOINABLE );
return pthread_create( &dat.pid, &dat.attr, run, (void*)&dat );
}

inline int my_pthread_join( … )
{
return pthread_join( … );
}

void* run( void* info )
{
my_data_t dat;
memcpy( &dat, info, sizeof( my_data_t ) );
// …

dat.release_fn( dat, … );
}

int my_pthread_release_resources( my_data_t data, … ) { }

you can alias alll pthread_* to my_pthread_* with inlined functions to do
what you want.

Another way is to do that via a Thread class, if you prefer a C++
approach.

BTW, when you pthread_join() the second param is a pointer to a function
which
is called when you want to release. But I never used it indirectly via
pthread_create,
if I never join my thread implicitly always explicitely…

Sincerely,
Fred.

Probably stored in pthread_attr_t

perhaps, I’m not sure how if this is changed ‘externally’…
It’s quite a black box. Also it might not be portable to other POSIX systems
!

My trick was since you don’t know what’s inside, just build a thin black box
around the existing one. I don’t say it’s efficient or anything.

Maybe someone as a clue ?!
Probably from one of the QNX Kernel Thread functions…

Sincerely,
Fred.


Xuedong Chen wrote in message <9lttdj$2do$1@inn.qnx.com>…

Thanks for the detailed information. I will give it a try.

But I was thinking that if there is an OS call which can solve my probem,
since the kernel should have the information that a thread is joined by
other thread or not, right?

Xuedong
“Fred” <fprog@nowhere.users.sourceforge.net> wrote in message
news:9lsev2$463$> 1@inn.qnx.com> …

Xuedong Chen wrote in message <9lscp5$30j$> 1@inn.qnx.com> >…
Hi, all:

Is there is a way that a thread (when it exits) can tell if there is
another
thread joining on it (pthread_join ())? The reason is that if the thread
is
created with PTHREAD_CREATE_JOINABLE, I want it to release system
resource
when exiting if there is no thread joining on it.

Thanks.

Xuedong


Why not doing something like this ?

struct my_data_t
{
int joined;
int status;
int pid;
int prio;
int sched;
struct pthread_attr_t attr;
struct sched_param param;
// …
void* (release_fn )( void );
}

inline int my_pthread_create( … )
{
my_data_t dat;
memset( &dat, 0, sizeof( my_data_t ) );
dat.joined = ( … == PTHREAD_CREATE_JOINABLE );
return pthread_create( &dat.pid, &dat.attr, run, (void*)&dat );
}

inline int my_pthread_join( … )
{
return pthread_join( … );
}

void* run( void* info )
{
my_data_t dat;
memcpy( &dat, info, sizeof( my_data_t ) );
// …

dat.release_fn( dat, … );
}

int my_pthread_release_resources( my_data_t data, … ) { }

you can alias alll pthread_* to my_pthread_* with inlined functions to
do
what you want.

Another way is to do that via a Thread class, if you prefer a C++
approach.

BTW, when you pthread_join() the second param is a pointer to a function
which
is called when you want to release. But I never used it indirectly via
pthread_create,
if I never join my thread implicitly always explicitely…

Sincerely,
Fred.
\

Xuedong Chen (Xuedong.Chen@IGT.com) wrote:
: Hi, all:

: Is there is a way that a thread (when it exits) can tell if there is another
: thread joining on it (pthread_join ())? The reason is that if the thread is
: created with PTHREAD_CREATE_JOINABLE, I want it to release system resource
: when exiting if there is no thread joining on it.

Why do you want to do this? There are two problems with this – a race
condition and an implementation issue.

The race condition stems from the fact that another thread may be just about
to join your thread – due to very short windows, it may or may not join your
thread.

The second issue stems from implementation – how are you actually going to
release the resources of this thread? It certainly can’t join() itself :slight_smile:

Generally, if you’re going to join() the thread, make it joinable, else don’t
make it joinable…

Cheers,
-RK

Robert Krten, PARSE Software Devices; email my initials at parse dot com
Consulting, Systems Architecture / Design, Drivers, Training, QNX 4 & Neutrino
Check out our new QNX 4 and Neutrino (QRTP) books at http://www.parse.com/
Wanted PDP-8/9/10/11/12 Systems/documentation/spare parts! Will trade books!