Thread and static objects

This is a discussion on Thread and static objects within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a static function that is the entry point for a thread. Code: static DWORD WINAPI readPacket(LPVOID thisObject) ...

  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,669
    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;
    };



    ITSA
    Socket Library!

  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,669
    do you have another declaration of readPacket outside of the class?



    ITSA
    Socket Library!

  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, 10: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, 07:50 AM
  5. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 10:34 PM

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