Runnable implementation using packaged_task in c++11 - multithreading

I am trying to create a Runnable interface in c++11 using packaged_task, with child class overriding run() function. I don't know why this code is not compiling. Its giving error related to type argument.
/usr/include/c++/4.8.1/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of()>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
Below is my code snippet. Could someone plz give me some information on this error and whether implementing Runnable this way is a right way to proceed ?
class Runnable {
public:
explicit Runnable() {
task_ = std::packaged_task<int()>(&Runnable::run);
result_ = task_.get_future();
std::cout << "starting task" << std::endl;
}
virtual int run() = 0;
int getResult() {
task_();
return result_.get();
}
virtual ~Runnable() {
std::cout << "~Runnable()" << std::endl;
}
private:
std::future<int> result_;
std::packaged_task<int()> task_;
};
class foo : public Runnable {
int fib(int n) {
if (n < 3) return 1;
else return fib(n-1) + fib(n-2);
}
public:
explicit foo(int n) : n_(n) {}
int run() {
cout << "in foo run() " << endl;
int res = fib(n_);
cout << "done foo run(), res = " << res << endl;
return res;
}
~foo() {}
private:
int n_;
};
int main(int argc, char*argv[]) {
stringstream oss;
oss << argv[1];
int n;
oss >> n;
shared_ptr<foo> obj(new foo(n));
obj->run();
cout << "done main" << endl;
return 0;
}

Related

ability to run a packaged task with std::bind function parameters in a seperate thread via template function

The question i have is the line where i indicate ERROR below gives error as std::thread constructor works out the function to be invoked and requires
the parameter as needed by the function signature.
Is there any way to solve this ? if i attempt to decode the function name and argument list from packaged_task then i cant use the get_future function over packaged task and need to add my own promise/future code to handle this.
#include<iostream>
#include<string>
#include<thread>
#include<future>
#include<functional>
using namespace std;
int sampleAddFunction(const int& a, const int& b)
{
int sum = a + b;
cout << "sum = " << sum << endl;
return(sum);
}
template<typename T> T asyncExecutor(std::packaged_task<T(T, T)>&& package)
{
std::future<T> result = package.get_future();
std::thread task_td(std::move(package)); // ERROR here as the std::thread identifies the function name from package and requires the params to be passed. How to handle this ?
task_td.join();
return(result.get());
}
int main(int argc, char* argv[])
{
// Executing via directly calling through main.
int testResult1 = sampleAddFunction(100, 200);
cout << "testResult1 = " << testResult1 << endl;
// Attempt to create a std::packaged_task and then run it in another thread.
std::packaged_task<int(int,int)> task(std::bind(sampleAddFunction, 10, 20));
std::future<int> result = task.get_future();
std::thread t(std::move(task), 100, 200); // 100 and 200 are dummy parameters.
t.join();
int testResult2=result.get();
cout << "testResult2 = " << testResult2 << endl;
// Attempt to run this in seperate thread and get results.
std::packaged_task<int(int,int)> task2(std::bind(sampleAddFunction, 15, 27));
int testResult3 = asyncExecutor<int>(std::move(task2), 100, 200);
cout << "testResult3 = " << testResult3 << endl;
}
This should work.
#include<iostream>
#include<string>
#include<thread>
#include<future>
#include<functional>
using namespace std;
int sampleAddFunction(int a, int b)
{
int sum = a + b;
cout << "sum = " << sum << endl;
return(sum);
}
template<typename R, typename F, typename... Ts>
R asyncExecutor(F&& package, Ts... args)
{
std::future<R> result = package.get_future();
std::thread task_td(std::move(package), args...);
task_td.join();
return(result.get());
}
int main(int argc, char* argv[])
{
std::packaged_task<int(int,int)> task2(sampleAddFunction);
int testResult3 = asyncExecutor<int>(std::move(task2), 15, 27);
cout << "testResult3 = " << testResult3 << endl;
}
You are constructing a binary packaged_task (std::packaged_task<int(int,int)>) from a nullary function, the result of your bind (std::function<int()>).
You should either not use bind, or have asyncExecutor accept a nullary packaged_task (std::packaged_task<T()>)
int sampleAddFunction(const int& a, const int& b)
{
int sum = a + b;
cout << "sum = " << sum << endl;
return(sum);
}
template<typename T, typename ... ARGS> T asyncExecutor(std::packaged_task<T(ARGS...)>&& package, ARGS ... args)
{
std::future<T> result = package.get_future();
std::thread task_td(std::move(package), args...);
task_td.join();
return(result.get());
}
int main(int argc, char* argv[])
{
std::packaged_task<int()> task(std::bind(sampleAddFunction, 10, 20));
int testResult = asyncExecutor(std::move(task));
cout << "testResult = " << testResult << endl;
std::packaged_task<int(int,int)> task2(sampleAddFunction);
int testResult2 = asyncExecutor(std::move(task2), 15, 27);
cout << "testResult2 = " << testResult2 << endl;
}

