Passing an Object and getting the return value from a thread call

768 Views Asked by At

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.

2

There are 2 best solutions below

2
Some programmer dude On

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);

    ...
}
2
SHR On

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);