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
    while(true){
        auto conn = server.accept();
        room.push_back(conn);
 
        std::thread([&room](Sock& conn){
            while(true){
                auto data = conn.recv();
                if(conn.state() == Sock::STATE::ERR)
                    break;
 
                std::stringstream print;
                print << conn.ipv4() << ":" << conn.port() << " " << data;
 
                std::cout << print.str() << std::endl;
 
                for(auto& guest : room){
                    if(guest == conn) continue;
                    guest.send(print.str());
                }
            }
 
            room.erase(
                std::remove(room.begin(),
                            room.end(),
                            conn),
                room.end());
 
            conn.close();
        }, conn).detach();;
cs


여기 보면 std::thread 호출 할 때 conn의 레퍼런스 넘겨주는데

바깥에 while 루프 한번 돌면 conn이 덮어씌어져서 thread 안에 있는 conn에서 에러나야 정상아님??

왜 잘 작동하는거죠?;;