Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Tasks in C++ (http://www.chiefdelphi.com/forums/showthread.php?t=113385)

AustinSchuh 03-02-2016 02:01 AM

Re: Tasks in C++
 
Quote:

Originally Posted by jreneew2 (Post 1544175)
(which I think is really just a wrapper for a pthread).

Task is a VXWorks API wrapper around pthreads. Any reason not to use pthreads directly?

Take a look at ::std::thread. It works well, and is much more cross platform. We use it exclusively.

taichichuan 04-02-2016 12:48 PM

Re: Tasks in C++
 
Quote:

Originally Posted by AustinSchuh (Post 1550046)
Task is a VXWorks API wrapper around pthreads. Any reason not to use pthreads directly?

Take a look at ::std::thread. It works well, and is much more cross platform. We use it exclusively.

Can you provide a simple example of using ::std::thread that's compatible with the roborio?

TIA,

Mike

AustinSchuh 04-02-2016 02:34 PM

Re: Tasks in C++
 
Quote:

Originally Posted by taichichuan (Post 1566454)
Can you provide a simple example of using ::std::thread that's compatible with the roborio?

TIA,

Mike

From our code:

Code:

-- snip --
    ::frc971::wpilib::GyroSender gyro_sender;
    ::std::thread gyro_thread(::std::ref(gyro_sender));

    //  Main loop here

    // Now cleanup
    gyro_sender.Quit();
    gyro_thread.join();

-- snip --

// Handles reading the gyro over SPI and sending out angles on a queue.
//
// This is designed to be passed into ::std::thread's constructor so it will run
// as a separate thread.
class GyroSender {
 public:
  GyroSender();

  // For ::std::thread to call.
  //
  // Initializes the gyro and then loops until Quit() is called taking readings.
  void operator()();

  void Quit() { run_ = false; }

 private:

  // Readings per second.
  static const int kReadingRate = 200;

  GyroInterface gyro_;

  ::std::atomic<bool> run_{true};
};

There are a bunch of different variants for how to create the ::std::function that ::std::thread takes for a constructor. A simpler version would be to define a void foo(){} and pass that in. You can also use ::std::bind to call arbitrary functions with arbitrary arguments.

taichichuan 04-02-2016 03:42 PM

Re: Tasks in C++
 
Quote:

Originally Posted by AustinSchuh (Post 1566487)
From our code:

Code:

-- snip --
    ::frc971::wpilib::GyroSender gyro_sender;
    ::std::thread gyro_thread(::std::ref(gyro_sender));

    //  Main loop here

    // Now cleanup
    gyro_sender.Quit();
    gyro_thread.join();

-- snip --

// Handles reading the gyro over SPI and sending out angles on a queue.
//
// This is designed to be passed into ::std::thread's constructor so it will run
// as a separate thread.
class GyroSender {
 public:
  GyroSender();

  // For ::std::thread to call.
  //
  // Initializes the gyro and then loops until Quit() is called taking readings.
  void operator()();

  void Quit() { run_ = false; }

 private:

  // Readings per second.
  static const int kReadingRate = 200;

  GyroInterface gyro_;

  ::std::atomic<bool> run_{true};
};

There are a bunch of different variants for how to create the ::std::function that ::std::thread takes for a constructor. A simpler version would be to define a void foo(){} and pass that in. You can also use ::std::bind to call arbitrary functions with arbitrary arguments.

Thanks!

Mike


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

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