SourcePro® API Reference Guide

 
List of all members | Public Member Functions | Static Public Member Functions | Related Functions
RWThreadId Class Reference

A wrapper for platform-specific thread IDs. More...

#include <rw/sync/RWThreadId.h>

Public Member Functions

 RWThreadId ()
 
 RWThreadId (RWStaticCtor)
 
 RWThreadId (const RWThreadIdRep &threadIdRep)
 
 RWThreadId (const RWThreadId &second)
 
 ~RWThreadId ()
 
void clear ()
 
RWThreadIdRepgetRep () const
 
unsigned hash () const
 
bool isValid () const
 
 operator RWThreadIdRep & () const
 
bool operator!= (const RWThreadId &second) const
 
RWThreadIdoperator= (const RWThreadId &second)
 
bool operator== (const RWThreadId &second) const
 

Static Public Member Functions

static bool isSelf (const RWThreadId &id)
 
static RWThreadId self ()
 

Related Functions

(Note that these are not member functions.)

unsigned rwThreadHash (const RWThreadId &id)
 
RWThreadId rwThreadId ()
 
typedef pthread_t RWThreadIdRep
 
typedef thread_t RWThreadIdRep
 
typedef unsigned RWThreadIdRep
 

Detailed Description

RWThreadId is a wrapper for platform-specific thread IDs. An RWThreadId represents the identification given by the underlying thread API to a specific thread of execution. The native thread IDs are used to identify and control these threads in the native thread API, but are not necessarily related to any Threads Module objects. You can get an instance of RWThreadId directly from a runnable object using the RWRunnableHandle::threadId() member function, or you can call the global function rwThreadId() to get an RWThreadId associated with the current thread. A thread ID is valid only while the separate thread of execution exists.

The global rwThread() function may be used to determine the threaded runnable object, if any, associated with the current thread of execution. Use the rwThreadHash() function to retrieve an integer associated with a thread id for output or debugging purposes. Do not attempt to stream an RWThreadId instance directly, as the native thread ID type that this class represents may be a structure and not a simple scalar.

Example
#include <rw/thread/RWThreadFunction.h>
#include <rw/sync/RWThreadId.h>
void whoamiReally()
{
std::cout << "My thread ID is " << rwThreadHash(myID) << std::endl;
}
int main()
{
RWThread t1, t2, t3;
(t1 = RWThreadFunction::make(whoamiReally)).start();
(t2 = RWThreadFunction::make(whoamiReally)).start();
(t3 = RWThreadFunction::make(whoamiReally)).start();
t1.join();
t2.join();
t3.join();
return 0;
}

OUTPUT:

My thread ID is 4
My thread ID is 5
My thread ID is 6
See also
rwThreadId(), rwThreadHash(), rwThread(), RWRunnableHandle

Constructor & Destructor Documentation

RWThreadId::RWThreadId ( )
inline

Creates an RWThreadId object not associated with any thread.

RWThreadId::RWThreadId ( RWStaticCtor  )
inline

Constructs a static instance, but does no direct initialization. Static instances are zero initialized, resulting in an RWThreadId with an invalid state.

Note
This constructor must be used only for static instances. Use of this constructor with an automatically or dynamically allocated instance may produce unpredictable behavior.
RWThreadId::RWThreadId ( const RWThreadIdRep threadIdRep)
inline

Creates an RWThreadId object directly from a native thread ID.

RWThreadId::RWThreadId ( const RWThreadId second)
inline

Copy constructor.

RWThreadId::~RWThreadId ( )
inline

Destructor.

Member Function Documentation

void RWThreadId::clear ( void  )
inline

Clears the native thread ID so it won't match other native IDs.

RWThreadIdRep * RWThreadId::getRep ( ) const
inline

Returns the address of the native thread ID member.

unsigned RWThreadId::hash ( ) const
inline

Returns a hash of the thread ID.

bool RWThreadId::isSelf ( const RWThreadId id)
inlinestatic

Returns true if id matches the thread ID of the currently running thread; otherwise, returns false.

bool RWThreadId::isValid ( void  ) const
inline

Returns true if this thread ID was constructed or assigned from a native thread instance, otherwise false.

RWThreadId::operator RWThreadIdRep & ( ) const
inline

Returns a reference to the native thread ID.

bool RWThreadId::operator!= ( const RWThreadId second) const
inline

Compares native thread IDs for inequality.

RWThreadId & RWThreadId::operator= ( const RWThreadId second)
inline

Assignment operator.

bool RWThreadId::operator== ( const RWThreadId second) const
inline

Compares native thread IDs for equality.

RWThreadId RWThreadId::self ( )
inlinestatic

Returns an RWThreadId for the current thread.

Friends And Related Function Documentation

unsigned rwThreadHash ( const RWThreadId id)
related

Use this function when an integer is required to identify a particular thread for hashing and trace/debug output purposes. The thread id value stored inside an RWThreadId instance should not be used directly, since some environments use a structure to represent a thread id.

See also
RWThreadId
RWThreadId rwThreadId ( )
related

The rwThreadId() function returns an RWThreadId instance containing the thread id value associated with the calling thread. This id value is assigned by the underlying threads system. You may query for the thread id of any thread, regardless of its method of creation.

See also
RWThreadId, rwThreadHash()
typedef pthread_t RWThreadIdRep
related

The internal thread id representation.

Condition:
This type is used for build configurations based on POSIX threads.
typedef thread_t RWThreadIdRep
related

The internal thread id representation.

Condition:
This type is used for build configurations based on Solaris threads.
typedef unsigned RWThreadIdRep
related

The internal thread id representation.

Condition:
This type is used for build configurations based on Win32 threads.

Copyright © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.