Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [RESOLVED]Creating threads problem  (Read 3599 times)

0 Members and 1 Guest are viewing this topic.

untorched

  • Newbie
  • *
  • Posts: 3
    • View Profile
[RESOLVED]Creating threads problem
« 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..
« Last Edit: December 20, 2013, 03:13:09 am by untorched »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Creating threads problem
« Reply #1 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
« Last Edit: December 20, 2013, 03:25:12 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

untorched

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: [RESOLVED]Creating threads problem
« Reply #2 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.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: [RESOLVED]Creating threads problem
« Reply #3 on: December 27, 2013, 04:40:55 am »
First learn how to manage memory without leaking it... and read this.

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.
« Last Edit: December 27, 2013, 04:44:38 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor