mthread  0.5.1
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | Friends
Thread Class Reference

Provides thread functionality. More...

#include <mthread.h>

Inheritance diagram for Thread:
EventHandler SwitchInput ThreadList

List of all members.

Public Types

enum  Mode {
  run_mode, pause_mode, sleep_mode, sleep_milli_mode,
  sleep_micro_mode, kill_mode
}
 Various running modes for a Thread. More...

Public Member Functions

 Thread ()
 Constructor.
virtual ~Thread ()
 Destructor.
Mode get_mode () const
 Returns the running mode for the Thread.
bool kill (bool force=false)
 Kills a Thread.
bool pause ()
 Pauses a Thread. This function will cause a Thread to pause until its resume() function is called. This function will cancel any sleep timer currently in effect.
bool resume ()
 Resumes a paused or sleeping Thread.
bool sleep (unsigned long t)
 Puts the Thread to sleep for a certain number of seconds. If already running, the thread's loop() function will be allowed to finish, but will not be called again until the timeout has expired, or the resume() or kill() function has been called.
bool sleep_micro (unsigned long t)
 Puts the Thread to sleep for a certain number of microseconds. If already running, the thread's loop() function will be allowed to finish, but will not be called again until the timeout has expired, or the resume() or kill() function has been called.
bool sleep_milli (unsigned long t)
 Puts the Thread to sleep for a certain number of milliseconds. If already running, the thread's loop() function will be allowed to finish, but will not be called again until the timeout has expired, or the resume() or kill() function has been called.

Protected Member Functions

virtual bool loop ()
 The Thread's main loop. This function is to be overridden. It takes the place of the loop function found in most Arduino programs. A single loop() should run as quickly as possible, as it will hold up other Thread objects while it is executing.

Protected Attributes

bool kill_flag
 Kill flag. This variable should be checked at the beginning of every loop() function. If it is true, the Thread has been requested to be killed, and the loop() function should behave accordingly. The request can be denied by resetting it to false.

Private Member Functions

bool call ()
 Determines if the function is active and runs through a loop if appropriate. This function is called automatically by a ThreadList.

Private Attributes

unsigned long stop_time
 The time the thread was stopped at.
unsigned long wait_time
 The amount of the the thread is to wait for.
Mode mode
 The thread's running mode (can be read through the get_mode() function).

Friends

class ThreadList
void loop (void)

Detailed Description

Provides thread functionality.


Member Enumeration Documentation

Various running modes for a Thread.

Enumerator:
run_mode 

Thread is running.

pause_mode 

Thread is paused.

sleep_mode 

Thread is sleeping (for seconds).

sleep_milli_mode 

Thread is sleeping (for milliseconds).

sleep_micro_mode 

Thread is sleeping (for microseconds).

kill_mode 

Thread is to be killed on next call.


Member Function Documentation

bool Thread::call ( ) [private]

Determines if the function is active and runs through a loop if appropriate. This function is called automatically by a ThreadList.

Returns:
true if the Thread needs to be called again, false if the Thread has completed execution.
Note:
It is important to note that once a Thread has completed its execution it will automatically destroy itself and MUST NOT be used again. A new instance must first be created.

Returns the running mode for the Thread.

Returns:
The running mode.
bool Thread::kill ( bool  force = false)

Kills a Thread.

Parameters:
forceIf true, the Thread will be killed immediately on the next call without running any more loops, if false, the Thread will have to opportunity to terminate cleanly but will be resumed if sleeping or paused.
Note:
If the force parameter is set to false, the Thread could possibly ignore or cancel the request, however this is still the preferred way of calling the kill() function.
Returns:
true on success, false on failure.
bool Thread::loop ( void  ) [protected, virtual]

The Thread's main loop. This function is to be overridden. It takes the place of the loop function found in most Arduino programs. A single loop() should run as quickly as possible, as it will hold up other Thread objects while it is executing.

Note:
At the beginning of each loop, the function should check the kill_flag.
Returns:
true if the loop needs to be called again, false if the Thread has completed executing (at which point it will be destroyed).

Reimplemented in SwitchInput, EventHandler, and ThreadList.

bool Thread::pause ( )

Pauses a Thread. This function will cause a Thread to pause until its resume() function is called. This function will cancel any sleep timer currently in effect.

Returns:
true on success, false on failure.
bool Thread::resume ( )

Resumes a paused or sleeping Thread.

Returns:
true on success, false on failure.
bool Thread::sleep ( unsigned long  t)

Puts the Thread to sleep for a certain number of seconds. If already running, the thread's loop() function will be allowed to finish, but will not be called again until the timeout has expired, or the resume() or kill() function has been called.

Parameters:
tThe number of seconds the Thread is to sleep for.
Returns:
true on success, false on failure.
bool Thread::sleep_micro ( unsigned long  t)

Puts the Thread to sleep for a certain number of microseconds. If already running, the thread's loop() function will be allowed to finish, but will not be called again until the timeout has expired, or the resume() or kill() function has been called.

Parameters:
tThe number of microseconds the Thread is to sleep for
Returns:
true on success, false on failure.
bool Thread::sleep_milli ( unsigned long  t)

Puts the Thread to sleep for a certain number of milliseconds. If already running, the thread's loop() function will be allowed to finish, but will not be called again until the timeout has expired, or the resume() or kill() function has been called.

Parameters:
tThe number of milliseconds the Thread is to sleep for.
Returns:
true on success, false on failure.

The documentation for this class was generated from the following files:
 All Classes Files Functions Variables Enumerations Enumerator Defines