|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
Re: Member function as Task
The technique is to create a static function in your class. The first parameter to your function is this. In the function, dispatch to the member function using the this pointer. This technique is a kind of a thunk.
Code:
class MyClass
{
private:
static void sProcessTask(MyClass* self, int someParam);
void DispatchTask();
void ProcessTask();
}
void MyClass::sProcessTask(MyClass* self, int someParam)
{
self.ProcessTask(someParam);
}
void MyClass::DispatchTask()
{
Task myTask = new Task("MyTask", sProcessTask, this, someParam);
}
void MyClass::ProcessTask(int param)
{
// do something on task
}
|
|
#2
|
|||
|
|||
|
Re: Member function as Task
Thanks, that works.
|
|
#3
|
||||
|
||||
|
Re: Member function as Task
You can use a member function but you'll have to pass an instance to Task. I've looked in the Task source and it should work, since it forwards all of its arguments to thread. Thread internally detects if you gave a member function pointer and uses the first argument as the instance to call the function from.
Code:
class MyClass
{
private:
void DispatchTask();
void ProcessTask(int param);
}
void MyClass::DispatchTask()
{
Task myTask = new Task("MyTask", &MyClass::ProcessTask, this, someParam);
}
void MyClass::ProcessTask(int param)
{
// do something on task
}
Last edited by euhlmann : 14-02-2016 at 11:15. |
|
#4
|
|||
|
|||
|
Re: Member function as Task
Quote:
You can do this in 1 line. Code:
::std::thread my_thread(::std::bind(&MyClass::MyMethod, this, arg1, arg2)); Code:
::std::thread my_thread; my_thread = ::std::thread(::std::bind(&MyClass::MyMethod, this, arg1, arg2)); |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|