Hello!
I have a problem with threads. I want make something like this:
void myFunc() {
std::cout << "Works!";
for(;;); // loop waiting for something
}
while(1) {
if(sth) {
sf::Thread t(&myFunc);
t.launch();
}
}
Now it will create 1 thread and while will stop. If in myFunc() won't be for() it works good. Help my please.
I have main() -> create thread sth observator -> if observator gets what want it create some threads...But now create only one ;c
I don't know, maybe I'm stupid...
// ===== RESOLVED
sf::Thread *th;
for(int i=0; i<10; i++) {
th = new sf::Thread(&func, i);
th->launch();
}
th->wait();
Rly, I'm stupid..
Hm. So this example of "multithread server" is wrong?
sf::TcpSocket* client;
sf::TcpListener listener;
sf::SocketSelector selector;
int main() {
listener.listen(5555);
selector.add(listener);
while(1) {
if(selector.wait()) {
sf::TcpSocket* temp = new sf::TcpSocket;
if(listener.accept(*temp) == sf::Socket::Status::Done) {
client = temp;
selector.add(*client);
sf::Thread* th;
th = new sf::Thread(&clientProc);
}
else {
delete temp;
}
}
}
}
void clientProc() {
// Client connected so disconnect.
client->disconnect();
}
This is working good on windows but on linux client won't disconnect after connect.
So maybe someone could tell me what i'm doing wrong? Maybe example how it should be?
Help man.
Thanks.