0

I want to pass a class object into a method of other class through a thread call, I tried but got the error can any one help me on this.please.

struct sample{

  int status;

  std::vector <std::string> a_row;

  size_t column_count;

  std::vector <std::string> column;

};

class sess {

public:

    int a;

    sess(int);

    sess();

};

class Gen { 

     private: 

       static Gen* gen_obj;

     public:

       static bool Gen_InstanceFlag;

       static Gen* GetInstance();

       sample addition(sess);

};

/* End of Header File */

/* Beginning of cpp File */

include"Class_thread_mixing.h"

bool Gen::Gen_InstanceFlag=false;

Gen* Gen::GetInstance(){

if(!Gen::Gen_InstanceFlag){

    Gen::gen_obj = new Gen();

   Gen::Gen_InstanceFlag= true;

   return gen_obj;

  }
else {

    return gen_obj;

  }

}


sample addition(sess ses_obj){

 sample sam;

 sam.a_row.push_back("success");

 sam.column.push_back("result");

 sam.column_count=1;

 sam.status=ses_obj.a;

return sam;
}


int main(int argc, char* argv[])

{

  HANDLE myhandleA;

  Gen* gen=Gen::GetInstance();

  sess ses_obj(10);

  myhandleA=(HANDLE)_beginthreadex(0, 0, gen->addition(ses_obj),(void*)0, 0, 0);

  WaitForSingleObject(myhandleA, INFINITE);

  CloseHandle(myhandleA);

  getchar();

  return 0;

}

This is my code and I am getting an error like "error C2665: '_beginthreadex' : none of the 2 overloads could convert all the argument types"

Can any one suggest me who can I pass the object of sess to a function in a thread call and how can I get the results from the thread.

Thanks for your answers..

s there any option such that I can call the function directly in the thread without calling a standalone thread function, like I mentioned in my code [(HANDLE)_beginthreadex(0, 0, gen->addition(ses_obj),(void*)0, 0, 0) ]

I need to call addition method in the thread can any body help me on this.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
vamsi krishna
  • 31
  • 1
  • 8
  • When posting a question about compilation errors, please put the whole, complete and unedited error message in the question. It will make it much easier to help you. You might want to read [the Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist), it will help you write good questions. – Some programmer dude Oct 09 '13 at 12:01

2 Answers2

2

The problem here is that instead of passing a function to the called by _beginthreadex, you are actually calling that function with an argument, causing the _beginthreadex function to be called with the return value from Gen::addition. This structure is if course not a function, and so the compiler complains.

The solution to this is not straightforward though. First of all because a stand-alone function (as required by _beginthreadex is not the same as a class member function. The reason being that all class member functions actually have a "zeroeth" hidden argument, and that is an instance of the class that becomes the this pointer that can be used inside member functions.

The best solution is probably to create a stand-alone function, which takes as argument a pointer to a structure, and the structure contains the object instance, and the argument to the actual member function.

Something like this:

struct call_data
{
    sess sess_obj;
    Gen* gen_obj;
};

static void thread_function(void* data)
{
    call_data *call = reinterpret_cast<call_data*>(data);

    // Do the actual member function call
    call->gen_obj->addition(call->sess_obj);
}

int main()
{
    ...

    call_data call = { ses_obj, gen };
    myhandleA=(HANDLE)_beginthreadex(0, 0, thread_function, &call, 0, 0);

    ...
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Hi, Is there any option such that I can call the function directly in the thread without calling a standalone thread function.like I mentioned in my code [(HANDLE)_beginthreadex(0, 0, gen->addition(ses_obj),(void*)0, 0, 0) ] – vamsi krishna Oct 10 '13 at 07:14
  • @vamsikrishna No there isn't. First of all what you do now is *calling* the member function in your `_beginthreadex` call. Secondly because member functions are not the same as functions. All of this I explain in my answer. – Some programmer dude Oct 10 '13 at 09:18
0

Entry point for thread can't be a class method. it has to be static or at global scope.

You need to define a static method like this

static unsigned ThreadEntry(void* arg)
{
   sess *p = (sess*)arg;
   Gen::GetInstance()->addition(*p);
} 

and run the thread like this:

sess ses_obj(10);
myhandleA=(HANDLE)_beginthreadex(0, 0, ThreadEntry,(void*)&ses_obj, 0, 0);
SHR
  • 7,940
  • 9
  • 38
  • 57
  • Hi, Is there any option such that I can call the function directly in the thread without calling a standalone thread function.like I mentioned in my code [(HANDLE)_beginthreadex(0, 0, gen->addition(ses_obj),(void*)0, 0, 0) ] – vamsi krishna Oct 10 '13 at 07:14
  • no. but you can call to a static function from the class. mean ThreadEntry can be "static unsigned Gen::ThreadEntry(void* arg){...}" but it is still the same. – SHR Oct 10 '13 at 08:30