Thread: Thread and static objects

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    Thread and static objects

    Hi,
    I have a static function that is the entry point for a thread.
    Code:
    static DWORD WINAPI readPacket(LPVOID thisObject)
    thisObject is a pointer to the class where the thread is instantiated.

    Since the thread accesses data and functions found in the class, I had to make them static and define them outside of the class.
    Code:
    };
    static FILE *filterPacket_logfile; /* The logfile to log errors in*/
    static std::string error; /*This is the error printed to the logfile*/
    static int (*callback_Function)(unsigned char *, int);
    
    static void writeError(const char *errorString, int init_write);
    The problem is that other objects outside of the class now have access to the variables and can modify them.

    Is there any way I can have variables that the thread can access but that other objects outside of the class cannot access.

    I tried accessing the private data members of the class using thisObject but the compiler says it's an error. The compiler is MS visual Studio .NET 2003.

    error C2248: 'filterPacket:ort_Name' : cannot access private member declared in class 'filterPacket'

    Thanks a lot for any help,
    Amish

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    you shouldn't have to declare any of the variables as 'static'. just declare the thread entry point within the class with the 'friend' qualifier and it should work.

    Code:
    struct filterPacket
    {
    	friend
    	DWORD 
    	WINAPI 
    	readPacket(LPVOID thisObject)
    	{
    		((filterPacket*)thisObject)->port = 80;
    	}
    	
    	private:
    	
    	unsigned short port;
    };
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    Quote Originally Posted by Sebastiani
    you shouldn't have to declare any of the variables as 'static'. just declare the thread entry point within the class with the 'friend' qualifier and it should work.

    Code:
    struct filterPacket
    {
    	friend
    	DWORD 
    	WINAPI 
    	readPacket(LPVOID thisObject)
    	{
    		((filterPacket*)thisObject)->port = 80;
    	}
    	
    	private:
    	
    	unsigned short port;
    };
    That does not quite work. I get the following errors:
    error C2255: 'readPacket' : a friend function can only be declared in a class
    error C2556: 'void readPacket(LPVOID)' : overloaded function differs only by return type from 'DWORD readPacket(LPVOID)'

    with the following code:
    Code:
    riend DWORD WINAPI readPacket(LPVOID thisObject)
    {
    	int errorCode = 0; /* This is the error code that is returned  */
    	filterPacket * filterPacketPtr = (filterPacket*)thisObject;
    ...

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    do you have another declaration of readPacket outside of the class?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    Sorry
    you were correct. I had the friend modifier in both the declaration and instantiation of the thread. It works now. Thanks a lot,
    Amish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Free.
    By chakra in forum C Programming
    Replies: 9
    Last Post: 12-15-2008, 11:20 AM
  2. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  3. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  4. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM
  5. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM