在C ++中使用std :: thread的问题

时间:2014-05-02 15:22:18

标签: c++ multithreading

我正在尝试用C ++创建一个聊天服务器,为此我需要使用线程。我的问题是创建线程,因为我得到奇怪的编译错误。

我得到的错误是:

我有一个Server.h和Server.cpp,这里是代码:

Server.h:

#ifndef SERVER
#define SERVER
#pragma once
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <iostream>
class Server
{
private:
sf::TcpListener listener;
std::vector<sf::TcpSocket*> clients;
sf::SocketSelector selector;
bool done;

public:
void time();
Server(int port);
~Server();
};
#endif

Server.cpp:注意:跳到线程实现(上面有注释)

#include "Server.h"
#include <thread>
void Server::time()
{
int clientIndex = 0;
sf::Clock c;
while (true)
{
    sf::Time time = c.getElapsedTime();
    if (time.asSeconds() > 6)
    {
        sf::Packet p;
        std::string message = "Time Done";
        p << message;
        clients[clientIndex]->send(p);
        return;
    }
}
}

Server::Server(int port)
{
listener.listen(port);
selector.add(listener);
done = false;
int clientNumber = 0;
sf::Packet sPacket; // packet to be sent
sf::Packet rPacket; // packet to be received
while (done == false) // while we are not finished with the server
{
    if (selector.wait()) // wait for something to happen
    {

        if (selector.isReady(listener)) 
        {
            clientNumber++;
            sf::TcpSocket * newClient = new sf::TcpSocket;
            listener.accept(*newClient);
            clients.push_back(newClient);
            selector.add(*newClient);
            sPacket << clientNumber; // send them their client number
            clients[clientNumber-1]->send(sPacket);
            sPacket.clear();
        }
        //Now loop through clients to check for activity:
        for (int i = 0; i < clients.size(); i++)
        {
            if (selector.isReady(*clients[i]))
            {
                clients[i]->receive(rPacket); // receive packet
                std::string message;
                rPacket >> message;
                std::cout << message << "\n";
                //Thread Here:
                **std::thread th(&time);
                th.join();**
                for (int j = 0; j < clients.size(); j++)
                {
                    if (j != i)
                    {
                        std::string finalM = "Client ";

                        finalM.append(": ");
                        finalM.append(message);
                        rPacket.clear();
                        rPacket << finalM;
                        clients[j]->send(rPacket);
                    }
                }
            }
        }
    }
}
listener.close(); // close the server when we are done
    }

如果可以,请帮忙。如果我的线程逻辑有问题,请协助:O

错误:

错误4错误C2100:非法间接c:\ program files(x86)\ microsoft visual studio 12.0 \ vc \ include \ functional 1241 1 ChatProgram

错误2错误C2276:&#39;&amp;&#39; :对绑定成员函数表达式的非法操作c:\ users \ michael \ documents \ visual studio 2013 \ projects \ chatprogram \ chatprogram \ server.cpp 55 1 ChatProgram

错误5错误C2296:&#39;。*&#39; :非法,左操作数有类型&#39; int&#39; c:\ program files(x86)\ microsoft visual studio 12.0 \ vc \ include \ functional 1241 1 ChatProgram

0 个答案:

没有答案