Program Listing for File threadable.hpp

Return to documentation for file (/tmp/ws/src/ecl_core/ecl_threads/include/ecl/threads/threadable.hpp)

/*****************************************************************************
** Ifdefs
*****************************************************************************/

#ifndef ECL_THREADS_THREADABLE_POS_HPP_
#define ECL_THREADS_THREADABLE_POS_HPP_

/*****************************************************************************
** Platform Check
*****************************************************************************/

#include <ecl/config/ecl.hpp>
#if defined(ECL_IS_POSIX)

/*****************************************************************************
** Includes
*****************************************************************************/

#include <ecl/utilities/parameter.hpp>
#include <ecl/config/macros.hpp>
#include "thread.hpp"
#include "priority.hpp"

/*****************************************************************************
** Namespaces
*****************************************************************************/

namespace ecl {

/*****************************************************************************
** Interface [Threadable]
*****************************************************************************/
class ECL_PUBLIC Threadable {
public:
    virtual ~Threadable() {}

    bool start(const Priority &priority = DefaultPriority) {
        if ( isRunning() ) { return false; }
        isRunning(true); // This is not ideal, as it's not perfectly atomic (i.e. someone can query isRunning before this gets set).
        Thread thread(&Threadable::executeThreadFunction,*this, priority);
        return true;
    }
    Parameter<bool> isRunning;

protected:
    Threadable() : isRunning(false) {}

    virtual void runnable() = 0;

private:
    void executeThreadFunction() {
        runnable();
        isRunning(false);
    }
};

} // namespace ecl

#endif /* ECL_IS_POSIX */
#endif /* ECL_THREADS_THREADABLE_POS_HPP_ */