Thread: Thread & Classes

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should change your function to not accept LPVOID. NextFreeSpace should have a type. That's what you'll use in your function. That is, if you can. C++ preserves types, unlike 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.

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Yes, I had this because of CreateThread but now its not needed well looks nice

    Btw what is returned as thread in :
    Code:
    boost::thread thread(&ClientHandler::ServeClient, CHandler, NextFreeSpace);
    What type has this variable and what it represent?

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The type is boost::thread and it represents your new thread. You can wait for it, query information, stop and resume it, and so on.
    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.

  4. #19
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Quote Originally Posted by Elysia View Post
    The type is boost::thread and it represents your new thread. You can wait for it, query information, stop and resume it, and so on.
    How can i assign it to Clients[NextFreeSpace].thread?

    And its property set?

    Code:
    typedef struct TClient {
        boost::thread thread; //<-------
        SOCKET sock;
        struct sockaddr_in clientinfo;
       // class PacketProc PProc;
    } TCLIENT, *PCLIENT;

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I take it Clients is some array of boost::thread, then?
    In such case, there might be two solutions:

    boost::thread thread(...);
    std::swap(Clients[NextFreeSpace].thread, thread);

    Or

    boost::thread thread(...);
    Clients[NextFreeSpace].thread = std::move(thread);
    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.

  6. #21
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    With swap it says me its const boost and to move i have to include sth or ? It says its not member of std.

    I have struct TClient which is info about connected client. I have also array of TClient named Clients. I need to store thread or pointer into struct too.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It sounds like one Clients[NextFreeSpace].thread is not assignable (it's const)?
    Yes, std::move requires <utility> and C++0x.

    Here is an example which works:
    Code:
    #include <boost/thread.hpp>
    #include <algorithm>
    
    class A
    {
    public:
    	void foo(int) {}
    };
    
    int main()
    {
    	A a;
    	boost::thread thread(&A::foo, a, 0);
    	boost::thread thread2;
    	std::swap(thread2, thread);
    	return 0;
    }
    If your code doesn't work, then I can only assume that you cannot assign to Clients thread due to it being const. Code to demonstrate the problem perhaps?
    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.

  8. #23
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    I have something ..........ed up i guess.. even your example don't work.

    C:/Program Files (x86)/Boost-1.41.0/include/boost-1_41/boost/thread/detail/thread.hpp: In function `void std::swap(_Tp&, _Tp&) [with _Tp = boost::thread]':
    Units/ClientHandler.cpp:124: instantiated from here
    C:/Program Files (x86)/Boost-1.41.0/include/boost-1_41/boost/thread/detail/thread.hpp:108: error: `boost::thread::thread(boost::thread&)' is private
    C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algobase.h:130: error: within this context
    C:/Program Files (x86)/Boost-1.41.0/include/boost-1_41/boost/thread/detail/thread.hpp:109: error: `boost::thread& boost::thread:perator=(boost::thread&)' is private
    C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algobase.h:131: error: within this context
    C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algobase.h:132: error: passing `const boost::thread' as `this' argument of `boost::thread:perator boost::detail::thread_move_t<boost::thread>()' discards qualifiers

    Can u upload fixed stl_algobase.h? I guess it have not fixed problems.
    Last edited by kargo; 03-15-2011 at 03:17 PM.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hmmm. I thought it would work. Are you using GCC? If so, you should try enabling C++0x mode.
    Command line for GCC is: -std=c++0x
    Maybe some option in an IDE if you use one.
    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.

  10. #25
    Registered User
    Join Date
    Sep 2010
    Posts
    69

    Wink

    Quote Originally Posted by kargo View Post
    I have something ..........ed up i guess.. even your example don't work.
    I guess it have not fixed problems.
    Sorry to intrude here, while you are in the middle of something..., but,
    Kargo, you mentioned that:
    1) you are using Dev-C++,
    2) OS = Win7

    1) what version of Dev-C++ are you using ?
    2) have you gotten Dev-C++ to compile any program ?
    3) where is the working folder where your project is located ?
    4) when you compile, what does your complete compile log say ?


    I ask these questions because Dev-C++ release 4.9.9.2 is the latest release of Dev-C++ and it dates back to 2005.
    Dev 4.9.9.2 worked on XP (with caveats) and is know to have problems working on Vista and may not work at all on Win-7 .
    Unfortunately, Colin LaPlace has abandoned further development on DevC++

    That's why I ask question #2.
    #3 is of particular importance too.

    Have you read the "Read this before..." at the Dev-C++ forum:
    SourceForge.net: Dev-C++: Topics for Bloodshed Software Forum

    It does explain some of the many issues and problems of using Dev-C++.
    Also, Dev-C++ is using an old version of GCC.

    Just a thought.
    Last edited by Steve A.; 03-15-2011 at 04:06 PM.

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Old version of GCC sounds bad. Perhaps it's time to upgrade. The easiest solution I can think of is Code::Blocks + the mingw compiler. That should get you started on using C++0x.
    You might also try

    Clients[NextFreeSpace].thread.swap(thread);
    or
    Clients[NextFreeSpace].thread = boost::move(thread);
    Last edited by Elysia; 03-15-2011 at 03:56 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.

  12. #27
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    Quote Originally Posted by Kargo
    I'm using win7 and dev-c++.
    Quote Originally Posted by Elysia
    Old version of GCC sounds bad. Perhaps it's time to upgrade. The easiest solution I can think of is Code::Blocks + the mingw compiler. That should get you started on using C++0x.
    Yes,
    you can't simply use Dev-C++ with a newer version of GCC, it doesn't seen to work that way.
    Dev-C++ (the IDE) is closely bound to that version of GCC.

    Code::Blocks with MingW is a better option at this point.

    I only use Dev-C++ for testing legacy code on an XP.
    Where the issues and caveats are know to me and I can control them.

    Unless you have Dev-C++ working, flawlessly, you may be beating a dead-horse.
    Last edited by Steve A.; 03-15-2011 at 04:08 PM.

  13. #28
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Quote Originally Posted by Steve A. View Post
    Yes,
    you can't simply use Dev-C++ with a newer version of GCC, it doesn't seen to work that way.
    Dev-C++ (the IDE) is closely bound to that version of GCC.

    Code::Blocks with MingW is a better option at this point.

    I only use Dev-C++ for testing legacy code on an XP.
    Where the issues and caveats are know to me and I can control them.

    Unless you have Dev-C++ working, flawlessly, you may be beating a dead-horse.
    Guess it's time to changes

    I have problem, I'm using sockets and added lib by Project->Build Options->Linker Settings->Link Libaries. libwsock32 and libws2_32. But still I'm getting :

    \main.cpp|62|error: 'getaddrinfo' was not declared in this scope|
    Last edited by kargo; 03-16-2011 at 03:01 AM.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did you include Wspiapi.h? MSDN says it's declared in there.
    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. #30
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Quote Originally Posted by Elysia View Post
    Did you include Wspiapi.h? MSDN says it's declared in there.
    No i don't but found solution :

    Code:
    #define _WIN32_WINNT 0x501
    Quote Originally Posted by Elysia View Post
    Did you include Wspiapi.h? MSDN says it's declared in there.
    No i don't but found solution :

    Code:
    #define _WIN32_WINNT 0x501
    Including is ........ing me off :P

    Share.hpp
    Code:
    #ifndef SHARE_H
    #define SHARE_H
    
    #ifndef NETWORK_HANDLER_H
        #include "NetworkHandler.hpp"
    #endif
    
    #ifndef CLIENT_HANDLER_H
        #include "ClientHandler.hpp"
    #endif
    
    #include <windows.h>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdio.h>
    #include <iostream>
    #include <boost/thread/thread.hpp>
    
    typedef BYTE PCK[65000];
    typedef PCK *PPCK;
    typedef BYTE Key16[16];
    
    NetworkHandler NHandler; //<----------- ERROR : NetworkHandler does not name a type
    #endif
    NetworkHandler.hpp
    Code:
    #ifndef NETWORK_HANDLER_H
    #define NETWORK_HANDLER_H
    #include "share.hpp"
    
    class NetworkHandler
    {
        private:
            bool StartWSA();
            bool InitSock();
            bool CreateSock();
            bool BindSock();
            bool SetListener();
        public:
            SOCKET ListenSocket;
         //   NetworkHandler(){};
         //   ~NetworkHandler(){};
            bool Init();
    };
    
    #endif
    ClientHandler.hpp
    Code:
    #ifndef CLIENT_HANDLER_H
    #define CLIENT_HANDLER_H
    #include "share.hpp"
    
    class ClientHandler
    {
          private:
              //
          public:
             ClientHandler();
             ~ClientHandler(){}
             void StartListen();
           //  DWORD WINAPI ServeClient(LPVOID lpParam);
             DWORD WINAPI ServeClient(int IDs); //<-------------- ERROR : DWORD does not name a type
             DWORD WINAPI WaitClients(PVOID pvParam); //<---------------- SAME
    };
    
    #endif
    Last edited by kargo; 03-16-2011 at 05:26 AM.

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