async_ read_until doesn't work as expected

So I am trying to write a program that reads and writes data through a tcp socket. I can successfully accept a connection, write data to it (though the write handler doesn't work as expected?). I also want to read data through the same socket - which doesnt seem to be working.
The class that handles all this is as follows:
using namespace boost::asio;
using namespace boost::asio::ip;
TcpServer::TcpServer(unsigned short port = 1700)
: ipPort(port){
tcp::acceptor acc(svc, tcp::endpoint(tcp::v4(), ipPort));
acc.listen();
acc.async_accept(socket, boost::bind(&TcpServer::Accept_Handler,this, placeholders::error));
SAY("Waiting for a New connection");
svc.run();
}
void TcpServer::Write_Handler(const boost::system::error_code& ec,
std::size_t bytes_transferred){
std::cout << ec.message() << std::endl;
if (!ec)
{
std::cout << "Just sent " << yawData << std::endl;
}
}
void TcpServer::Read_Handler(const boost::system::error_code& ec,
std::size_t bytes_transferred){
if (!ec)
{
std::string line;
std::istream is(&input_buffer_);
std::string test;
is >> test;
std::cout << "test" << test << std::endl;
std::getline(is, line);
if (!line.empty())
{
std::cout << "Recieved: " << line << std::endl;
}
}
else
std::cout << "Error reading:" << ec.message() << std::endl;
}
void TcpServer::Accept_Handler(const boost::system::error_code& ec){
if (!ec)
{
std::cout << "Accepted a connection! - Now switching to write mode " << std::endl;
connectMode = 1;
}
}
void TcpServer::Write_Data(){
if (connectMode){
SAY("Sent data");
std::ostringstream ss;
std::string sendBuffer;
ss << std::fixed << std::setprecision(2);
ss << yawData;
sendBuffer = ss.str() + "\r";
async_write(socket, buffer(sendBuffer), boost::bind(&TcpServer::Write_Handler, this,
placeholders::error,
placeholders::bytes_transferred));
}
}
void TcpServer::UpdateYaw(double data) {
yawData = data;
}
void TcpServer::Read_Data(){
if (connectMode){
async_read_until(socket, input_buffer_, "\n" , boost::bind(&TcpServer::Read_Handler, this,
placeholders::error,
placeholders::bytes_transferred));
}
}
TcpServer::~TcpServer(){
svc.stop();
}
The class header goes as:
class TcpServer {
private:
io_service svc;
tcp::socket socket{svc};
double yawData = 0;
unsigned short ipPort;
bool connectMode = 0;
streambuf input_buffer_;
void Write_Handler(const boost::system::error_code&,
std::size_t);
void Read_Handler(const boost::system::error_code&,
std::size_t);
void Accept_Handler(const boost::system::error_code&);
public:
TcpServer(unsigned short );
void Write_Data();
void Read_Data();
void UpdateYaw(double);
~TcpServer();
};
To use this, I call Write_Data(), followed by Read_Data(). Write_Data works, but the write handler isn't called - I can recieve data on the client side.
Read_Data() doesn't work at all. I know for sure that Data is being sent through the socket in teh format needed (ends with "\n")
Any ideas on what could possibly be wrong or, any debugging tips?
Thanks
EDIT
I plan to run the write_data and read_data functions from my main function as follows:
TcpServer *socketObj = new TcpServer(1700);
while ( i < 100 && trackObj->ReadTrackingState() != 0) {
SAY("Current Yaw - %.02f", trackObj->CurrentYaw());
socketObj->UpdateYaw(trackObj->CurrentYaw());
socketObj->Write_Data();
socketObj->Read_Data();
Platform::sleepMillis(1000);
i++;
}
void TcpServer::Accept_Handler(const boost::system::error_code &ec) {
if (!ec) {
std::cout << "Accepted a connection! - Now switching to write mode " << std::endl;
connectMode = 1;
}
}
This function ends the async processing. It doesn't schedule any more async work and therefore io_service::run() ends, as documented.
You want to chain directly or use io_service::work to keep the service running. I suggest the chaining:
void TcpServer::Accept_Handler(const boost::system::error_code &ec) {
if (!ec) {
std::cout << "Accepted a connection! - Now switching to write mode " << std::endl;
Write_Data();
}
}
But...
HOLD ON
You'll want to carefully review all the code.
void TcpServer::Write_Data() {
SAY("Sent data");
std::ostringstream ss;
std::string sendBuffer;
ss << std::fixed << std::setprecision(2);
ss << yawData;
sendBuffer = ss.str() + "\r";
async_write(socket, buffer(sendBuffer),
boost::bind(&TcpServer::Write_Handler, this, placeholders::error, placeholders::bytes_transferred));
}
What's going on here? First you create a temporary stream, fail to use it to append the carriage-return, and then pass the reference to a local string to async_write... That can't work. It's Undefined Behaviour.
A fix:
void TcpServer::Write_Data() {
SAY("Send data");
std::ostream ss(&output_buffer_);
ss << std::fixed << std::setprecision(2) << yawData << "\r";
async_write(socket, output_buffer_,
boost::bind(&TcpServer::Write_Handler, this, placeholders::error, placeholders::bytes_transferred));
}
DEMO
Live On Coliru
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <iomanip>
using namespace boost::asio;
using namespace boost::asio::ip;
template <typename T>
static inline void SAY(T&& v) { std::cout << std::forward<T>(v) << "\n"; }
class TcpServer {
private:
io_service svc;
tcp::socket socket{ svc };
double yawData = 0;
unsigned short ipPort;
streambuf input_buffer_, output_buffer_;
void Write_Handler(const boost::system::error_code &, std::size_t);
void Read_Handler(const boost::system::error_code &, std::size_t);
void Accept_Handler(const boost::system::error_code &);
public:
TcpServer(unsigned short = 1700);
void Write_Data();
void Read_Data();
void UpdateYaw(double);
~TcpServer();
};
TcpServer::TcpServer(unsigned short port) : ipPort(port) {
tcp::acceptor acc(svc, tcp::endpoint(tcp::v4(), ipPort));
acc.listen();
acc.async_accept(socket, boost::bind(&TcpServer::Accept_Handler, this, placeholders::error));
SAY("Waiting for a New connection");
svc.run();
}
void TcpServer::Write_Handler(const boost::system::error_code &ec, std::size_t /*bytes_transferred*/) {
std::cout << ec.message() << std::endl;
if (!ec) {
std::cout << "Just sent " << yawData << std::endl;
Read_Data();
}
}
void TcpServer::Read_Handler(const boost::system::error_code &ec, std::size_t /*bytes_transferred*/) {
if (!ec) {
std::cout << "Recieved: " << &input_buffer_ << std::endl;
} else
std::cout << "Error reading:" << ec.message() << std::endl;
}
void TcpServer::Accept_Handler(const boost::system::error_code &ec) {
if (!ec) {
std::cout << "Accepted a connection! - Now switching to write mode " << std::endl;
Write_Data();
}
}
void TcpServer::Write_Data() {
SAY("Send data");
std::ostream ss(&output_buffer_);
ss << std::fixed << std::setprecision(2) << yawData << "\r";
async_write(socket, output_buffer_,
boost::bind(&TcpServer::Write_Handler, this, placeholders::error, placeholders::bytes_transferred));
}
void TcpServer::UpdateYaw(double data) { yawData = data; }
void TcpServer::Read_Data() {
async_read_until(socket, input_buffer_, "\n", boost::bind(&TcpServer::Read_Handler, this, placeholders::error,
placeholders::bytes_transferred));
}
TcpServer::~TcpServer() { svc.stop(); }
int main() {
TcpServer server;
}

std::thread context of execution (c++14)

The problem appear when an in/out variable of a function called by std::thread changes the value during the execution...
Function:
static int func(stThread_t *&pStThread)
Parameters
pStThread: It´s a struct that has a pointer to std::thread and other variables (some flags)
typedef struct stThread {
stThread() noexcept {...};
stThread(const stThread &cRigth) noexcept {...};
stThread & operator = (const stThread &cRigth) noexcept {...};
std::thread *pThread;
volatile bool bBegin;
volatile bool bEnd;
} stThread_t;
The function func print the address of the std::thread of the parameter
pStThread and the thread id
func before 1785280 this_id 21968
after making an this_thread::sleep for 2 seconds, it print it again
func afer ... this_id 21968
static int func(stThread_t *&pStThread) {
std::thread::id this_id = std::this_thread::get_id();
long long p_begin = (long long)pStThread;
std::cout << "func before " << std::to_string(p_begin) << " this_id " << this_id << "\n";
std::cout.flush();
pStThread->bBegin = true;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
this_id = std::this_thread::get_id();
long long p_end = (long long)pStThread;
std::cout << "func afer " << std::to_string(p_end) << " this_id " << this_id << "\n";
std::cout.flush();
pStThread->bEnd = true;
return 1;
};
The address of the pointer to std::thread it´s changed (corrutped, deleted..?)
The pStThread is pushing_back of a list of pointer struct stThread_t
std::list<stThread_t*> listOfThreads;
listOfThreads.push_back(pStThread);
I read about std::move, but does not work with pointers
At the end there is a thread "garbage collector" that it is trying to erase all the threads pending of execution.
Full code here
#include <string>
#include <list>
#include <vector>
#include <map>
#include <thread>
#include <mutex>
#include <atomic>
#include <iostream>
typedef struct stThread {
stThread() noexcept {
pThread = NULL;
bBegin = false;
bEnd = false;
};
stThread(const stThread &cRigth) noexcept {
this->pThread = cRigth.pThread;
this->bBegin = (bool)cRigth.bBegin;
this->bEnd = (bool)cRigth.bEnd;
};
stThread & operator = (const stThread &cRigth) noexcept {
this->pThread = cRigth.pThread;
this->bBegin = (bool)cRigth.bBegin;
this->bEnd = (bool)cRigth.bEnd;
return *this;
};
std::thread *pThread;
volatile bool bBegin;
volatile bool bEnd;
} stThread_t;
class CMain
{
public:
typedef std::list<stThread_t*> MyList_threads;
MyList_threads listOfThreads;
public:
CMain() {
std::cout << std::boolalpha << "Ex1 is move-constructible? "
<< std::is_move_constructible<stThread_t>::value << '\n'
<< "Ex1 is trivially move-constructible? "
<< std::is_trivially_move_constructible<stThread_t>::value << '\n'
<< "Ex1 is nothrow move-constructible? "
<< std::is_nothrow_move_constructible<stThread_t>::value << '\n'
<< "Ex2 is trivially move-constructible? "
<< std::is_trivially_move_constructible<stThread_t>::value << '\n'
<< "Ex2 is nothrow move-constructible? "
<< std::is_nothrow_move_constructible<stThread_t>::value << '\n';
};
static int func(stThread_t *&pStThread) {
std::thread::id this_id = std::this_thread::get_id();
long long p_begin = (long long)pStThread;
std::cout << "func before " << std::to_string(p_begin) << " this_id " << this_id << "\n";
std::cout.flush();
pStThread->bBegin = true;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
this_id = std::this_thread::get_id();
long long p_end = (long long)pStThread;
std::cout << "func afer " << std::to_string(p_end) << " this_id " << this_id << "\n";
std::cout.flush();
pStThread->bEnd = true;
return 1;
};
int _createThreads() {
for (int iIdx = 0; (iIdx < 5); iIdx++) {
stThread_t *pStThread = new stThread_t;
pStThread->pThread = new std::thread(&CMain::func,
std::ref(pStThread));
if (pStThread) {
do {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
} while (!pStThread->bBegin);
listOfThreads.push_back(pStThread);
std::string sLog;
sLog = "\nlistOfThreads.push_back " + std::to_string((long long)pStThread) + "\n";
std::cout << sLog;
std::cout.flush();
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return 1;
};
int _main() {
_createThreads();
std::thread thread_collector([=]() {
bool bEnd = false;
MyList_threads::iterator it;
it = listOfThreads.end();
do {
stThread_t *pStThread = NULL;
if (it == listOfThreads.end()) {
it = listOfThreads.begin();
if (it == listOfThreads.end()) bEnd = true;
}
else it++;
if (it != listOfThreads.end()) {
if ((*it)->bEnd) {
pStThread = *it;
listOfThreads.erase(it);
it = listOfThreads.begin();
}
}
if (pStThread) {
if (pStThread->pThread) {
if (pStThread->pThread->joinable()) {
pStThread->pThread->join();
std::cout << " element deleted " << std::to_string((long long)pStThread) << "\n";
std::cout.flush();
}
delete pStThread->pThread;
pStThread->pThread = NULL;
}
delete pStThread;
}
pStThread = NULL;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
} while (!bEnd);
});
if (thread_collector.joinable()) {
thread_collector.join();
}
return 1;
};
};
int main()
{
CMain _main;
_main._main();
return 0;
}
You have a rather straightforward bug, that is mostly unrelated to threading:
(1) func takes a reference to an stThread_t*.
static int func(stThread_t *&pStThread);
(2) You pass in a reference to pStThread
std::thread(&CMain::func,std::ref(pStThread));
(3) Which is a local variable whose lifetime ends as soon as the loop iteration is finished
for (int iIdx = 0; (iIdx < 5); iIdx++) {
stThread_t *pStThread = new stThread_t;
//...
}
(4) And consequently, you get undefined behavior when your function tries to access the object after it is destroyed. (be careful! here, "the object" refers to the pointer in question, not the object the pointer is pointing to)
It's unclear why you insist on passing pStThread by reference; your function doesn't actually modify the pointer (just what's pointed to), and you don't seem to intend to do any of the things that such a device is actually good for.

Making changes to future object

I am trying to change the behavior of a future object based on user input.
#include <iostream>
#include <future>
//=======================================================================================!
struct DoWork
{
DoWork(int cycles, int restTime) : _cycles(cycles), _restTime(restTime), _stop(false)
{
}
void operator () ()
{
for(int i = 0 ; i < _cycles; ++i)
{
std::this_thread::sleep_for(std::chrono::milliseconds(_restTime));
if(_stop)break;
doTask();
}
}
void stop()
{
_stop = true;
}
private:
void doTask()
{
std::cout << "doing task!" << std::endl;
}
private:
int _cycles;
int _restTime;
bool _stop;
};
//=======================================================================================!
int main()
{
DoWork doObj(50, 500);
std::future<int> f = std::async(std::launch::async, doObj);
std::cout << "Should I stop work ?" << std::endl;
std::cout << "('1' = Yes, '2' = no, 'any other' = maybe)" << std::endl;
int answer;
std::cin >> answer;
if(answer == 1) doObj.stop();
std::cout << f.get() << std::endl;
return 0;
}
//=======================================================================================!
However this does not stop the execution of the future object. How do I change the behavior of the doObj after I have created the future object?
You have a few problems. First, your function object doesn't actually return int, so std::async will return a std::future<void>. You can fix this either by actually returning int from DoWork::operator(), or by storing the result from async in a std::future<void> and not trying to print it.
Second, std::async copies its arguments if they aren't in reference wrappers, so the doObj on the stack is not going to be the same instance of DoWork that is being used by the asynchronous thread. You can correct this by passing doObj in a reference wrapper a la std::async(std::launch::async, std::ref(doObj)).
Third, both the main thread and the asynchronous thread are simultaneously accessing DoWork::_stop. This is a data race and means the program has undefined behavior. The fix is to protect accesses to _stop with a std::mutex or to make it a std::atomic.
Altogether, program should look like (Live at Coliru):
#include <iostream>
#include <future>
//=======================================================================================!
struct DoWork
{
DoWork(int cycles, int restTime) : _cycles(cycles), _restTime(restTime), _stop(false)
{
}
int operator () ()
{
for(int i = 0 ; i < _cycles; ++i)
{
std::this_thread::sleep_for(std::chrono::milliseconds(_restTime));
if(_stop) return 42;
doTask();
}
return 13;
}
void stop()
{
_stop = true;
}
private:
void doTask()
{
std::cout << "doing task!" << std::endl;
}
private:
int _cycles;
int _restTime;
std::atomic<bool> _stop;
};
//=======================================================================================!
int main()
{
DoWork doObj(50, 500);
std::future<int> f = std::async(std::launch::async, std::ref(doObj));
std::cout << "Should I stop work ?" << std::endl;
std::cout << "('1' = Yes, '2' = no, 'any other' = maybe)" << std::endl;
int answer;
std::cin >> answer;
if(answer == 1) doObj.stop();
std::cout << f.get() << std::endl;
}
//=======================================================================================!

C++ passing an object to a function, the operator= is not called

So here is the code snippet:
class MyClass { public: MyClass(char chIn) { std::cout <<
"Constructor!" << std::endl; }
MyClass & operator= (char chIn) { std::cout << "Assigment
operator!" << std::endl; } } ;
void Func(MyClass objIn) { return; }
int _tmain(int argc, _TCHAR* argv[]) { Func('T'); system("PAUSE");
return 0; }
In the upper example the constructor of the object is called!!!! Why is this behavior? Shouldn't the assigment operator be called? Because we're assigning a value to the function parameter, aren't we?
operator= invoked for already existent object otherwise constructor(or copy constructor) is used to create needed instance

Resources