File: test_barrier.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 (109 lines) | stat: -rw-r--r-- 2,536 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
/*********************************************************************/
// 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
/*********************************************************************/

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

#include "../../src/libthreadar.hpp"

using namespace std;

class myfile: public libthreadar::thread
{
public:
    myfile(libthreadar::barrier* ptr, unsigned int index): syncr(ptr), ind(index)
    {
	if(ptr == nullptr)
	    throw libthreadar::THREADAR_BUG;
    };

    ~myfile()
    {
	cancel();
	join();
    }

protected:
    virtual void inherited_run() override
    {
	cout << "thread " << ind << " starting..." << endl;
	syncr->wait();
	cout << "thread " << ind << " passed the barrier!" << endl;
    }

private:
    libthreadar::barrier* syncr;
    unsigned int ind;
};

void release_deque(deque<myfile*> & dek)
{
    deque<myfile*>::iterator it = dek.begin();

    while(it != dek.end())
    {
	if(*it != nullptr)
	    delete *it;
	++it;
    }
    dek.clear();
}

int main()
{
    unsigned int num = 10;
    libthreadar::barrier bar(num);
    deque<myfile*> file;
    myfile* ptr = nullptr;

    cout << "barrier implementation: " << libthreadar::barrier::used_implementation() << endl;

    try
    {

	for(unsigned int i = 0 ; i < num ; ++i)
	{
	    ptr = new myfile(&bar, i);
	    if(ptr == nullptr)
		release_deque(file);
	    else
		file.push_back(ptr);
	}

	for(deque<myfile*>::iterator it = file.begin(); it != file.end(); ++it)
	    (*it)->run();

	for(deque<myfile*>::iterator it = file.begin(); it != file.end(); ++it)
	    (*it)->join();
    }
    catch(...)
    {
	release_deque(file);
	throw;
    }
    release_deque(file);

    return 0;
}