boost::thread + singelton + private



  • hidiho,
    hab folgendes Problem,
    und zwar hab ich eine Singelton Klasse (bisschen Hintergrund: die Klasse verwaltet die ganze Netzwerkarbeit im Programm).
    Diese Klasse hat eine public Funktion "init()" und eine private "main_thread()".
    Mithilfe von boost::thread will ich in der funktion "init" neben dem ganzen Netzwerk initialisierungs Zeug auch einen Thread erstellen.
    Wie man sich vielleicht schon denken kann soll die Thread Funktion "main_thread" sein.
    So, das Problem ist jetzt nur folgendes:
    boost::thread beschwert sich das es weder den Konstruktor noch den Desktruktor meiner Klasse aufrufen kann

    c:\Boost\include\boost\function\function_template.hpp(289) : error C2248: 'core::network::Network::Network' : cannot access private member declared in class 'core::network::Network'
    d:\([ FtP ])\([ documents ])[ coding ]\--- CPP\--- BinaryControl\TestApps\netzwerk\network.hpp(82) : see declaration of 'core::network::Network::Network'

    c:\Boost\include\boost\function\function_template.hpp(289) : error C2248: 'core::network::Network::~Network' : cannot access private member declared in class 'core::network::Network'
    d:\([ FtP ])\([ documents ])[ coding ]\--- CPP\--- BinaryControl\TestApps\netzwerk\network.hpp(80) : see declaration of 'core::network::Network::~Network'

    Eine Lösung wäre eine Funktion ausserhalb der Klasse zu machen die den "main_thread()" der Klasse einfach aufruft und dann boost::thread diese Funktion zu übergeben, dann müsste ich "main_thread" aber public machen, was mir ehrlich gesagt nicht so ganz passt...

    Hoffentlich weiss jemand von euch ne schönere Lösung für das Problem ansonsten werd ich wohl -sollte mir nix anderes einfallen- diese Verwenden müssen 😕



  • hm, wo sind die boost experten hier 😕



  • Wenn ich dich richtig verstehe, übergib dem Thread ein boost::ref-Objekt, dass eine Referenz auf die Instanz hält. Dann müsste das gehen (glaube ich).

    Genau kann ich das aber nicht sagen, ohne den Code gesehen zu haben.



  • hm, wenn du code sehn willst:

    // ****************
    // * network.hpp  *
    // ****************
    
    class Network {
    	public:
    		static Network* get_instance() throw();
    		static void Kill() throw();
    
    		void init(unsigned port=51930) throw(net_err);
    		// ...
    
    	//
    	private:
    		~Network();
    		Network();
    		Network(const Network &other);
    		void operator=(const Network&);
    
    		// ...
    
    		void server_thread() throw(net_err);
    
    		// ...
    
    		static Network* s_pClass;
    };
    
    // ****************
    // * network.cpp  *
    // ****************
    
    #include "network.hpp"
    #include <boost/thread/thread.hpp>
    #include <boost/thread/mutex.hpp>
    #include <boost/thread/condition.hpp>
    
    //.....
    
    Network* Network::s_pClass = 0;
    
    Network* Network::get_instance()
    {
    	// ...
    }
    
    void Network::Kill()
    {
    	// ...
    }
    
    void Network::init(unsigned port) throw(net_err)
    {
    	// ...
    	boost::thread sv_thrd(server_thread); // krrcsskckrrcksckkrscks !
    }
    
    void Network::server_thread()
    {
    	// ...
    }
    


  • jetz muss ich doch glatt nochmal nachhaken...



  • Schonmal in etwa so probiert ?

    #include <boost/thread/thread.hpp>
    #include <boost/bind.hpp>
    
    class Network
    {
    	private:
    		void server_thread()
    		{
    			// ...
    		}
    
    	public:
    		void init(unsigned int port)
    		{
    			// ...
    
    			boost::thread sv_thrd(boost::bind(&Network::server_thread, this));
    		}
    };
    

Anmelden zum Antworten