cgv
|
base class for system command input pipe or named pipe threads including a queue of data blocks and a separate thread More...
#include <pipe_thread.h>
Public Member Functions | |
queued_output_thread (bool is_binary=true, unsigned _ms_to_wait=20) | |
construct queued output thread from flag, whether to use binary mode and wait time in ms used when queue is empty | |
void | run () |
connect to child process and continuously write queue content to pipe; if empty wait in intervals of ms_to_wait miliseconds or close pipe and terminate if done() had been called | |
bool | has_connection () const |
returns true as soon as child process has connected to pipe | |
bool | send_block (const char *data, size_t count) |
if done() had not been called, insert a data block into the queue; can fail if done() or out of memory | |
size_t | get_nr_blocks () const |
returns the number of blocks in the queue of not yet written data | |
size_t | get_nr_bytes () const |
returns the number of bytes in the queue of not yet written data, what is more time consuming than get_nr_blocks() | |
void | done () |
call this to announce the all data has been sent | |
![]() | |
thread () | |
create the thread | |
virtual | ~thread () |
standard destructor (a running thread will be killed) | |
void | start (bool _delete_after_termination=false) |
start the implemented run() method (asynchronly) and destruct the thread object | |
void | stop () |
try to stop the thread execution via indicating a stop request. | |
void | kill () |
kill a running thread | |
void | wait_for_completion () |
the thread is interpreted as a slave thread and started from another master thread. | |
bool | is_running () |
return true if thread is running | |
bool | have_stop_request () |
check if there is a stop request | |
thread_id_type | get_id () const |
return id of this thread | |
Protected Member Functions | |
virtual bool | connect_to_child_process ()=0 |
to be implemented in derived classes | |
virtual void | write_block_to_pipe (const char *data, size_t count)=0 |
to be implemented in derived classes | |
virtual void | close ()=0 |
to be implemented in derived classes | |
![]() | |
void | execute () |
executes the run method | |
Protected Attributes | |
bool | is_binary |
whether binary mode should be used | |
bool | connected = false |
flag that tells whether the pipe has been connected to from the other side | |
cgv::os::mutex | m |
mutex used to protect access to blocks | |
std::deque< std::pair< char *, size_t > > | blocks |
deque used to queue the data blocks that should be written to the pipe by the thread | |
unsigned | ms_to_wait |
time in miliseconds to wait while queue is empty | |
![]() | |
void * | thread_ptr |
bool | stop_request |
bool | running |
bool | delete_after_termination |
Additional Inherited Members | |
![]() | |
static void | wait_for_signal (condition_mutex &cm) |
sleep till the signal from the given condition_mutex is sent, lock the mutex first and unlock after waiting | |
static void | wait_for_signal_with_lock (condition_mutex &cm) |
prefered approach to wait for signal and implemented as { cm.lock(); wait_for_signal(cm); cm.unlock(); } | |
static bool | wait_for_signal_or_timeout (condition_mutex &cm, unsigned millisec) |
sleep till the signal from the given condition_mutex is sent or the timeout is reached, lock the mutex first and unlock after waiting | |
static bool | wait_for_signal_or_timeout_with_lock (condition_mutex &cm, unsigned millisec) |
prefered approach to wait for signal or the timeout is reached and implemented as { cm.lock(); wait_for_signal_or_timeout(cm,millisec); cm.unlock(); } | |
static void | wait (unsigned millisec) |
wait the given number of milliseconds | |
static thread_id_type | get_current_thread_id () |
return the id of the currently executed thread | |
![]() | |
static void * | execute_s (void *args) |
base class for system command input pipe or named pipe threads including a queue of data blocks and a separate thread
Definition at line 19 of file pipe_thread.h.
cgv::os::queued_output_thread::queued_output_thread | ( | bool | is_binary = true , |
unsigned | _ms_to_wait = 20 |
||
) |
construct queued output thread from flag, whether to use binary mode and wait time in ms used when queue is empty
Definition at line 11 of file pipe_thread.cxx.
References is_binary, and ms_to_wait.
|
protectedpure virtual |
to be implemented in derived classes
Implemented in cgv::os::named_pipe_output_thread, and cgv::os::pipe_output_thread.
Referenced by run().
|
protectedpure virtual |
to be implemented in derived classes
Implemented in cgv::os::named_pipe_output_thread, and cgv::os::pipe_output_thread.
Referenced by run().
void cgv::os::queued_output_thread::done | ( | ) |
call this to announce the all data has been sent
Definition at line 101 of file pipe_thread.cxx.
References cgv::os::mutex::lock(), m, and cgv::os::mutex::unlock().
size_t cgv::os::queued_output_thread::get_nr_blocks | ( | ) | const |
returns the number of blocks in the queue of not yet written data
Definition at line 84 of file pipe_thread.cxx.
References blocks, cgv::os::mutex::lock(), m, and cgv::os::mutex::unlock().
size_t cgv::os::queued_output_thread::get_nr_bytes | ( | ) | const |
returns the number of bytes in the queue of not yet written data, what is more time consuming than get_nr_blocks()
Definition at line 92 of file pipe_thread.cxx.
References blocks, cgv::os::mutex::lock(), m, and cgv::os::mutex::unlock().
bool cgv::os::queued_output_thread::has_connection | ( | ) | const |
returns true as soon as child process has connected to pipe
Definition at line 52 of file pipe_thread.cxx.
References connected, cgv::os::mutex::lock(), m, and cgv::os::mutex::unlock().
|
virtual |
connect to child process and continuously write queue content to pipe; if empty wait in intervals of ms_to_wait
miliseconds or close pipe and terminate if done() had been called
Implements cgv::os::thread.
Definition at line 16 of file pipe_thread.cxx.
References blocks, close(), connect_to_child_process(), connected, cgv::os::thread::have_stop_request(), cgv::os::mutex::lock(), m, ms_to_wait, cgv::os::mutex::unlock(), cgv::os::thread::wait(), and write_block_to_pipe().
bool cgv::os::queued_output_thread::send_block | ( | const char * | data, |
size_t | count | ||
) |
if done() had not been called, insert a data block into the queue; can fail if done() or out of memory
Definition at line 60 of file pipe_thread.cxx.
References blocks, cgv::os::mutex::lock(), m, and cgv::os::mutex::unlock().
|
protectedpure virtual |
to be implemented in derived classes
Implemented in cgv::os::named_pipe_output_thread, and cgv::os::pipe_output_thread.
Referenced by run().
|
protected |
deque used to queue the data blocks that should be written to the pipe by the thread
Definition at line 31 of file pipe_thread.h.
Referenced by get_nr_blocks(), get_nr_bytes(), run(), and send_block().
|
protected |
flag that tells whether the pipe has been connected to from the other side
Definition at line 27 of file pipe_thread.h.
Referenced by has_connection(), and run().
|
protected |
whether binary mode should be used
Definition at line 25 of file pipe_thread.h.
Referenced by cgv::os::named_pipe_output_thread::connect_to_child_process(), cgv::os::pipe_output_thread::connect_to_child_process(), and queued_output_thread().
|
mutableprotected |
mutex used to protect access to blocks
Definition at line 29 of file pipe_thread.h.
Referenced by done(), get_nr_blocks(), get_nr_bytes(), has_connection(), run(), and send_block().
|
protected |
time in miliseconds to wait while queue is empty
Definition at line 33 of file pipe_thread.h.
Referenced by queued_output_thread(), and run().