SFML community forums

Help => System => Topic started by: untorched on December 20, 2013, 02:53:25 am

Title: [RESOLVED]Creating threads problem
Post by: untorched on December 20, 2013, 02:53:25 am
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..
Title: Re: Creating threads problem
Post by: zsbzsb on December 20, 2013, 03:13:55 am
You obviously have a misunderstanding of how threads and basic C++ loops work. I suggest you get a good C++ book and learn how basic control structures work. As for multithreading, I suggest you avoid that entire area unless you have a real good reason for needing it. In most cases you will just loose performance.

To your edit

It is not resolved, now you have a major memory leak, please learn C++ first... And 10 threads really shows that you don't understand multithreading at all  :P
Title: Re: [RESOLVED]Creating threads problem
Post by: untorched on December 26, 2013, 09:13:51 pm
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.
Title: Re: [RESOLVED]Creating threads problem
Post by: zsbzsb on December 27, 2013, 04:40:55 am
First learn how to manage memory without leaking it... and read this. (http://en.sfml-dev.org/forums/index.php?topic=13902.msg97463#msg97463)

Second, seriously don't use threads, at this stage you won't gain any benefit and on top of that you don't understand them because a new thread for each client is a terrible idea (how do you plan to synchronize that many threads to a single game state?).

Third, maybe consider something like SFNUL (http://en.sfml-dev.org/forums/index.php?topic=13723.0).