[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.programming.threads

who owns pthread ?

gee_jay_

7/22/2004 11:39:00 AM

Hi,

I have some questions about pthreads:
1. If i have several processes which create each a pthread,
do they generate each a unique pthread id ?
2. If 1==yes, is there a way to find out the process who
owns a specific pthread id ?
Suppose process A creates a pthread b. Can process C see that process A
owns pthread b?

Thanks in advance,

Gerry
10 Answers

Joe Seigh

7/22/2004 11:54:00 AM

0



Gerry Jacobs wrote:
>
> Hi,
>
> I have some questions about pthreads:
> 1. If i have several processes which create each a pthread,
> do they generate each a unique pthread id ?
> 2. If 1==yes, is there a way to find out the process who
> owns a specific pthread id ?
> Suppose process A creates a pthread b. Can process C see that process A
> owns pthread b?
>

Unique within the process that creates the thread.

Each thread knows what its id is from pthread_self() and what its pid is
from getpid(). The process that creates the threads knows the thread
ids from the threads it creates (but they're only meaningful if the
threads are not detached) and its pid from getpid(). You'll have
to track pid and thread id together.

Joe Seigh

Alexander Terekhov

7/22/2004 12:00:00 PM

0


Gerry Jacobs wrote:
>
> Hi,
>
> I have some questions about pthreads:
> 1. If i have several processes which create each a pthread,
> do they generate each a unique pthread id ?

pthread_t objects are process local. It can go boom if you try
to share and access them across processes.

regards,
alexander.

Alexander Terekhov

7/22/2004 12:20:00 PM

0


Joe Seigh wrote:
[...]
> You'll have to track pid and thread id together.

And compare pids first. ;-) Seriously, pthread_t is ought to be no
"stronger" than a pointer... which has an "indeterminate value" (can't
even read it without triggering undefined behavior) when it isn't a
process locally valid pointer. Oder?

regards,
alexander.

David Eng

7/22/2004 4:33:00 PM

0


"Gerry Jacobs" <gee_jay_@yahoo.com> wrote in message
news:ce2b54c8.0407220339.4e48ecb9@posting.google.com...
> Hi,
>
> I have some questions about pthreads:
> 1. If i have several processes which create each a pthread,
> do they generate each a unique pthread id ?
> 2. If 1==yes, is there a way to find out the process who
> owns a specific pthread id ?
> Suppose process A creates a pthread b. Can process C see that process A
> owns pthread b?
>
> Thanks in advance,
>
> Gerry


David Eng

7/22/2004 4:36:00 PM

0


"Gerry Jacobs" <gee_jay_@yahoo.com> wrote in message
news:ce2b54c8.0407220339.4e48ecb9@posting.google.com...
> Hi,
>
> I have some questions about pthreads:
> 1. If i have several processes which create each a pthread,
> do they generate each a unique pthread id ?
> 2. If 1==yes, is there a way to find out the process who
> owns a specific pthread id ?
> Suppose process A creates a pthread b. Can process C see that process A
> owns pthread b?
>
> Thanks in advance,
>
> Gerry


Loic Domaigne

7/23/2004 10:21:00 AM

0

Hi Gerry,

> I have some questions about pthreads:
> 1. If i have several processes which create each a pthread,
> do they generate each a unique pthread id ?

Depends what Do you mean by "unique". If you mean "process-wide", yes.
If you mean "system-wide", no... As Joe and Alexander told you.

> 2. If 1==yes, is there a way to find out the process who
> owns a specific pthread id ?
> Suppose process A creates a pthread b. Can process C see that process A
> owns pthread b?

Why would you need such an information? "Normally", it is impossible
from outside to see that a process is multi-threaded!!

[
OK, some OSes can tell you that the process is MT, how many threads
there is etc. But they won't necessary give you the corresponding thread
id, since that information is "local to the process".
]


Cheers,
Loic.

David Schwartz

7/23/2004 7:38:00 PM

0


"Alexander Terekhov" <terekhov@web.de> wrote in message
news:40FFAC2E.55E8AD88@web.de...

> Gerry Jacobs wrote:

>> I have some questions about pthreads:
>> 1. If i have several processes which create each a pthread,
>> do they generate each a unique pthread id ?

That's a meaningless question since there is no way to compare a thread
ID in one process with a thread ID in another.

> pthread_t objects are process local. It can go boom if you try
> to share and access them across processes.

There is no way to do so. The only things you can legally do with a
pthread_t is pass one or a pointer to one to a function that takes on,
allocate one, take the address of one, or copy the value of one to another.
You cannot compare them, print them, or otherwise mess with them except
through the supplied functions that take them. There is no way to get a
pthread_t from one process to another, so how could you compare a pthread_t
from one process with one from another?

DS


Alexander Terekhov

7/24/2004 2:57:00 PM

0


David Schwartz wrote:
[...]
> You cannot compare them,

pthread_equal().

> print them, or otherwise mess with them except
> through the supplied functions that take them. There is no way to get a
> pthread_t from one process to another,

You can save pthread_t object in a shared memory.

> so how could you compare a pthread_t
> from one process with one from another?

// pid and tid are always saved together
if (shared_memory->pid == getpid()) {
// Can compare
if (pthread_equal(shared_memory->tid, pthread_self()) ) {
// ...
}
// ...
}
else {
// Can't compare/touch shared_memory->tid, except reassigning
// ...
shared_memory->pid = getpid();
shared_memory->tid = pthread_self(); // OK
// ...
}

Oder?

regards,
alexander.

David Schwartz

7/26/2004 2:03:00 AM

0


"Alexander Terekhov" <terekhov@web.de> wrote in message
news:410278CE.13B1E63F@web.de...

> David Schwartz wrote:
> [...]
>> You cannot compare them,

> pthread_equal().

As I said, you can pass them to a function that takes one (or a pointer
to one) as an argument.

>> print them, or otherwise mess with them except
>> through the supplied functions that take them. There is no way to get a
>> pthread_t from one process to another,
>
> You can save pthread_t object in a shared memory.

Sure, and you can retrieve it from shared memory in the same process.
But you can't access it from another process because it may have embedded
pointers to memory that are not valid in that other process.

>> so how could you compare a
>> pthread_t
>> from one process with one from another?
>
> // pid and tid are always saved together
> if (shared_memory->pid == getpid()) {
> // Can compare
> if (pthread_equal(shared_memory->tid, pthread_self()) ) {
> // ...
> }
> // ...
> }
> else {
> // Can't compare/touch shared_memory->tid, except reassigning
> // ...
> shared_memory->pid = getpid();
> shared_memory->tid = pthread_self(); // OK
> // ...
> }
>
> Oder?

That's not legal. The 'pthread_t' may have an embedded pointer in it
that's not valid inside the other process' memory space. There is no
guarantee that the 'pthread_t' is self-contained. In fact, it may be *only*
a pointer.

DS


Alexander Terekhov

7/26/2004 7:30:00 AM

0


David Schwartz wrote:
[...]
> That's not legal. The 'pthread_t' may have an embedded pointer in it
> that's not valid inside the other process' memory space. There is no
> guarantee that the 'pthread_t' is self-contained. In fact, it may be *only*
> a pointer.

Yes. But it (i.e. what I said and showed) is legal. Read the C std.
BTW, pthread_t is a non-array POD.

regards,
alexander.