12 #include <ecl/config/ecl.hpp>
13 #if defined(ECL_IS_POSIX)
20 #include "../../include/ecl/threads/thread_pos.hpp"
21 #include <sys/resource.h>
38 start(
function, priority, stack_size);
41 Error Thread::start(VoidFunction
function,
const Priority &priority,
const long &stack_size)
44 ecl_debug_throw(StandardException(LOC,BusyError,
"The thread has already been started."));
45 return Error(BusyError);
49 initialise(stack_size);
51 thread_task =
new threads::ThreadTask< NullaryFreeFunction<void> >(nullary_function_object, priority);
52 int result = pthread_create(&(this->thread_handle), &(this->attrs), threads::ThreadTask< NullaryFreeFunction<void> >::EntryPoint, thread_task);
53 pthread_attr_destroy(&attrs);
58 return threads::handlePthreadCreateError(result);
60 return Error(NoError);
76 if ( has_started && !join_requested ) {
77 pthread_detach(thread_handle);
80 void Thread::cancel() {
81 int result = pthread_cancel(thread_handle);
85 if ( thread_task != NULL ) {
95 join_requested =
true;
96 if( thread_task != NULL ) {
97 int result = pthread_join( thread_handle, 0 );
98 ecl_assert_throw( result == 0, threads::throwPthreadJoinException(LOC,result));
102 void Thread::initialise(
const long &stack_size) {
104 pthread_attr_init( &attrs );
108 pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
110 pthread_attr_setinheritsched(&attrs,PTHREAD_INHERIT_SCHED);
119 pthread_attr_setdetachstate(&attrs,PTHREAD_CREATE_JOINABLE);
124 if ( stack_size != DefaultStackSize ) {
125 int result = pthread_attr_setstacksize(&attrs,stack_size);
126 ecl_assert_throw( result == 0, StandardException(LOC,ConfigurationError,
"Specified stack size was less than PTHREAD_STACK_MIN or wasn't a multiple of the page size."));