Why is std::ref not working in this code?

I am getting garbage values in keyboard_interrupt and speech_interrupt x variables when they should have been passed references by std::ref.

Thanks
Chris

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <cstdlib>
#include <memory>
#include <thread>
#include <deque>
#include <iostream>
#include <vector>
#include <atomic>
#include <unistd.h>

#include <mutex>
#include <condition_variable>

//bool behavior_is_running=true;

class Robot;

typedef int(Robot::*m_behavior)(int);
typedef int(Robot::*m_listener)(int&);

struct cancelled_error {};
class thread_item;

class Behavior {
public:
Behavior(Robot &o, m_behavior b_have, std::string i_id): r(o), main_b(b_have),id(i_id) {};

void run_listeners();

int exec(int flag);

m_behavior main_b;
std::string id;
std::vector<thread_item> thread_list;
Robot &r;
};





struct State {
    std::shared_ptr<Behavior> current_behavior;
    int current_param;
};


class Stack {
    public:
    Stack() {};
    void empty(){
         state.clear();
    };
    
    State pop_out(){  //always pop from front
        State last=state.front();
        state.pop_front();
        return last;
    };
    
    void push_b(std::shared_ptr<Behavior> i_behavior, int iparam) {
        State temp;
             temp.current_behavior=i_behavior;
             temp.current_param=iparam;        
             state.push_back(temp);
    };
    
    
    void push_f(std::shared_ptr<Behavior> i_behavior, int iparam) {
        State temp;
              temp.current_behavior=i_behavior;
              temp.current_param=iparam;        
	      state.push_front(temp);
    };
    
    std::deque<State> state;
};



class Robot {
public:
    
    Robot() {  
    std::shared_ptr<Behavior> move=std::make_shared<Behavior>(*this,&Robot::move_and_scan,"M");
    run_stack->push_b(move,10);
    };

    int keyboard_interrupt(int &x){
      std::cout << "Keyboard running "<< x <<std::endl;  //PROBLEM here, x gives garbage values
      int c;
        for (;;) {
            if (x!=1)
                break;
           
        }
      std::cout << "Keyboard interrupt ending" << std::endl;
    }
    
    int speech_interrupt(int &x){
      std::cout << "Speech running "<< x <<  std::endl;  //PROBLEM HERE, x gives  garbage values
      int c;
        for (;;) {
            if (x!=1)
                break;
            
            }
            
      std::cout << "Speech interrupt ending" << std::endl;
    }
    
    int move_and_scan(int x){
        std::cout << "Move starting " << std::endl;
        
        usleep(x*1000000);
        
        std::cout << "Move stopping" << std::endl;
    }
    
    void run_state() {
           if (run_stack->state.size() > 0) {
                std::cout << "Run stack size is  " << run_stack->state.size() << std::endl;
                s=run_stack->pop_out();
                
                int outcome=s.current_behavior->exec(s.current_param);
           }
    }
           
           std::shared_ptr<Stack> run_stack=std::make_shared<Stack>();
           State s;
           
};


class thread_item {
    public:
        thread_item(Robot &o, m_listener m_list, std::string i_id) : r(o), id(i_id) {
            thr=std::thread(m_list,r,std::ref(l_flag));  //PROBLEM HERE, std::ref not passing the correct values
            std::cout << "Listener flag " << l_flag << std::endl;
        }
        
        std::string id;
        int l_flag=1;
        std::thread thr;
        Robot &r;
    
};


void Behavior::run_listeners() {
    thread_list.push_back(thread_item(r,&Robot::keyboard_interrupt,"KL"));
    thread_list.push_back(thread_item(r,&Robot::speech_interrupt,"SL"));
}


int Behavior::exec(int flag){
       run_listeners();
       
       std::cout << "Executing behavior "  << this->id << std::endl;  
       
       bool result = (r.*(this->main_b))(flag);
       
       for ( auto &l: thread_list ) {
           l.l_flag=0;
           l.thr.join();
      }
} 

int main(int argc, char** argv) {
    Robot create;
    
    //for (;;) {
        create.run_state();
    //}
    
    return 0;
}



The problem is not on line 137. It is on line 150/151: thread_item and hence l_flag will be temporarily created and then copied to the actual vector item.

In other words: When run_listeners() is done the passed object l_flag does not exists and the reference is invalid.

Consider using [smart] pointers.
Thank You Very Much.

I did solve the problem with smart pointers. I just didn't know how smart pointers solved the problem so I posted the nonworking code. This is the second time I encountered a problem like this and the first time, the fix was smart pointers as well.

Thanks for your time and interest
Chris
Topic archived. No new replies allowed.