Thread & Classes

This is a discussion on Thread & Classes within the C++ Programming forums, part of the General Programming Boards category; Hey i have this class: Code: class ClientHandler { private: // public: ClientHandler(); ~ClientHandler(){} DWORD WINAPI ServeClient(LPVOID lpParam); DWORD WINAPI ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    69

    Thread & Classes

    Hey i have this class:

    Code:
    class ClientHandler
    {
          private:
              //    
          public:
             ClientHandler();
             ~ClientHandler(){}
             DWORD WINAPI ServeClient(LPVOID lpParam);
             DWORD WINAPI WaitClients(PVOID pvParam);
    };
    and in function WaitClients Im trying to create thread :

    Code:
    CreateThread(NULL, 0, ServeClient, (LPVOID)NextFreeSpace, 0, &thread_id);
    Units/ClientHandler.cpp: In member function `DWORD ClientHandler::WaitClients(void*)':
    Units/ClientHandler.cpp:115: error: argument of type `DWORD (ClientHandler:(void*)' does not match `DWORD (*)(void*)'

    Units/ClientHandler.cpp: In member function `DWORD ClientHandler::ServeClient(void*)':

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,456
    you cannot create a thread from a class member function using CreateThread() unless it is static. you can do it with boost::thread and boost::bind, so I would suggest looking at those to see if they will work for you, if this is something that you need to do with non-static functions.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    69

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,337
    Sure. Create a function (either a global function, or a static member function of a class) that, in turn, invokes the member function on an object.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Ok, i will try. But i have question about crypt my connection, i would like to use openssl lib. So should it be done?

    1. Client is sending to server his public key
    2. Server random big number and crypt it with client's public key
    3. Client use this big number to create secret key and send to server
    4. Server use this same big number to create his secret key and send to client

    And then server is using client's secret key to crypt and client is using server's secret key to crypt?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Why do you not want to use boost? You are aware that CreateThread is dangerous if you use C/C++ library functions, yes?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Quote Originally Posted by Elysia View Post
    Why do you not want to use boost? You are aware that CreateThread is dangerous if you use C/C++ library functions, yes?
    I will try, i just don't like to complicate life by writing 10 lines instead of 1

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    o_O
    Boost makes life easier. Here is what boost would look like:
    Code:
    boost::thread thread(&ServeClient, NextFreeSpace);
    And we're done!
    If you want to wait on the thread, add
    Code:
    thread.join();
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Quote Originally Posted by Elysia View Post
    o_O
    Boost makes life easier. Here is what boost would look like:
    Code:
    boost::thread thread(&ServeClient, NextFreeSpace);
    And we're done!
    If you want to wait on the thread, add
    Code:
    thread.join();
    Then i was wrong :P

    But any clues about ssl?

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    I am not well versed in networking stuff, sorry. I have never used ssl from C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Ok

    Btw, im trying to create thread this way u wrote but :

    Units/ClientHandler.cpp:122: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say `&ClientHandler::ServeClient'

    And when i try :

    Code:
    boost::thread thread(&ClientHandler::ServeClient, NextFreeSpace);
    Then it shows me errors in bind_template.hpp

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Not quite how it works. Member functions are coupled to instances, so you need to provide an instance in your parameter list. Eg:
    Code:
    ClientHandler handler;
    boost::thread thread(&ClientHandler::ServeClient, handler, NextFreeSpace);
    Where basically handler would be your instance of the class whose member function ServeClient you would like to call.
    Btw, this would never work with CreateThread
    Savor the power of boost!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Quote Originally Posted by Elysia View Post
    Not quite how it works. Member functions are coupled to instances, so you need to provide an instance in your parameter list. Eg:
    Code:
    ClientHandler handler;
    boost::thread thread(&ClientHandler::ServeClient, handler, NextFreeSpace);
    Where basically handler would be your instance of the class whose member function ServeClient you would like to call.
    Btw, this would never work with CreateThread
    Savor the power of boost!
    Doing like this again shows me errors in bind_template.hpp

    And this :

    Code:
    boost::thread thread(&ClientHandler::ServeClient, handler, (LPVOID)NextFreeSpace);
    Units/ClientHandler.o(.text+0x2891):ClientHandler.cpp: undefined reference to `boost::thread::~thread()'
    Units/ClientHandler.o(.text$_ZN5boost6threadC1IM13Client HandlerFmPvES2_S3_EET_T0_T1_[boost::thread::thread<unsigned long (ClientHandler::*)(void*), ClientHandler, void*>(unsigned long (ClientHandler::*)(void*), ClientHandler, void*)]+0xfa):ClientHandler.cpp: undefined reference to `boost::thread::start_thread()'


    I'm using win7 and dev-c++.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    This is right, actually. You need to compile boost and link to it (which the library should fix by itself, I believe). There are instructions at the boost homepage, or alternatively there might be binaries ready. I know there are for Visual Studio at least.
    Oh, and don't cast NextFreeSpace to LPVOID.

    EDIT: You might get binaries from here:
    http://ascendwiki.cheme.cmu.edu/Bina...Boost_on_MinGW
    Last edited by Elysia; 03-15-2011 at 01:09 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Weird.. ihave installed binary and if i don't cast NextFreeSpace to LPVOID then i got errors in bind_template.hpp, but now if i cast to LPVOID it works, atleast compile

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-09-2010, 07:31 PM
  2. user thread library
    By Eran in forum C Programming
    Replies: 4
    Last Post: 06-17-2008, 01:44 AM
  3. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  4. Replies: 12
    Last Post: 05-17-2003, 05:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21