CreateThread() arguement error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eagerlearner
    New Member
    • Jul 2007
    • 29

    CreateThread() arguement error

    Hallo, when I use class and I put one of the member function as the CreateThread argument, I keep getting the compile error in Visual Studio 2005.

    Code:
    #include <windows.h>
    
    class X {
    public:
    	void f();
    	void s(){};
    };
    
    void X::f()
    {
    	//error C2664: 'CreateThread' : cannot convert parameter 3 from 'void (__thiscall X::* )(void)' to 'LPTHREAD_START_ROUTINE'
    	HANDLE hThread = CreateThread(NULL, 0, &X::s, (void *) 1, 0, NULL);
    
    	//error C3867: 'X::s': function call missing argument list; use '&X::s' to create a pointer to member
    	HANDLE hThread2 = CreateThread(NULL, 0, s, (void *) 1, 0, NULL);
    }
    int main() {
    	return 0;
    }
    What should I put for the third argument of CreateThread ? Thank you.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    CreateThread() requires that your thread function be a THREADPROC.

    A THREADPROC can be any function with this prototype:

    DWORD WINAPI ThreadProc(LPVO ID);

    Only functions with this prototype can be used as a CreateThread() argument.

    Also, the function needs to be static. Typcially, you call CreateThread with the address of your threadproc function and using this as the thread argument.

    This allows you to typecast the argument inside the thread function back to the class type so you can call other class methods.

    Comment

    • eagerlearner
      New Member
      • Jul 2007
      • 29

      #3
      Thanks, in my actual implementation I decalare it as static member function, now I see my mistake is actually the function prototype that I did not follow. I got another problem here using hash map. I thought no need to post in new thread.


      Code:
      #include <hash_map> 
      #include <iostream>
      #include <string>
      
      using namespace std;
      
      static enum H_FIELD {H_UNDEFINED, H_FIRST, H_SECOND, H_THIRD};
      
      class X
          {
      	public:
      		X()
      		{
      			hm[H_UNDEFINED] = "abc";
      		};
      
      		string getHField(H_FIELD) const;
      
      	private:
      		stdext::hash_map<H_FIELD, string> hm;
      	};
      
      
      //When this is const member function
      //error C2678: binary '[' : no operator found which takes a left-hand operand of 
      // type 'const stdext::hash_map<_Kty,_Ty>' (or there is no acceptable 
      // conversion)
      
      string X::getHField(H_FIELD h) const
      {
      	return hm[h];
      }
      
      int main()
      {
      	X obj;
      	cout << obj.getHField(H_UNDEFINED);
      	return 0;
      }
      It will only compile successfully when I delete the keyword "const" of the function X::getHField. is it because I only insert the map with hm[H_UNDEFINED] = "abc", so if I use X::getHField(H_ FIRST), the default value zero will be returned and the hm map element will be increased by one so I will indirectly change the data inside the hm, so that's why my function X::getHField cannot decalared as const. Can you confirm that ? Thank you.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        The private member variable hm is a hash_map. When you have a const member function, you promise not to change any of the class members. However, by calling hash_map methods, you may, in fact, change the hm variable internally.

        Just because you don't change the variable doesn't mean that the variable can't change itself. And that is prohibited in a const member function.

        You have two options:
        1) remove the const from the member function.
        2) keep the const but qualify hm as mutable indicating that hm does not participate in the const.

        Comment

        • eagerlearner
          New Member
          • Jul 2007
          • 29

          #5
          Thanks, before you tell me, I never know there is a keyword mutable, cool. I think I would prefer using the mutable and keep my member function const. I wish I have started learning programming since I was a baby. :)

          Comment

          • eagerlearner
            New Member
            • Jul 2007
            • 29

            #6
            Again I got another problem, why does this give me the default value of 0 instead of 1 ? Thank you.

            Code:
            #include <iostream>
            #include <hash_map>
            #include <string>
            using namespace std;
            
            int main()
            {
            	stdext::hash_map<const char *, int> hm;
            	hm["a"] = 1;
            	hm["b"] = 2;
            
            	string s = "a";
            
            	cout << hm[s.c_str()];
            
            	return 0;
            }

            Comment

            Working...