File: stack_sizer.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 (114 lines) | stat: -rw-r--r-- 2,686 bytes parent folder | download
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
/*********************************************************************/
// libthreadar - is a library providing several C++ classes to work with threads
// Copyright (C) 2014-2024 Denis Corbin
//
// This file is part of libthreadar package
//
//  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
/*********************************************************************/

extern "C"
{
#include <unistd.h>
#include <fcntl.h>
}

#include <iostream>
#include "../../src/libthreadar.hpp"

size_t get_default_stack_size();
void read_proc_self_maps(const std::string & context);


class my_thread: public libthreadar::thread_signal
{
protected:

    virtual void inherited_run() override
    {
	std::cout << "default thread size reported get_stacksize() from within thread: " << get_default_stack_size() << std::endl;
	read_proc_self_maps("in a thread");
    };
};

void read_proc_self_maps(const std::string & context)
{
    static const unsigned int buf_size = 1024*1024;

    int fd = open("/proc/self/maps", O_RDONLY);
    char buf[buf_size];
    int lu = 0;

    std::cout << " --------------------- " << context << "   [START]" << std::endl;
    if(fd < 0)
	return;
    try
    {
	do
	{
	    lu = read(fd, buf, buf_size);
	    if(lu > 0)
	    {
		buf[lu] = '\0';
		std::cout << buf << std::flush;
	    }
	}
	while(lu > 0);
    }
    catch(...)
    {
	close(fd);
	throw;
    }
    close(fd);
    std::cout << " --------------------- " << context << "   [END]" << std::endl;
}

size_t get_default_stack_size()
{
    size_t ret = 0;
    pthread_attr_t attr;

    switch(pthread_attr_init(&attr))
    {
    case 0:
	break;
    default:
	return 0;
    }

    pthread_attr_getstacksize(&attr,
			      &ret);

    pthread_attr_destroy(&attr);

    return ret;
}

int main()
{
    my_thread t1;

    std::cout << "default stack size is: " << get_default_stack_size() << std::endl;
    read_proc_self_maps("in main thread");

    t1.run();
    t1.join();
    t1.set_stack_size(4*1024*1024);
    t1.run();
    t1.join();
}