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

Author Topic: My thread isn't running  (Read 2857 times)

0 Members and 2 Guests are viewing this topic.

lukeescude

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • http://www.lukeescude.com
My thread isn't running
« on: January 21, 2012, 04:39:12 am »
Hey all, I'm new here, and pretty new to multithreading.

I'm using SFML threads as a class, and here's the class code:
Code: [Select]
class SendUsers : public sf::Thread
{
public:
double sock;
private:
    virtual void Run()
    {
        // Print something...
        cout << "Cheers" << endl;
    }
};


And here's where the thread is SUPPOSED to start:
Code: [Select]
SendUsers t;
t.sock=Players[i].Sock; //Players[i].Sock is a double
t.Launch();


But the thread isn't launching/running at all, not sure what to do.

Thanks in advance!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
My thread isn't running
« Reply #1 on: January 21, 2012, 09:24:41 am »
Can you please post a complete source code?
Laurent Gomila - SFML developer

lukeescude

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • http://www.lukeescude.com
My thread isn't running
« Reply #2 on: January 21, 2012, 03:36:46 pm »
Here's the ZIP of the entire project:

http://www.lukeescude.com/serverc.zip

This is my first program in C++, so please excuse the terrible coding.

Line 609 of CPP.cpp (the main program file) is where the thread should be launching.

SendUserlist.h contains the outline of the thread code.

EDIT: I wrote a separate program using SFML threads and it worked perfectly, so there's something in my server code that's interfering with the thread creation.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Laurent Gomila - SFML developer

posva

  • Full Member
  • ***
  • Posts: 118
  • Feed me pls
    • View Profile
    • Posva Dev Blog
My thread isn't running
« Reply #4 on: January 21, 2012, 03:58:56 pm »
Might this help, from the tutorial?
Code: [Select]
class Task
 {
 public :
     void Run()
     {
         ...
     }
 };

 Task task;
 sf::Thread thread(&Task::Run, &task);
 thread.Launch(); // start the thread (internally calls task.run())

You have to give a minimal code to let people find the errors (never give your entire project, juste paste some code)

lukeescude

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • http://www.lukeescude.com
My thread isn't running
« Reply #5 on: January 21, 2012, 04:03:05 pm »
@Laurent - That's the problem with a project like this: It's a server, so if I give you "minimal code" it'll just not run. It needs all components to run.

@Posva - I'll check and see if that code works.

EDIT: No instance of constructor "sf::Thread::Thread" matches the argument list

EDIT2: Here, I've cut down the server... http://www.lukeescude.com/serverc.zip

EDIT3: Alright, instead of using sf::Thread as a base class, I've gone back to using threads from functions.

The function launches, but not as a separate thread.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
My thread isn't running
« Reply #6 on: January 21, 2012, 05:26:34 pm »
Quote
That's the problem with a project like this: It's a server, so if I give you "minimal code" it'll just not run. It needs all components to run.

... that's why we need a minimal code, not your big project. You need to write it, not just try to cut pieces from your original code. I'm pretty sure that we don't need all the components that you mentioned, to reproduce this thread problem.

But I'll save you the effort: it looks like your sf::Thread instance is local to a function, so it is destroyed as soon as it is launched. And guess what, since the thread cannot live without its sf::Thread instance, the destructor waits for the threaded function to finish, so it's exactly as if you didn't use a thread at all. Well, that's just a guess.

And you forgot to tell us which version of SFML you're using.
Laurent Gomila - SFML developer

lukeescude

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • http://www.lukeescude.com
My thread isn't running
« Reply #7 on: January 21, 2012, 05:34:47 pm »
The current version, 1.6

I didn't think of that. I've changed it to make a thread from the sendUsers() function, instead of using the thread as a base class, and it works, but it doesn't execute as a thread, it executes as a normal function.

I wrote a separate program to test, and it worked just fine. So I think what you said about it being destroyed as soon as it was created is what the problem is - How would I counteract this? Make the thread instance outside the program scope?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
My thread isn't running
« Reply #8 on: January 21, 2012, 06:08:11 pm »
Yeah, just declare your thread instance in a scope which is wider than the current function.
Laurent Gomila - SFML developer

lukeescude

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • http://www.lukeescude.com
My thread isn't running
« Reply #9 on: January 21, 2012, 06:09:45 pm »
Since each new connection to the server is treated as a separate thread, the thread is instantiated within the function that detects for new connections. I'm not sure how I'd go about declaring the thread outside the scope, then using it later on.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
My thread isn't running
« Reply #10 on: January 21, 2012, 06:13:21 pm »
You must have a container of all living clients, don't you? Then do the same for the thread instances (or include them directly into the "client" instances or whatever).
Laurent Gomila - SFML developer

lukeescude

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • http://www.lukeescude.com
My thread isn't running
« Reply #11 on: January 21, 2012, 06:23:07 pm »
Yeah, for some reason, even outside in the main() method, only 1 thread can be created, and the main application stops execution - the thread is being treated like a normal function.

EDIT: Never mind, I essentially re-created the issue I started with. Figured it out now. Thanks a ton!

Do you know if it's possible to store threads in an array?

 

anything