Go to Post Every now and then, the judicious application of a modicum of common sense can make all the difference in the world. - dlavery [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 13-02-2016, 19:05
cantdecide cantdecide is offline
Registered User
FRC #5773 (YAFL Mechatronics)
Team Role: Programmer
 
Join Date: Jan 2016
Rookie Year: 2016
Location: Turkey
Posts: 31
cantdecide is an unknown quantity at this point
Member function as Task

I'm using the Task class in WPILib to poll for IMU data at the moment, and in doing so I've discovered that I can't cast member functions to a FUNCPTR or a VOIDFUNCPTR. To solve this, I created a static class which holds an IMU instance and has a static function that calls the instance's update method. That did it, but now I have more methods that I'd like to call in the background. I figured that creating a class for every member function is not a very extendable solution, so my question is, how can I create Tasks with member functions? Or am I missing something and can cast a member function to a FUNCPTR? Do I even need to cast functions to FUNCPTRs? The online examples I saw casted functions to FUNCPTRs, and directly passing functions as arguments throws a compile-time error.
Reply With Quote
  #2   Spotlight this post!  
Unread 13-02-2016, 22:25
duane's Avatar
duane duane is offline
Registered User
FRC #0701 (RoboVikes)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2003
Location: Vacaville
Posts: 90
duane is an unknown quantity at this point
Send a message via AIM to duane
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
}
(Warning: Uncompiled and untested)
__________________
Duane Murphy
Mentor - Software
Vanden Vikings FIRST Team 701
http://www.vandenrobotics.com
Reply With Quote
  #3   Spotlight this post!  
Unread 14-02-2016, 06:12
cantdecide cantdecide is offline
Registered User
FRC #5773 (YAFL Mechatronics)
Team Role: Programmer
 
Join Date: Jan 2016
Rookie Year: 2016
Location: Turkey
Posts: 31
cantdecide is an unknown quantity at this point
Re: Member function as Task

Thanks, that works.
Reply With Quote
  #4   Spotlight this post!  
Unread 14-02-2016, 11:13
euhlmann's Avatar
euhlmann euhlmann is offline
CTO, Programmer
AKA: Erik Uhlmann
FRC #2877 (LigerBots)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2015
Location: United States
Posts: 305
euhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud ofeuhlmann has much to be proud of
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
}
&MyClass::SomeFunction is a pointer to a member function.

Last edited by euhlmann : 14-02-2016 at 11:15.
Reply With Quote
  #5   Spotlight this post!  
Unread 14-02-2016, 14:33
AustinSchuh AustinSchuh is offline
Registered User
FRC #0971 (Spartan Robotics) #254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Feb 2005
Rookie Year: 1999
Location: Los Altos, CA
Posts: 800
AustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond reputeAustinSchuh has a reputation beyond repute
Re: Member function as Task

Quote:
Originally Posted by euhlmann View Post
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
}
&MyClass::SomeFunction is a pointer to a member function.
I'm not a very big fan of the Task class... There are some weird pieces of WPILib which have APIs which are trying to make Linux look like VxWorks, and Task is one of them.

You can do this in 1 line.

Code:
::std::thread my_thread(::std::bind(&MyClass::MyMethod, this, arg1, arg2));
If you want to delay starting the thread:

Code:
::std::thread my_thread;

my_thread = ::std::thread(::std::bind(&MyClass::MyMethod, this, arg1, arg2));
We use ::std::thread for all threads we start, and it works wonderfully.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 03:00.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi