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

Author Topic: Problem Creating Threads  (Read 6074 times)

0 Members and 1 Guest are viewing this topic.

Walta69

  • Newbie
  • *
  • Posts: 30
    • View Profile
Problem Creating Threads
« on: April 30, 2014, 03:28:47 pm »
My problem is simple... I am creating a TCP server and I need threads to process requests
and track time. However when i try create a thread it gives the error:

"error C2064: term does not evaluate to a function taking 1 arguments"

Here is some of the code:

sf::Thread * t = new sf::Thread(&Server::time, 0);
t->launch();

Where there is a method:

void Server::time(int clientIndex)

Any help would be welcomed :)

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Problem Creating Threads
« Reply #1 on: April 30, 2014, 07:02:12 pm »
you can use several types of entry points:

non-member functions with no argument
non-member functions with one argument of any type
functors with no argument (this one is particularly useful for compatibility with boost/std::bind)
functors with one argument of any type
member functions from any class with no argument
I don't see a "member function with an argument" in the doc. ;)

Walta69

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Problem Creating Threads
« Reply #2 on: April 30, 2014, 07:10:21 pm »
Alright :p so any advice on how to fix this issue? :)

Walta69

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Problem Creating Threads
« Reply #3 on: April 30, 2014, 07:15:07 pm »
Note:

Even if I change it to std::thread th(&Server::time); (firstly changing the time function),
it still doesn't work and gives the error "term does not evaluate to a function taking 0 arguments"

Here are portions of the code:

#include "Server.h"
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)
{

...
...
...

sf::Thread th(&Server::time);
}

This is all done in the implementation of Server.h

Walta69

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Problem Creating Threads
« Reply #4 on: April 30, 2014, 07:15:51 pm »
sf::Thread,

not std::thread :P

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Problem Creating Threads
« Reply #5 on: April 30, 2014, 08:11:41 pm »
sf::Thread needs to know what variable to use. Which in your case is "this".
sf::Thread th(&Server::time, this);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problem Creating Threads
« Reply #6 on: April 30, 2014, 11:20:34 pm »
Use std::thread (and lambdas and/or std::bind) if your compiler supports it. It's much better.
Laurent Gomila - SFML developer

Walta69

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Problem Creating Threads
« Reply #7 on: May 02, 2014, 05:10:49 pm »
Even with std::thread I still get problems:

std::thread th(&time);

I get the error: "error C2276: '&' : illegal operation on bound member function expression   "

Note I am doing all of this in Server.cpp which is the implementation of Server.h.

I'm really confused as to why its not working :/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problem Creating Threads
« Reply #8 on: May 02, 2014, 05:18:11 pm »
The syntax to take the address of a member functions is the following:

&Class::function

When you pass a non-static member function to something that will try to call it, you often need to pass the object on which it will be called as well ("this"), because you can't call a member function without an instance of the class.

So...

std::thread th(&Server::time, this);

I'm pretty sure any tutorial about std::thread explains this kind of stuff... ;)
Laurent Gomila - SFML developer

Walta69

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Problem Creating Threads
« Reply #9 on: May 02, 2014, 05:26:20 pm »
Ok so how would I make a thread then from the constructor? :)
( I don't do well with tutorials because they all use big words :P )

Here is basically what I want to achieve:

Server::Server()
{
 ...
...
...
std::thread (&Server::time, this) // Note this still gives errors
}
void Server::time()
{
 ...
...
}


Also, if you could maybe explain, what if I wanted my time() function to take in arguments , e.g: time(int x)? :)

Thanks alot for help :)

Walta69

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Problem Creating Threads
« Reply #10 on: May 02, 2014, 06:13:26 pm »
I followed your advice and did :

std::thread th(&(Server::time), this);
th.join();

and I get a different error now :

Error   4 error C2100: illegal indirection

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problem Creating Threads
« Reply #11 on: May 02, 2014, 06:49:07 pm »
I followed your advice
No, you did not.  There's no parenthesis in Laurent's code.

You should really read a tutorial about threads. Multithreading is a complex topic, and if you're already getting problems at the syntax, this will become really difficult.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problem Creating Threads
« Reply #12 on: May 02, 2014, 08:45:55 pm »
Quote
Also, if you could maybe explain, what if I wanted my time() function to take in arguments , e.g: time(int x)?
You would use std::bind. Or maybe the constructor of std::thread can handle that directly. Just pass x as a third argument.
Laurent Gomila - SFML developer

 

anything