Go to Post I just weighed myself and I have gained two pounds since last Saturday. Thats a sign of a good robotics team. - russell [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 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 09:52.

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