Keyple C++  0.9.0
Keyple, an Open Source SDK for Ticketing
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
keyple::common::Thread Class Referenceabstract

#include <Thread.h>

Public Member Functions

 Thread ()
 
 Thread (const std::string &name)
 
virtual ~Thread ()
 
void start ()
 
int join ()
 
int detach ()
 
std::thread::id selfId ()
 
std::thread * self ()
 
void setDaemon (bool on)
 
void interrupt ()
 
bool isInterrupted () const
 
virtual void * run ()=0
 
std::string getName ()
 
void setThread (std::thread *t)
 

Static Public Member Functions

static void runThread (void *arg)
 
static void sleep (long millis)
 
static std::thread::id currentThreadId ()
 

Public Attributes

int mAlive
 

Constructor & Destructor Documentation

◆ Thread() [1/2]

keyple::common::Thread::Thread ( )
inline

Constructor

Allocates a new Thread object. This constructor has the same effect as Thread (null, null, gname), where gname is a newly generated name. Automatically generated names are of the form "Thread-"+n, where n is an integer.

◆ Thread() [2/2]

keyple::common::Thread::Thread ( const std::string &  name)
inline

Constructor

Allocates a new Thread object. This constructor has the same effect as Thread (null, null, name).

Parameters
namethe name of the new thread

◆ ~Thread()

virtual keyple::common::Thread::~Thread ( )
inlinevirtual

Destructor

Member Function Documentation

◆ currentThreadId()

static std::thread::id keyple::common::Thread::currentThreadId ( )
inlinestatic

◆ detach()

int keyple::common::Thread::detach ( )
inline

This is a utility method that detaches a thread when the caller doesn’t want to wait for the thread to complete. If the thread is running and not detached, pthread_detach() is called and the thread is flagged as detached if the call is successful.

◆ getName()

std::string keyple::common::Thread::getName ( )
inline

◆ interrupt()

void keyple::common::Thread::interrupt ( )
inline

Interrupts this thread.

Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException.

If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

If none of the previous conditions hold then this thread's interrupt status will be set.

Interrupting a thread that is not alive need not have any effect.

Exceptions
SecurityExceptionif the current thread cannot modify this thread

◆ isInterrupted()

bool keyple::common::Thread::isInterrupted ( ) const
inline

◆ join()

int keyple::common::Thread::join ( )
inline

By default Pthreads are joinable. meaning you can wait for them to complete with a call to pthread_join(). The Thread class join method checks to see if the thread is running, then calls this function to wait for the thread to complete. If the call is successful the thread is marked as detached since pthread_join() automatically detatches a thread.

◆ run()

virtual void* keyple::common::Thread::run ( )
pure virtual

◆ runThread()

static void keyple::common::Thread::runThread ( void *  arg)
inlinestatic

In the call to pthread_create() the last argument is a void pointer to a data structure which will be passed to the runThread() function when it is called. Since the input argument to the runThread() is the Thread class this pointer, we can cast it to a Thread pointer then use it to call the Thread::run() method. Due to polymorphism, the Thread subclass run() method will be called to carry out the thread’s action.

◆ self()

std::thread* keyple::common::Thread::self ( )
inline

◆ selfId()

std::thread::id keyple::common::Thread::selfId ( )
inline

This is another utility method that returns the thread ID for display or logging purposes.

◆ setDaemon()

void keyple::common::Thread::setDaemon ( bool  on)
inline

◆ setThread()

void keyple::common::Thread::setThread ( std::thread *  t)
inline

◆ sleep()

static void keyple::common::Thread::sleep ( long  millis)
inlinestatic

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.

Parameters
millisthe length of time to sleep in milliseconds
Exceptions
IllegalArgumentExceptionif the value of millis is negative
InterruptedExceptionif any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.static void sleep(long millis) throw(InterruptedException)

◆ start()

void keyple::common::Thread::start ( )
inline

Causes this thread to begin execution.

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

Exceptions
IllegalThreadStateExceptionif the thread was already started.

Member Data Documentation

◆ mAlive

int keyple::common::Thread::mAlive

The documentation for this class was generated from the following file: