- 19.05 (released) ...
Thread library overview
- Creating and launching threads: the class THREAD (deferred)
- The class MUTEX
- The class SEMAPHORE
- The class CONDITION_VARIABLE
- Miscellaneous classes
- Controlling execution: THREAD_CONTROL
This is only a quick overview of the EiffelThread Library. The class reference for this library should give its complete interface.
Creating and launching threads: the class THREAD (deferred)
The class of the thread object you want to create should inherit the
Your thread is represented by a class which inherits from
-- define the deferred feature from THREAD.
end -- class MY_THREAD
Creating a thread is like creating an Eiffel object:
-- MY_THREAD inherits from THREAD and defines
-- the deferred procedure `execute'
On the Eiffel side, the procedure
On the C side, a C thread will be created and launched.
The class MUTEX
The implementation of the class
- Declaration of the mutex:
- Creation of mutex:
- Locking the mutex:
- Unlocking the mutex:
try_lock: if it is not locked yet, lock the mutex and return True, otherwise it returns False.
- Is my mutex initialized?
The class SEMAPHORE
- Declaration of the semaphore :
Creation of semaphore: initialize semaphore with nb_tokens, it requires nb_tokens > = 0
create my_sem.make (nb_tokens)
- Wait for a token:
- Give back a token:
try_wait, similar to try_lock from MUTEX, if a token is available, take it and return
True, otherwise return
The class CONDITION_VARIABLE
This class allows to use condition variables in Eiffel. An instance of class
- Declaration of the condition variable
- Wait for a signal (send by
signal). You need to use a mutex.
my_mutex must be locked by the calling thread so as
my_mutex and waits for the condition variable
my_mutex to receive a signal. As soon as it received a signal,
-- You must lock `my_mutex' before calling wait.
-- Here the critical code to execute when `my_cond' received a signal.
-- Unlock the mutex at the end of the critical section.
- Send a signal one thread blocked on the condition variable `my_cond'.
- Send a signal to all the threads blocked on the condition variable `my_cond'.
Controlling execution: THREAD_CONTROL
yield: the calling thread yields its execution in favor of an other thread of same priority.
join_all: the calling thread waits for all other threads to finished (all its children).
- A parent thread can wait for the termination of a child process through the feature
joinof class THREAD_CONTROL(inherited by THREAD):