thread-test.cpp

M. Goerlich, 09/10/2015 05:11 PM

Download (3.86 KB)

 
1
/* 
2
* File:   ConfigurableGrabber.cpp
3
* Author: mgoerlic
4
*
5
* Created on 19. August 2015, 09:57
6
*/
7

    
8
#include <cstdlib>
9

    
10
#include <rsc/misc/SignalWaiter.h>
11

    
12
#include <rsb/depthsensors2/VideoMode.h>
13

    
14
#include <map>
15
#include <vector>
16
#include <deque>
17
#include <sstream>
18

    
19
#include <boost/program_options.hpp>
20
#include <boost/algorithm/string/split.hpp>
21
#include <boost/algorithm/string/classification.hpp>
22
#include <boost/date_time/posix_time/posix_time.hpp>
23

    
24
#include <rsb/depthsensors2/Factory.h>
25
#include <rst/tracking/TrackedPostures3DFloat.pb.h>
26
#include <rsb/depthsensors2/BoostSignalToRSBConnector.h>
27

    
28
using namespace std;
29
using namespace rsb;
30
using namespace boost;
31
using namespace rsc::threading;
32

    
33
boost::shared_ptr<rsc::threading::SynchronizedQueue<std::pair<boost::posix_time::ptime, boost::shared_ptr<string> > > > dataQueue1;
34
boost::shared_ptr< rsb::Informer<string> > informer1;
35

    
36
void publishThread1() {
37
    try {
38
        while(true) {
39
            std::pair<boost::posix_time::ptime, boost::shared_ptr<string> > data = dataQueue1->pop();
40

    
41
            if(!data.second) {
42
                break;
43
            }
44

    
45
            rsb::EventPtr event = informer1->createEvent();
46

    
47
            event->mutableMetaData().setCreateTime(data.first);
48
            event->setData(data.second);
49

    
50
            informer1->publish(event);
51
        }
52
    } catch(rsc::threading::InterruptedException ex) {
53
                    
54
    }
55
}
56

    
57
boost::shared_ptr<rsc::threading::SynchronizedQueue<std::pair<boost::posix_time::ptime, boost::shared_ptr<string> > > > dataQueue2(
58
    new rsc::threading::SynchronizedQueue<std::pair<boost::posix_time::ptime, boost::shared_ptr<string> > >(1));
59
boost::shared_ptr< rsb::Informer<string> > informer2;
60

    
61
void publishThread2() {
62
    try {
63
        while(true) {
64
            std::pair<boost::posix_time::ptime, boost::shared_ptr<string> > data = dataQueue2->pop();
65
            
66
            if(!data.second) {
67
                break;
68
            }
69

    
70
            rsb::EventPtr event = informer2->createEvent();
71

    
72
            event->mutableMetaData().setCreateTime(data.first);
73
            event->setData(data.second);
74

    
75
            informer2->publish(event);
76
        }
77
    } catch(rsc::threading::InterruptedException ex) {
78
                    
79
    }
80
}
81

    
82
int main(int argc, char* argv[]) {
83
    dataQueue1 = boost::shared_ptr<rsc::threading::SynchronizedQueue<std::pair<boost::posix_time::ptime, boost::shared_ptr<string> > > >(
84
        new rsc::threading::SynchronizedQueue<std::pair<boost::posix_time::ptime, boost::shared_ptr<string> > >());
85
    dataQueue2 = boost::shared_ptr<rsc::threading::SynchronizedQueue<std::pair<boost::posix_time::ptime, boost::shared_ptr<string> > > >(
86
        new rsc::threading::SynchronizedQueue<std::pair<boost::posix_time::ptime, boost::shared_ptr<string> > >());
87
    
88
    informer1 = boost::shared_ptr< rsb::Informer<string> >(rsb::getFactory().createInformer<string>(Scope("/fun1")));
89
    informer2 = boost::shared_ptr< rsb::Informer<string> >(rsb::getFactory().createInformer<string>(Scope("/fun2")));
90

    
91
    thread t1(publishThread1);
92
    thread t2(publishThread2);
93

    
94
    for(int i=0; i<10000; i++) {
95
        boost::shared_ptr<string> ptr1(new string("fun"));
96
        dataQueue1->push(pair< boost::posix_time::ptime, boost::shared_ptr<string> >(boost::posix_time::ptime(), ptr1));
97
        boost::shared_ptr<string> ptr2(new string("fun"));
98
        dataQueue2->push(pair< boost::posix_time::ptime, boost::shared_ptr<string> >(boost::posix_time::ptime(), ptr2));
99
    }
100
    
101
    dataQueue1->push(pair< boost::posix_time::ptime, boost::shared_ptr<string> >(boost::posix_time::ptime(), boost::shared_ptr<string>()));
102
    dataQueue2->push(pair< boost::posix_time::ptime, boost::shared_ptr<string> >(boost::posix_time::ptime(), boost::shared_ptr<string>()));
103
    
104
    t1.join();
105
    t2.join();
106

    
107
    return 0;
108
}