this is an example of use of class libthreadar::fast_tampon and libthreadar::exception_base and derivated classes
#include <libthreadar/libthreadar.hpp>
{
public:
const unsigned int block_size = 100;
my_thread(): inter(10, block_size) {}
set_fd(int val)
{
fd = val;
};
void show()
{
char *ptr;
unsigned int size;
do
{
inter.fetch(ptr, size);
ptr[block_size - 1] = '\0';
cout << ptr << endl;
inter.fetch_recycle(ptr);
}
while(size > 0);
try
{
}
{
std::cout <<
"Error met while reading file: " << e.
get_message(
": ") << std::endl;
}
}
protected:
{
char *ptr;
unsigned int size, read;
do
{
inter.get_block_to_feed(ptr, size);
read = std::read(fd, ptr, size);
if(read >= 0)
inter.feed(ptr, read);
else
{
inter.feed(ptr, 0);
}
}
while(read > 0);
}
private:
int fd;
};
int main(int argc, char *argv[])
{
if(argc < 2)
{
std::cout << "usage: " << argv[0] << " <filename>" << std::endl;
return 1;
}
int fd = open(argv[1]);
if(fd >= 0)
{
my_thread t1;
t1.set_fd(fd);
t1.run();
t1.show();
}
}
std::string get_message(const std::string &sep) const
concatenated messages and use the given separator between messages
Exception used to report webdar internal bugs.
Exception used to report out or range value or argument.
Class fast_tampon provides asynchronous communication between two threads.
Class thread is a pure virtual class, that implements thread creation and operations.
bool is_running() const
checks whether a separated thread is running the inherited_run() method of this object
virtual void inherited_run()=0
action to be performed in the separated thread (implementation is expected in inherited classes)
void join() const
the caller will be suspended until the current object's thread ends