// I asked this question
// https://stackoverflow.com/questions/61026135/asio-use-future-instead-of-yieldec
// and comments lead to need to modify code of answer and put in in this new
// question.
// I tried to ask questions in form of code trials and causes of writing them
// or how i under stand them
// asio_packaged_task.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#define BOOST_COROUTINES_NO_DEPRECATION_WARNING
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/use_future.hpp>
#include <boost/bind.hpp>
#include <iostream>
using boost::system::error_code;
namespace asio = boost::asio;
template <typename Token>
auto async_meaning_of_life(bool success, Token&& token) {
#if BOOST_VERSION >= 106600
using result_type =
typename asio::async_result<std::decay_t<Token>, void(error_code, int)>;
typename result_type::completion_handler_type handler(
std::forward<Token>(token));
result_type result(handler);
#else
typename asio::handler_type<Token, void(error_code, int)>::type handler(
std::forward<Token>(token));
asio::async_result<decltype(handler)> result(handler);
#endif
if (success)
handler(error_code{}, 42); // 4-18-2020 this line happens when
// async_meaning_of_life work is done,this
// line is calling the handler and passing it
// the result of async_meaning_of_life
// function which here for simplicity are
// supplied as error_code{} and 42
else
handler(asio::error::operation_aborted, 0);
return result.get();
}
void using_yield_ec(asio::yield_context yield) {
for (bool success : { true, false }) {
boost::system::error_code ec;
auto answer = async_meaning_of_life(success, yield[ec]);
std::cout << __FUNCTION__ << ": Result: " << ec.message() << "\n";
std::cout << __FUNCTION__ << ": Answer: " << answer << "\n";
}
}
void using_yield_catch(asio::yield_context yield) {
for (bool success : { true, false })
try {
auto answer = async_meaning_of_life(success, yield);
std::cout << __FUNCTION__ << ": Answer: " << answer << "\n";
} catch (boost::system::system_error const& e) {
std::cout << __FUNCTION__ << ": Caught: " << e.code().message()
<< "\n";
}
}
// 4-18-2020 something interesting happens here,when we call the function
// using_future or using_handler in the same thread we get into these two
// functions then inside them we call async_meaning_of_life which is an
// initiating function ,the async_meaning_of_life has two parts: its code which
// ends before if(success) then it calls the completion token passed to it which
// is promise OR lambda "it might be fuction object ,functor,function pointer, "
// using handler(error,42) where handler represents the true handler type
// according to the token passed to function. then it returns the result by
// result.get to using_future or using_handler. inside using handler we notice
// that code returns back to lambda after handler(error,42) .if completion token
// were bind or function object,we would have seen code jumping to bound
// function or function object
void using_future() {
for (bool success : { true, false })
try {
auto answer = async_meaning_of_life(success, asio::use_future);
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
} catch (boost::system::system_error const& e) {
std::cout << __FUNCTION__ << ": Caught: " << e.code().message()
<< "\n";
}
}
void using_handler() {
for (bool success : { true, false })
async_meaning_of_life(success, [](error_code ec, int answer) {
std::cout << "using_handler: Result: " << ec.message() << "\n";
std::cout << "using_handler: Answer: " << answer << "\n";
});
}
void print(const boost::system::error_code& /*e*/) {
std::cout << "Hello, world!" << std::endl;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename Token>
auto async_meaning_of_life_composed(bool success, Token&& token) {
#if BOOST_VERSION >= 106600
using result_type =
typename asio::async_result<std::decay_t<Token>, void(error_code, int)>;
typename result_type::completion_handler_type handler(
std::forward<Token>(token));
result_type result(handler);
#else
typename asio::handler_type<Token, void(error_code, int)>::type handler(
std::forward<Token>(token));
asio::async_result<decltype(handler)> result(handler);
#endif
// here i will add intermediate initiating functions
async_meaning_of_life(success, [](error_code ec, int answer) {
std::cout << "using_handler: Result: " << ec.message() << "\n";
std::cout << "using_handler: Answer: " << answer << "\n";
});
try {
auto answer = async_meaning_of_life(success, asio::use_future);
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
} catch (boost::system::system_error const& e) {
std::cout << __FUNCTION__ << ": Caught: " << e.code().message() << "\n";
}
// using_yield_ec(asio::yield_context yield);
// spawn(svc, using_yield_ec);
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
if (success)
handler(error_code{}, 42); // 4-18-2020 this line happens when
// async_meaning_of_life work is done,this
// line is calling the handler and passing it
// the result of async_meaning_of_life
// function which here for simplicity are
// supplied as error_code{} and 42
else
handler(asio::error::operation_aborted, 0);
return result.get();
}
void using_future_composed() {
for (bool success : { true, false })
try {
auto answer =
async_meaning_of_life_composed(success, asio::use_future);
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
} catch (boost::system::system_error const& e) {
std::cout << __FUNCTION__ << ": Caught: " << e.code().message()
<< "\n";
}
}
int main() {
asio::io_service svc;
boost::asio::steady_timer t(svc, boost::asio::chrono::seconds(45));
// this function returns immediately and make new thread
t.async_wait(&print);
// this function returns immediately>>>>also it adds 1 out standing work to
// svc.is async_wait body runned in main threaed OR in another thread????if
// it is ran in another thread,how immediate return happens"not
// blocking"??why async_meaning is not returning immediately like
// async_wait?
auto answer = async_meaning_of_life(true, asio::use_future);
// this function does not return immediately and is executing in main thread
// >>>>>how can we make it behave like async_wait???? first
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
svc.post([]() { // this adds 1 outstanding work to svc and does not start
auto answer = async_meaning_of_life(true, asio::use_future);
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
});
svc.post(using_future);
// this increase outstanding work by 1
// boost::asio::yield_context yield;
// 4-18-2020 this is only used with spawn ,if you want to use stakeful
// coroutines,use push and pull types of coroutine "i wonder how to do
// this???"
// using_yield_ec( yield);this is wrong usage
// using_yield_catch( yield);this is wrong usage
// using_future();this is normal usage but it does not return immediately
// and it executes in main thread.
// using_handler();
svc.post(using_future_composed);
spawn(svc, using_yield_ec);
// this adds 2 outstanding work to svc why 2 works are made while we are
// launching one function????
spawn(svc, using_yield_catch);
// what i think i understand about mechanism of work of spawn:spawn is
// called from main thread>>>>it is just used with coroutines taking
// yield_context as argument,spawn post function to service,spawn makes link
// between the context in which service will be ran"may be main thread or
// new thread AND the context of coroutine function ran in same thread as
// service" or may be the coroutine makes new thread in which it is
// running???".Then when svc.run is called,svc calls task"here svc is caller
// and coroutine is callee",task is executing,yield is called as completion
// token"can we call yield outside initiating function to switch to caller
// "here caller is svc"????. then we are now in svc context which calls
// another task .....
// t.async_wait(&using_future);wrong usage leading to error?why can not in
// use using_future function as completion callback with async_wait???
// spawn(svc, using_future);wrong usage as using_future is not coroutine?
std::thread work([] {
using_future();
using_handler();
auto answer = async_meaning_of_life(true, asio::use_future);
// this function does not return immediately and is executing in main
// thread >>>>>how can we make it behave like async_wait???? first
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
});
std::thread work_io([&] { // this starts new thread in which svc is run
svc.run();
});
svc.run(); // this run svc in main thread
// general question:
/*
using_* is considered normal function or coroutine OR composed operation??
async_meaning is considered initiating function?
why does not it return immediately when ran in main thread?how can we make
it return immediately and then when certain result is present ,it calls its
callback??
async_wait is considered initiating function? why does it return
immediately then when timer expires ,it calls back its completion token??
can i make the following composed operation:
i will make composed operation which returns future to caller thread,
and inside it i shall call another composed operation with coroutine,
*/
work.join();
work_io.join();
}
boost::asio::steady_timer t(svc, boost::asio::chrono::seconds(45));
// this function returns immediately and make new thread
No it doesn't create a new thread. It just constructs a service object (the timer) and returns. obviously immediately, like std::string s("hello"); returns when the string is constructed.
t.async_wait(&print);
// this function returns immediately>>>>also it adds 1 out standing work to
// svc. is async_wait body runned in main threaed OR in another thread????if
// it is ran in another thread,how immediate return happens"not
// blocking"??why async_meaning is not returning immediately like
// async_wait?
Slow down.
is async_wait body runned in main threaed OR in another thread?
It's just a function. It runs on the current thread. Like when you called printf.
if it is ran in another thread, how immediate return happens "not
blocking"?
Well, it's not in another thread. But if it were, then it would be obvious how
it would return "not blocking": because the work is not happening on the
current thread.
Why async_meaning_of_life is not returning immediately like
async_wait?
It is returning immediately.
Now, a bit subtler: Even if you use it with a yield_context (inside a
coroutine). It will return immediately and cause the the coroutine to yield.
This means that other tasks get a chance to run on the service thread(s) and
only when the async operation completed, the coroutine will be resumed. From
the point of view of the coroutine, it will have appeared as if the call was
blocking. This is the whole point of (stackful) coroutines. It "abstracts away"
the asynchrony.
So, yes, async_meaning_of_life always (always) returns (almost) immediately.
svc.post([]() { // this adds 1 outstanding work to svc and does not start
Correct. Use a {poll|run}[_one,_for,_until] function to run tasks².
auto answer = async_meaning_of_life(true, asio::use_future);
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
You don't ask anything here, but using a future just to await it immediately is
an anti-pattern¹. It's absolutely useless, as it will always generate blocking
behaviour.
You should store the future somewhere, do other work and then when you need the result of the future (and it may or may not have already been completed) you await it (.get() You should store the future somewhere, do other work and then when you need the result of the future (and it may or may not have already been completed) you await it (e.g. by invoking .get()).
// using_yield_ec( yield);this is wrong usage
// using_yield_catch( yield);this is wrong usage
Correct. Used correctly, the Asio service will provide a yield context for you.
// boost::asio::yield_context yield;
// 4-18-2020 this is only used with spawn ,if you want to use stakeful
// coroutines,use push and pull types of coroutine "i wonder how to do
// this???"
No idea. Just refer to the documentation of Boost Coroutine (I suggest Boost Coroutine2). This is off-topic for Asio async operations.
// using_future();this is normal usage but it does not return immediately
// and it executes in main thread.
Well, duh. You took it from a minimal example that ONLY shows the mechanics of the different async_result tokens.
Just refer to a few lines above:
You should store the future somewhere, do other work and then when you need
the result of the future (and it may or may not have already been completed)
you await it (.get() You should store the future somewhere, do other work
and then when you need the result of the future (and it may or may not have
already been completed) you await it (e.g. by invoking .get()).
svc.post(using_future_composed);
Again, I see no questions, but I don't think it means you understand it. I tread.
I see using_future_composed is basically using_future but calling async_meaning_of_life_composed instead.
Now looking at async_meaning_of_life_composed I have no idea what that's supposed to do. It looks like async_meaning_of_life with random lines of code added, doing all kinds of things including blocking operations (see anti-pattern¹) in a function that is supposed to schedule a async operation only.
That's just not what you want to do. Ever.
spawn(svc, using_yield_ec);
// this adds 2 outstanding work to svc why 2 works are made while we are
// launching one function????
Honestly, I do not know. I assume it's because the launch of the coro itself is posted onto the work queue, so it runs exception-safely from one of the worker threads.
The bigger point here is that you still haven't actually started any io-workers, see [²] above.
spawn(svc, using_yield_catch);
// what i think i understand about mechanism of work of spawn:spawn is
// called from main thread>>>>it is just used with coroutines taking
// yield_context as argument,spawn post function to service,spawn makes link
// between the context in which service will be ran"may be main thread or
// new thread AND the context of coroutine function ran in same thread as
// service"...
Erm, basically, yes.
// ... or may be the coroutine makes new thread in which it is
// running???" ...
Definitely not. Both Coroutines and Asio are a device/framework to arrive at concurrency without necessarily multi-threading. Coroutine will never create a thread. Asio will typically not create any threads (unless to implement certain kinds of services on some platforms, but they'd be implementation-details and your tasks/handlers will never run on such a hidden thread).
// ... .Then when svc.run is called,svc calls task"here svc is caller
// and coroutine is callee",task is executing,yield is called as completion
// token"can we call yield outside initiating function to switch to caller
// "here caller is svc"????. then we are now in svc context which calls
// another task .....
Huh. No, yield_context is not a portal to a different time-space continuum.
I'm not very sure what you mean with `'call yield' so when you are thinking about calling it from outside the initiating function, I'd say: probably don't do that.
// t.async_wait(&using_future);wrong usage leading to error?why can not in
// use using_future function as completion callback with async_wait???
Because it doesn't satisfy the handler requirements for steady_time::async_wait (which should take a boost::system::error_code only. Did you perhaps mean use_future (from Asio) instead of your own using_future?
auto ignored_future = t.async_wait(boost::asio::use_future);
I admit the names are somewhat confusing. If it helps, rename all the using_XYZ functions to demonstration_using_XYZ.
// spawn(svc, using_future);wrong usage as using_future is not coroutine?
You got that right.
std::thread work([]
using_future();
using_handler();
auto answer = async_meaning_of_life(true, asio::use_future);
// this function does not return immediately and is executing in main
// thread >>>>>how can we make it behave like async_wait???? first
std::cout << __FUNCTION__ << ": Answer: " << answer.get() << "\n";
});
I believe you just copy/pasted the comment, but in case you really worried: no that is not run on the main thread. It's run on the work thread, and yes, that's because you block on the future::get(). See above¹.
std::thread work_io([&] { // this starts new thread in which svc is run
svc.run();
});
Better late than never :)
svc.run(); // this run svc in main thread
Correct, and running more doesn't hurt. Running the service on multiple threads may require handler synchronization: Why do I need strand per connection when using boost::asio?
// general question:
/*
using_* is considered normal function or coroutine OR composed operation??
Normal functions (see the clarification about renaming it to demonstration_using_XYZ above)
async_meaning is considered initiating function?
Correct.
why does not it return immediately when ran in main thread?
It does. See above. If you mean, why does your own function async_meaning_of_life_composed bblock? That's because you made it do blocking operations (see above).
how can we make
it return immediately and then when certain result is present ,it calls its
callback??
The usual way to do to it, is by launching other async operations. Say, for example, you wait for network operation to complete (asynchronously, e.g. using boost::asio::async_write) and when it's done, you invoke the handler. The async_result helper makes it so you don't have to know the actual completion_handler_type, and it will "magically" do the right thing regardless of how your initiating function was invoked.
async_wait is considered initiating function? why does it return
immediately then when timer expires ,it calls back its completion token??
Because that's how async operations are designed. They were designed that way because that is useful behaviour.
can i make the following composed operation:
i will make composed operation which returns future to caller thread,
and inside it i shall call another composed operation with coroutine,
*/
You are free to start a coroutine. Just make sure you transfer ownership of the async_result result so you can invoke the handler from there, to signal completion of your operation.
In the case of futures, the usual way to compose operations is by composing futures, like: https://www.boost.org/doc/libs/1_72_0/doc/html/thread/synchronization.html#thread.synchronization.futures.then
std::string someotheroperation(int);
future<int> fut1 = foo();
future<std::string> fut2 = foo().then(someotheroperation);
BONUS
The ultimate piece of documentation on writing Composed Operations with Asio is (ironically) this page in the Beast documentation. Perhaps seeing some more real-life examples may give you more ideas.
Keep in mind Beast comes with a few facilities that make library maintenance for /them/ a bit easier, but could well be overkill for your own application. Then again, if you err on theur path you will not overlook important things like the one we discussed here earlier:
Related
I am converting some of my code from a SC_THREAD to a SC_METHOD. My question is, do I need to stop using the sc_fifo class? I realize an SC_METHOD should not call sc_fifo.write() because this uses a wait call which is not allowed for functions that cannot be suspended. However, sc_fifo provides non-blocking versions of various functions and potentially I could use these instead. Some of the documentation I've read indicates you should never use sc_fifo from a SC_METHOD at all but provided no justification.
Here is a sample of code I am currently using.
class Example : public sc_module {
public:
sc_fifo<int> myFifo;
sc_in<bool> clock_in;
SC_HAS_PROCESS(Example);
// constructor
Example(sc_module_name name) : sc_module(name) {
SC_METHOD(read);
sensitive << clock_in;
}
void read() {
int value = -1;
bool success = myFifo.nb_read(value);
if (success) { cout << "Read value " << value << endl; }
else { cout << "No read done but that's okay." << endl; }
}
};
int sc_main(int argc, char* argv[]) {
sc_clock clock("clock");
Example example("example");
example.clock_in(clock);
sc_start(10, SC_NS);
return 0;
}
This throws no errors even though I am calling an sc_fifo function from a SC_METHOD. Is it bad policy to use nb_read() from inside a SC_METHOD? If so why?
Using sc_fifo non-blocking calls from SC_METHOD should be fine.
I have not found any place in standard manual that prohibits it.
Neither nb_read, nor nb_write, as their names suggest, call wait internally so it's fine to use them from an SC_METHOD.
While your example code works, it's rather inefficient when things are put into the fifo infrequently. If you want your code to be more event driven, you could make the SC_METHOD sensitive to sc_fifo.data_written_event(); then it will only be called when something is actually written to the fifo (though it's still a good idea to check that nb_read returns true in case something else pulled from the same fifo). Of course, this would skip your "No read done but that's okay." prints.
Also, I think the title of your question probably meant to ask about calling nb_write from SC_METHOD rather than SC_THREAD.
I have this situation where I need to have an async_read operation "prepared" for reading before I call a custom function which is sending something on the counter-part of the websocket. I have the completion handler of the async_read wrapped in the same strand to which I post my functor.
The problem is that sometimes on::read is not called, so basically I think that the writeFromCounterpart() is called before the ws_1 was in "reading" state.
My understanding is that strand is guaranteeing the order between completion handlers, but I don't understand if guarantees that the async_* operation is "ready" (running in its thread and reading) before continuing with other operation from the strand FIFO.
Full code bellow: When running it, most of the time I've seen the following output:
on_read called
Run 2 Handlers
I was able to see the following also a few times (~1 in 20 in stress condition),
on_write called
Run 1 Handlers
I never seen both on_write and on_read
Code:
#include <boost/asio.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/core.hpp>
#include <string>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
namespace websocket = boost::beast::websocket; // from <boost/beast/websocket.hpp>
boost::beast::multi_buffer buffer;
boost::asio::io_context ioc_1;
boost::asio::io_context ioc_2;
websocket::stream<tcp::socket> ws_1(ioc_1);
websocket::stream<tcp::socket> ws_2(ioc_2);
void on_write(boost::system::error_code, std::size_t) {
std::cout << "on_write called" << std::endl << std::flush;
}
void writeFromCounterpart() {
ws_2.async_write(boost::asio::buffer(std::string("Hello")), on_write);
}
void on_read(boost::system::error_code, std::size_t) {
std::cout << "on_read called" <<std::endl << std::flush;
}
int main() {
std::thread t([](){
auto const address = boost::asio::ip::make_address("127.0.0.1");
tcp::acceptor acceptor{ioc_2, {address, 30000}};
tcp::socket socket{ioc_2};
acceptor.accept(socket);
websocket::stream<tcp::socket> ws{std::move(socket)};
ws.accept();
ws_2 = std::move(ws);
ioc_2.run();
});
t.detach();
// allow acceptor to accept
std::this_thread::sleep_for(std::chrono::milliseconds(200));
tcp::resolver resolver_(ioc_1);
boost::asio::io_context::strand strand_(ioc_1);
auto const results = resolver_.resolve("127.0.0.1", "30000");
boost::asio::connect(ws_1.next_layer(), results.begin(), results.end());
ws_1.handshake("127.0.0.1", "/");
ws_1.async_read(buffer, strand_.wrap(on_read));
strand_.post(writeFromCounterpart);
auto handlers = ioc_1.run_for(std::chrono::milliseconds(5000));
std::cout << "Run " + std::to_string(handlers) + " Handlers" << std::endl << std::flush;
}
The short answer is: yes.
The documentation for it is here:
https://www.boost.org/doc/libs/1_68_0/doc/html/boost_asio/reference/io_context__strand.html#boost_asio.reference.io_context__strand.order_of_handler_invocation
The problem is that sometimes when I run this flow, on::read is not called, so basically I think that the SendSomethingOnTheWs is called before the ws_ was in "reading" state.
Why would that matter? Streaming sockets (ws or otherwise) behave like streams. If the socket was open the data will just be buffered.
TL;DR: Does the __block attribute on an std::vector prevent RVO in Objective-C++?
In Modern C++, the canonical way to return a vector from a function is to just return it by value so that return value optimization can be used if possible. In Objective-C++, this appears to work the same way.
- (void)fetchPeople {
std::vector<Person> people = [self readPeopleFromDatabase];
}
- (std::vector<Person>)readPeopleFromDatabase {
std::vector<Person> people;
people.emplace_back(...);
people.emplace_back(...);
// No copy is made here.
return people;
}
However, if the __block attribute is applied to the second vector, then it appears that a copy of the vector is being created when it returns. Here is a slightly contrived example:
- (std::vector<Person>)readPeopleFromDatabase {
// __block is needed to allow the vector to be modified.
__block std::vector<Person> people;
void (^block)() = ^ {
people.emplace_back(...);
people.emplace_back(...);
};
block();
#if 1
// This appears to require a copy.
return people;
#else
// This does not require a copy.
return std::move(people);
#endif
}
There are plenty of Stack Overflow questions that explicitly state that you don't need to use std::move when returning a vector because that will prevent copy elision from taking place.
However, this Stack Overflow question states that there are, indeed, some times when you do need to explicitly use std::move when copy elision is not possible.
Is the use of __block in Objective-C++ one of those times when copy elision is not possible and std::move should be used instead? My profiling appears to confirm that, but I'd love a more authoritative explanation.
(This is on Xcode 10 with C++17 support.)
I don't know about authoritative, but a __block variable is specifically designed to be able to outlive the scope it's in and contains special runtime state that tracks whether it's stack- or heap-backed. For example:
#include <iostream>
#include <dispatch/dispatch.h>
using std::cerr; using std::endl;
struct destruct_logger
{
destruct_logger()
{}
destruct_logger(const destruct_logger& rhs)
{
cerr << "destruct_logger copy constructor: " << &rhs << " --> " << this << endl;
}
void dummy() {}
~destruct_logger()
{
cerr << "~destruct_logger on " << this << endl;
}
};
void my_function()
{
__block destruct_logger logger;
cerr << "Calling dispatch_after, &logger = " << &logger << endl;
dispatch_after(
dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(),
^{
cerr << "Block firing\n";
logger.dummy();
});
cerr << "dispatch_after returned: &logger = " << &logger << endl;
}
int main(int argc, const char * argv[])
{
my_function();
cerr << "my_function() returned\n";
dispatch_main();
return 0;
}
If I run that code, I get the following output:
Calling dispatch_after, &logger = 0x7fff5fbff718
destruct_logger copy constructor: 0x7fff5fbff718 --> 0x100504700
dispatch_after returned: &logger = 0x100504700
~destruct_logger on 0x7fff5fbff718
my_function() returned
Block firing
~destruct_logger on 0x100504700
There's quite a lot happening here:
Before we call dispatch_after, logger is still stack-based. (0x7fff… address)
dispatch_after internally performs a Block_copy() of the block which captures logger. This means the logger variable must now be moved to the heap. As it's a C++ object, this means the copy constructor is invoked.
And indeed, after dispatch_after returns, &logger now evaluates to the new (heap) address.
The original stack instance of course must be destroyed.
The heap instance is only destroyed once the capturing block has been destroyed.
So a __block "variable" is actually a much more complex object that can move around in memory on demand behind the scenes.
If you were to subsequently return logger from my_function, RVO wouldn't be possible, because (a) it now lives on the heap, not the stack, and (b) not making a copy on returning would allow mutation of the instance captured by blocks.
I guess it might be possible to make it runtime state dependent - use RVO memory for stack-backing, then if it gets moved to the heap, copy back into the return value when the function returns. But this would complicate functions that operate on blocks, as the backing state would now need to be stored separately from the variable. It also seems like overly complex and surprising behaviour, so I'm not surprised that RVO doesn't happen for __block variables.
I wrote this test for DataSelection Event in my ternary plot in Techlog, and it gets too much data.
What am I doing wrong here?
In run method – subscribe to event:
void SetupTernaryPlot::run()
{
workspace.connect(Workspace::SelectionChanged, this, SLOT(onSelectionChanged(const Slb::Techlog::SelectionChangedArgs&)));
}
This is my event handler:
void SetupTernaryPlot::onSelectionChanged(const Slb::Techlog::SelectionChangedArgs args)
{
qWarning() << "TernaryPlot::onInteractiveSelectionChanged";
int i=0;
foreach (Selection selection, args.selectionsChanged())
{
qWarning() << "Interactive Selection " << i++ << selection.dataset().referenceVariable().rowCount() << endl; //selectlist;
}
}
My event handler is getting too many points. How can I know what has been selected?
It is indeed possible to achieve this, even though the solution is not very elegant at this point.
If you study the "setuplogview" sample (SetupLogview::onSelectionChanged) in the package, you can find that you may use using -1 to filter out non-selected indices.
(If you have access to WAT, there is a ticket created to enhance this https://wat.grabels-fr0235.slb.com/techlog/ticket/24300)
This is my first ever post here, so please excuse any formatting issues.
I have an interactive program which spawns external processes and monitors their IO. Things work fine until I spawn something off with "mpiexec", after which STDIN appears to break.
I realize this will be difficult to reproduce for most folks, but if anyone sees anything obvious or knows of this problem.... please help!
Here's a snippet:
int main( ... )
{
std::string choice;
while(std::getline(std::cin,choice)){
if(!choice.empty()){
if(choice == "Parallel"){
system("mpiexec ./aprogram");
}
if(choice == "Serial"){
system("./aprogram");
}
// Now the external process is done... so far, so good
std::cout << "Program is done. Press ENTER to continue."
<< std::endl;
// This next line *works* if the external process was serial
// But *fails* when "mpiexec" was invoked
std::getline(std::cin,choice);
if(std::cin.eof()){
std::cout << "STDIN has been closed." << std::endl;
exit(1);
}
}
}
}
I have tried lots of various things, e.g. pipes, explicit forking, meticulous descriptor management. The weirdest thing is that if I dup off and save stdin and then restore it after "mpiexec" returns, then I no longer get EOF on std::cin, but instead, std::getline(std::cin,...) no longer blocks! The program goes into an infinite loop reading zero bytes off std::cin in the std::readline call.
If, while the external process is running under mpiexec, I stack a bunch of data into std::cin (e.g. by typing), then subsequent calls to std::readline correctly parse the lines of data that I have stuck in there, but again... once it is done reading through that data, it just keeps going in an infinite loop (i.e. not blocking on std::readline(std::cin,..) even if there is no data to read! Ugh. So annoying.
Any help is deeply appreciated.
Cheers!
I think I fixed your problem, for me either a call to Serial or Parallel blocked, and I think it was the std::cin.eof() test,
std::getline(std::cin,choice);
if(std::cin.eof()){
td::cout << "STDIN has been closed." << std::endl;
exit(1);
}
However, changing this to std::cin.get(), works great for both the parallel run and serial run.
if(std::cin.get()) {
std::cout << "STDIN has been closed." << std::endl;
exit(1);
}
Works on my system.