0

Recently i started using boost::thread (also tried with STL - thread) in order to build a chat. I made at my "server-station" a class that has

  1. void function that get calls from main.cpp which starting the server listening + binding
  2. void function that is the thread that is being called from the function i stated before

Code:

this is ServerSocket.cpp file

void ServerSocket::startHosting()
{
    Bind();
    Listen();

    boost::thread clients_listener = boost::thread(&ServerSocket::clientsListener);
}

//This is the thread function

void ServerSocket::clientsListener()
{
     ..... handling incoming clients sockets  , code goes here ....
}

In the creating thread part i added 'this' because i read on stackoverflow that a no-static member function must have the 'this' member that represents the class, But when i added that i had another error but this time 1 value instead of 0 when the 'this' is not given

Error C2064 - term does not evaluate to a function taking 1 argument

Does someone know how to solve this and explain me the answer please? I can do a static function but that requires me to make all the other members that i need in that function as static to and i don't wanna do that

ToastMaster
  • 135
  • 1
  • 9
  • `boost::thread(boost::bind(&ServerSocket::clientsListener, this));` (note, not tested, but if history has taught me anything...). Not sure what you're planning on doing with that object, as you create it, then immediately destroy it when closing `startHosting()`. – WhozCraig Sep 17 '14 at 06:33
  • Didn't work as well... – ToastMaster Sep 17 '14 at 06:39
  • you mean, you tried this : boost::thread clients_listener = boost::thread(&ServerSocket::clientsListener, this); and it fails? – sajas Sep 17 '14 at 06:45
  • 2
    Did you at-all address what I mentioned in the last sentence of that comment? I'm guessing not. Again, you're **immediately** destroying the `boost::thread` object just-created. And "didn't work" *how*? Didn't *compile*? Didn't *run* ? Didn't run like you wanted it to? If this question *isn't* [**a duplicate of this question**](http://stackoverflow.com/questions/4581476/using-boost-thread-and-a-non-static-class-function) its as close as it can get. – WhozCraig Sep 17 '14 at 06:45
  • But the thread is on the Operating System level , does the boost::thread object instance needs to be saved in order to run? – ToastMaster Sep 17 '14 at 07:12
  • Yes. Check out [the docs](http://www.boost.org/doc/libs/1_56_0/doc/html/thread/thread_management.html#thread.thread_management.thread.destructor). Note that the exact behavior depends on both your version of Boost and a couple of preprocessor flags that can be used for configuration. – ComicSansMS Sep 17 '14 at 08:13

1 Answers1

2

In the following line:

boost::thread clients_listener = boost::thread(&ServerSocket::clientsListener);

You pass the member function pointer but the object to which to apply the pointer is missing. You probably want something like:

boost::thread clients_listener = boost::thread(&ServerSocket::clientsListener, this);

Also, you do not need to spell the type twice on the same line as in Java, so:

boost::thread clients_listener(&ServerSocket::clientsListener, this);

Would be sufficient.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271