1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
9 #ifndef anna_core_mt_Thread_hpp
10 #define anna_core_mt_Thread_hpp
15 #include <anna/core/RuntimeException.hpp>
16 #include <anna/core/mt/Mutex.hpp>
17 #include <anna/core/Allocator.hpp>
26 Recubrimiento para acceder a las funciones de threads de POSIX.
28 La funcionalidad de �ta clase slo estar�disponible en aplicaciones multithread aunque el
29 interfaz se mantiene constante de cualquier forma.
34 * Flags que puede ajuster el funcionamiento del thread.
38 enum { None = 0, Joinable = 1 };
44 Thread() : a_id((pthread_t) - 1), a_manager(NULL), a_flags(Flag::None) {;}
53 @return El identificador de este thread a nivel de sistema operativo.
55 pthread_t getId() const { return a_id; }
58 * Establece los flags que configuran el comportamiento de este thread.
59 * \param flag Una combinación de los valores definidos por Thread::Flags::_v.
60 * \warning Los flags deben establecerse antes de invocar al método #start.
62 void setFlags(const int flags) { a_flags = flags; }
65 * Devuelve el indicador que informa sobre si se podría aplicar el método #join sobre este thread
67 bool isJoinable() const { return (a_flags & Flag::Joinable) != 0; }
70 Devuelve el estado de ejecucion de �te thread.
71 \return El estado de ejecucion de �te thread \em true si est�en ejecucion o \em false en otro
74 bool isRunning() const { return (a_id != (pthread_t) - 1); }
78 Comienza la ejecucion de este threads siempre y cuando el thread no est�ejecutandose
81 Invoca al m�odo anna::Runnable::run del runnable recibido como par�etro.
83 @param runnable Instancia del objeto que va a ser ejecutado en el nuevo thread.
85 void start(Runnable& runnable) noexcept(false);
88 Introduce un nuevo punto de cancelacin. Esto slo sera necesario en caso de que el proceso
89 que implementa nuestro thread no haga ninguna llamada al sistema.
91 void testCancel() { pthread_testcancel(); }
94 Suspende la ejecución del thread que invoca a este método hasta que termine la ejecución
95 del thread sobre el que aplica.
103 cout << "Esperando a B." << endl;
105 cout << "B terminado." << endl;
109 catch (Exception& e) {
113 Con esto desde el thread C hemos lanzado los threads A y B; C quedar�bloqueado a la espera de que termine la ejecucion del
114 thread B y una vez hecho esto termina la ejecucion del thread A.
116 void join() noexcept(false);
119 Devuelve una cadena con la informacin referente a �te thread.
120 \return Una cadena con la informacin referente a �te thread.
122 virtual std::string asString() const ;
132 ThreadManager* a_manager;
135 Thread(const Thread& other);
137 static void* exec(void* arg);
139 friend class Allocator <Thread>;
140 friend class ThreadManager;