mthread 0.5
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.
bool resume ()
 Resumes a paused or sleeping Thread.
bool sleep (unsigned long t)
 Puts the Thread to sleep for a certain number of seconds.
bool sleep_micro (unsigned long t)
 Puts the Thread to sleep for a certain number of microseconds.
bool sleep_milli (unsigned long t)
 Puts the Thread to sleep for a certain number of milliseconds.

Protected Member Functions

virtual bool loop ()
 The Thread's main loop.

Protected Attributes

bool kill_flag
 Kill flag.

Private Member Functions

bool call ()
 Determines if the function is active and runs through a loop if appropriate.

Private Attributes

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

Friends

class ThreadList
void loop ()

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:
This function is called automatically by a ThreadList object. It is important to note that once a Thread has completed its execution (this function returns false) it will automatically destroy itself and MUST NOT be used again. A new instance must first be created.
Thread::Mode Thread::get_mode ( ) const

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 otherwise.
bool Thread::loop ( ) [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.

See also:
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 ThreadList, EventHandler, and SwitchInput.

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 otherwise.
bool Thread::resume ( )

Resumes a paused or sleeping Thread.

Returns:
true on success, false otherwise.
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 for which the Thread is to sleep.
Returns:
true on success, false otherwise.
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 for which the Thread is to sleep.
Returns:
true on success, false otherwise.
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 for which the Thread is to sleep.
Returns:
true on success, false otherwise.

Member Data Documentation

bool Thread::kill_flag [protected]

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 cancelled by resetting it to false.


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