How to do async call without blocking on c++? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
I have a code on c++
void func()
{
int rc = thisDoneIn10sec();
return;
}
Function thisDoneIn10sec() takes long time to finish :)
I want to launch somehow this function and finish func after that without waiting this time. I dont need in rc variable.
Searching on answer I tried to do advices from here Why should I use std::async?
Firstly, I rewrite my code to this.
#include <future>
void func()
{
auto fut = std::async(std::launch::async,thisDoneIn10sec);
fut.get();
return;
}
But, behavior is still sequentical.
In the answer of that question I see, that if I need the result of operation, I have to block and wait on result.
Maybe compiler decides, that I need the result, because I am launching int function, so I have to rewrite thisDoneIn10sec to void thisDoneIn10sec(), or the issue is in the fut.get() part?
If you have any ideas, I will be glad for any help.

Related

Emscripten_set_main_loop not working -- usleep undefined? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 15 days ago.
Improve this question
My project is compiling and opening up on the web but the main loop is not working. The error seems to be that usleep is undefined. I'm not sure how I can correct this. As per the error message, I seem to be dividing by zero (undefined). I tried removing 'fps' but no luck.
#define emscripten_set_main_loop(func,fps,simulateInfiniteLoop)
while (1) { func(); usleep(1000000/fps); }
Expands to:
while (1) { loop(); usleep(1000000/0); }
identifier "usleep" is undefined C/C++(20)
Problem solved! I switched over to using emscripten_set_main_loop_arg, put all my variables in a struct, and rewrote my functions to accept pointers to the struct and deal appropriately.

How to make program waiting in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to make program wait for some time until continue. I tried: system("stop") but console output was:
" 'stop' is not recognized as an internal or external command, operable
program or batch file.". I need answer in c++. Answers in Is there a decent wait function in C++? doesn't work.
Following makes your program stop. At least for the next year or so. Roughly. Bonus: Its portable and doesn't drain your laptop's battery.
#include <chrono>
#include <thread>
int main() {
using namespace std::chrono_literals;
std::this_thread::sleep_for(8760h);
return 0;
}

Best way to call a function C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Calling functions into another function or even main is what my question is about.
One way to call a function is :
callOne(1, 2);
Another way to call a function is :
callTwo = func2(5, 9);
Please explain what is the difference? Is one way better than the other? Which way is most encouraged to use?
It depends on the return value of the function.
If this is void the function does not have a return value and you do the first.
callOne(1, 2);
If it has something else like int, char or std::string you can use the first but also the second.
callTwo = func2(5, 9);
In this case you save the value the function returns in the variable.
If you use the first variant on a function with return value (not void) the result will be ignored.
I would advice you to google for some basic tutorials like this or this for example. You won't be very fast if you let SO teach you basic things.

How to suspend and resume a Process and Thread in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i wan't to suspend and resume main function is there any way except sleep method. Please Help
Assuming you have other threads running apart from main, you can use use a sem_wait (semaphore initialized to 0) in main() and then from your thread you can call sem_post whenever you want main() to run.
Read about semaphore and usage:

potential issue in this code using osstreamstring [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is there any issue in the following code? I am told it's there, but I couldn't find it...
std::string fun(int i)
{
std::ostringstream t;
t<<"My int is "<<i<<returnSomething();
return t.str();
}
The code in the question is fine. It would be a problem if you returned a pointer into the local object (say that you returned a const char* obtained as t.str().c_str()) or if you returned a reference. But in your code, a copy of the internal string in the std::ostringstream is performed before the function completes (as part of the return statement), and before t gets destroyed, so it is fine.