File: fast_tampon_example.cpp

package info (click to toggle)
libthreadar 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,072 kB
  • sloc: sh: 4,379; cpp: 2,499; makefile: 78
file content (124 lines) | stat: -rw-r--r-- 3,451 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*********************************************************************/
// libthreadar - is a library providing several C++ classes to work with threads
// Copyright (C) 2014-2020 Denis Corbin
//
// This file is part of libthreadar
//
//  libthreadar is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  libhtreadar is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public License
//  along with libthreadar.  If not, see <http://www.gnu.org/licenses/>
//
//----
//  to contact the author: dar.linux@free.fr
/*********************************************************************/

#include <libthreadar/libthreadar.hpp>


    // let's define a new thread class
    // to illustrate how to use libthreadar::fast_tampon template

class my_thread: public libthreadar::thread
{
public:
    const unsigned int block_size = 100;

	// we initialize inter as a file of 10 block
	// each of size 100 chars (T = char)
    my_thread(): inter(10, block_size) {}

	// providing a filedescriptor to read data from
    set_fd(int val)
    {
	if(is_running())
	    throw libthreadar::exception_bug(__FILE__,__LINE__);
	fd = val;
    };

	// the caller will be the fetcher of object inter
    void show()
    {
	char *ptr;
	unsigned int size;

	do
	{
	    inter.fetch(ptr, size); // we grab the next block of data
	    ptr[block_size - 1] = '\0'; // have a null terminated string
	    cout << ptr << endl;
	    inter.fetch_recycle(ptr); // as we have finished using it we recycle it into inter
	}
	while(size > 0);
	    // by convention, returning a zero sized block
	    // means that the subthread will not provide any
	    // more data

	    // so we wait for its termination
	try
	{
	    join();
	}
	catch(libthreadar::exception_range & e)
	{
	    std::cout << "Error met while reading file: " << e.get_message(": ") << std::endl;
	}
    }

protected:
	// the subthread will be the feeder of object inter
    virtual void inherited_run() override
    {
	char *ptr;
	unsigned int size, read;

	    // this sub-thread will read the data from fd up to eof or error
	do
	{
	    inter.get_block_to_feed(ptr, size); // obtaining a block of data
	    read = std::read(fd, ptr, size);    // using it
	    if(read >= 0)
		inter.feed(ptr, read);          // pushing it back to inter
	    else
	    {
		inter.feed(ptr, 0);
		throw libthreadar::exception_range(strerror(errno));
		    // this exception will be progragated to the parent thread
		    // when calling join() in the show() method above
	    }
	}
	while(read > 0);
    }

private:
    libthreadar::fast_tampon<char> inter;
    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); // setup the future thread
	t1.run();      // spawn the thread that will read data from fd and pass it to inter
	t1.show();     // extract data from inter in the current (parent) thread
    }
}