std::async is not working in c++11, linux platform - c++

bool tf()
{
sleep(5000);
return true;
}
int main()
{
std::future<bool> bb = std::async(std::launch::async, tf);
bool b = false;
while(1)
{
if(b == true) break;
b = bb.get();
}
return 0;
}
why don't work?
I intended to terminate program after 5 seconds. However, the program is freezing.

There is a much better alternative to the direct use of invoking a global sleep. Use the <chrono> header and the string literals it provides together with std::this_thread::sleep_for. This is less error prone, e.g.
#include <chrono>
// Bring the literals into the scope:
using namespace std::chrono_literals;
bool tf()
{
std::this_thread::sleep_for(5s);
// ^^ Awesome! How readable is this?!
return true;
}
Together with the rest of the snippet you posted, this should work as intended.

Related

Calling a function which is inside a Thread

I was doing Unit Testing and finding code coverage using gtest and lcov.
My function is
void MyClass::MyFunction(const std::string& argument1, const std::string& argument2) {
std::thread([this, argument1, argument2]() {
std::unique_lock<std::mutex> live_lock_(live_mutex_);
int64_t time_stamp = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
int64_t time_to_live = myList[argument1] - (time_stamp % myList[argument1]);
cond_var_.wait_for(time_to_live_lock_, std::chrono::milliseconds(time_to_live), [this, argument1] { return cond_var_status_[argument1]; });
if (message_master_.find(argument1) != message_master_.end()) {
//something
}
}).detach();
std::cout<< "end line is executed"<<std::endl; }
and my test function is
TEST(test, test1) {
Myclass testObj;
testObj.MyFunction("arg1", "arg2");
ASSERT_EQ("","");
};
When I run the test, all codes except those inside thread are executed.
So is there any solution to call those codes inside thread too?
How do you know none of those lines are being executed?
I would suggest making a test program, compiling with -g and -Wall, then using gdb to make sure a thread is actually being created. If it is, step through the code.
It could be that the compiler gets rid of code it thinks is doing nothing.
This is how I would test it (I modified the code a little, but in principle it is the same thing. All the globals shall be moved to MyClass most probably):
#include <atomic>
#include <condition_variable>
#include <map>
#include <mutex>
#include <thread>
std::mutex time_to_live_mutex_;
std::map<std::string, int64_t> myList;
std::map<std::string, bool> cond_var_status_;
std::map<std::string, std::string> incoming_message_master_;
std::condition_variable cond_var_;
std::atomic_bool something_happened{false};
void foo(const std::string& argument1, const std::string& argument2) {
std::thread([argument1, argument2]() {
std::unique_lock<std::mutex> time_to_live_lock_(time_to_live_mutex_);
int64_t time_stamp = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
int64_t time_to_live = myList[argument1] - (time_stamp % myList[argument1]);
cond_var_.wait_for(time_to_live_lock_, std::chrono::milliseconds(time_to_live), [argument1] { return cond_var_status_[argument1]; });
if (incoming_message_master_.find(argument1) != incoming_message_master_.end()) {
//something
something_happened = true;
}
}).detach();
}
TEST(TestFoo, testing_foo_thread) {
int64_t timeout = 10000;
myList["bar"] = timeout; // so that cond variable waits long time
cond_var_status_["bar"] = false; // so that cond variable returns false at first try
incoming_message_master_["bar"] = std::string("42"); // so that something can happen
foo("bar", "fiz");
ASSERT_FALSE(something_happened); // cond variable is still waiting at this point
{
std::unique_lock<std::mutex> time_to_live_lock_(time_to_live_mutex_);
cond_var_status_["bar"] = true; // so that cond variable returns eventually
}
while(!something_happened && timeout > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
timeout -= 10;
}
ASSERT_TRUE(something_happened); // cond variable is not waiting, should have returned
}

In C++ leave scope when error occurs without exceptions

In bash we can say:
(
set -e
function OnError { caller | { read line file; echo "in $file:$line" >&2; }; }
trap OnError ERR ## catch exception
echo hello ## step 1
false
echo "never come here"
)
# continue here
Every command returns exit code. Flag -e tells bash to check every result and exit if it is non-zero.
Exceptions in C++ provides similar logic:
#include <iostream>
using namespace std;
void step1(){
cout<<"hello"<<endl;
}
void step2(){
throw std::runtime_error("step2 always fail");
}
void step3(){
cout<<"never come here"<<endl;
}
int main(){
try{
step1();
step2(); // throws
step3(); // never come here
}catch(...){
cerr<<"caught error"<<endl;
}
// continue here
}
And this works pretty same. But need additional manipulations to detect from where exception was thrown.
Often C++ developers refuse using exceptions with -fno-exceptions. And code become looking like C – need to check result for every action.
#include <iostream>
using namespace std;
int step1(){
cout<<"hello"<<endl;
return 0;
}
int step2(){
return -1;
}
int step3(){
cout<<"never come here"<<endl;
return 0;
}
#define CHECK(err,msg) \
if(err){ \
cerr<<"error in "<<msg<<endl; \
break; \
}
int main(){
while(0){
CHECK(step1(),"step1");
CHECK(step2(),"step2");
CHECK(step3(),"step3");
}
// continue here;
}
And that looks slightly cumbersome. But here we can directly track file:line.
I would like to have clean code without exceptions. Like:
#include <iostream>
using namespace std;
enum Result {SUCCESS,FAIL};
Result step1(){
cout<<"hello"<<endl;
return SUCCESS;
}
Result step2(){
return FAIL;
}
Result step3(){
cout<<"never come here"<<endl;
return SUCCESS;
}
int main(){
{
step1(); // success
step2(); // fail , interrupt execution and go out of scope
step3(); // never come here
}
// continue here
}
How to achieve same behavior as in bash keeping code simple and and clean without exceptions?
You can (ab)use the fact that C++ uses lazy evaluation for checking boolean logic:
#include <iostream>
using namespace std;
bool step1(){
cout<<"hello"<<endl;
return true;
}
bool step2(){
return false;
}
bool step3(){
cout<<"never come here"<<endl;
return true;
}
bool executeSteps() {
return step1() && step2() && step3();
}
int main(){
executeSteps();
}
Since step2() returns false, the whole condition cannot evaluate to true, so the rest of it isn't even checked.
You don't even need a separate function for that (although ignoring the result of boolean calculations might be confusing to the readers):
int main(){
step1() && step2() && step3();
}
There are several implementations for such approach:
From https://buckaroo.pm/posts/error-handling-in-cpp:
loopperfect/neitherstd::expectedBoost Outcomebeark/ftl
and also https://github.com/TartanLlama/expected, imo the best.

Threading in c++

Lets say I want to take an input from user and perform a search in a text file for that input. The search will be performed for every character user inputs. There will be a loop performing search and there will be another loop to check if new character is input by the user. Second loop will restart the first loop if new char is given by the user.
Please just explain how to perform above with c++. I think threads need to be created.
Below variables will be used to maintain common values:
static var`
bool change;
while(!change)
{
change=false
<<do something, like search in file>>
}
Other loop will be like below:
while(1)
{
if(user enters another char)
{
var=new value input by the user;
change=true;
}
else change=false;
}
Thanks!
Something like this? Now I wrote this on ideone and their threads didn't work for me so I wasn't able to test it but yeah.. Something close to this should work. Probably a bad example. A thread pool would be best.
#include <iostream>
#include <thread>
#include <atomic>
#include <queue>
#include <mutex>
#include <chrono>
std::mutex lock;
std::atomic<bool> stop(false);
std::queue<std::function<void()>> jobs;
void One()
{
while(!stop)
{
if (!jobs.empty())
{
if (lock.try_lock())
{
std::function<void()> job = jobs.front();
jobs.pop();
lock.unlock();
job();
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
void Two()
{
std::string var;
while(true)
{
if (std::cin>> var)
{
std::lock_guard<std::mutex> glock(lock);
jobs.push([] {std::cout<<"Task added to the queue..\n";});
}
else
break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
int main()
{
std::thread T(One);
Two();
stop = true;
T.join();
return 0;
}
Create two threads: one for reading user-input, and another for performing the search.
Use a binary-semaphore in order to synchronize between the two threads in a Consumer-Producer manner, i.e., one thread acquires the semaphore and the other thread releases it:
static BinarySemaphore binSem;
static int inputCharacter = 0;
static void ReadUserInput();
static void PerformSearch();
void Run()
{
BinarySemaphore_Init(&binSem,0);
CreateThread(ReadUserInput,LOWER_PRIORITY);
CreateThread(PerformSearch,HIGHER_PRIORITY);
}
static void ReadUserInput()
{
while (inputCharacter != '\n')
{
inputCharacter = getc(stdin);
BinarySemaphore_Set(&binSem);
}
}
static void PerformSearch()
{
while (inputCharacter != '\n')
{
BinarySemaphore_Get(&binSem,WAIT_FOREVER);
// <<do something, like search in file>>
}
}
Please note that you need to create the thread which performs the search, with priority higher than that of the thread which reads user-input (as in the code above).

Using a C++11 condition variable in VS2012

I can't get code working reliably in a simple VS2012 console application consisting of a producer and consumer that uses a C++11 condition variable. I am aiming at producing a small reliable program (to use as the basis for a more complex program) that uses the 3 argument wait_for method or perhaps the wait_until method from code I have gathered at these websites:
condition_variable:
wait_for,
wait_until
I'd like to use the 3 argument wait_for with a predicate like below except it will need to use a class member variable to be most useful to me later. I am receiving "Access violation writing location 0x__" or "An invalid parameter was passed to a service or function" as errors after only about a minute of running.
Would steady_clock and the 2 argument wait_until be sufficient to replace the 3 argument wait_for? I've also tried this without success.
Can someone show how to get the code below to run indefinitely with no bugs or weird behavior with either changes in wall-clock time from daylight savings time or Internet time synchronizations?
A link to reliable sample code could be just as helpful.
// ConditionVariable.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <condition_variable>
#include <mutex>
#include <thread>
#include <iostream>
#include <queue>
#include <chrono>
#include <atomic>
#define TEST1
std::atomic<int>
//int
qcount = 0; //= ATOMIC_VAR_INIT(0);
int _tmain(int argc, _TCHAR* argv[])
{
std::queue<int> produced_nums;
std::mutex m;
std::condition_variable cond_var;
bool notified = false;
unsigned int count = 0;
std::thread producer([&]() {
int i = 0;
while (1) {
std::this_thread::sleep_for(std::chrono::microseconds(1500));
std::unique_lock<std::mutex> lock(m);
produced_nums.push(i);
notified = true;
qcount = produced_nums.size();
cond_var.notify_one();
i++;
}
cond_var.notify_one();
});
std::thread consumer([&]() {
std::unique_lock<std::mutex> lock(m);
while (1) {
#ifdef TEST1
// Version 1
if (cond_var.wait_for(
lock,
std::chrono::microseconds(1000),
[&]()->bool { return qcount != 0; }))
{
if ((count++ % 1000) == 0)
std::cout << "consuming " << produced_nums.front () << '\n';
produced_nums.pop();
qcount = produced_nums.size();
notified = false;
}
#else
// Version 2
std::chrono::steady_clock::time_point timeout1 =
std::chrono::steady_clock::now() +
//std::chrono::system_clock::now() +
std::chrono::milliseconds(1);
while (qcount == 0)//(!notified)
{
if (cond_var.wait_until(lock, timeout1) == std::cv_status::timeout)
break;
}
if (qcount > 0)
{
if ((count++ % 1000) == 0)
std::cout << "consuming " << produced_nums.front() << '\n';
produced_nums.pop();
qcount = produced_nums.size();
notified = false;
}
#endif
}
});
while (1);
return 0;
}
Visual Studio Desktop Express had 1 important update which it installed and Windows Update has no other important updates. I'm using Windows 7 32-bit.
Sadly, this is actually a bug in VS2012's implementation of condition_variable, and the fix will not be patched in. You'll have to upgrade to VS2013 when it's released.
See:
http://connect.microsoft.com/VisualStudio/feedback/details/762560
First of all, while using condition_variables I personally prefer some wrapper classes like AutoResetEvent from C#:
struct AutoResetEvent
{
typedef std::unique_lock<std::mutex> Lock;
AutoResetEvent(bool state = false) :
state(state)
{ }
void Set()
{
auto lock = AcquireLock();
state = true;
variable.notify_one();
}
void Reset()
{
auto lock = AcquireLock();
state = false;
}
void Wait(Lock& lock)
{
variable.wait(lock, [this] () { return this->state; });
state = false;
}
void Wait()
{
auto lock = AcquireLock();
Wait(lock);
}
Lock AcquireLock()
{
return Lock(mutex);
}
private:
bool state;
std::condition_variable variable;
std::mutex mutex;
};
This may not be the same behavior as C# type or may not be as efficient as it should be but it gets things done for me.
Second, when I need to implement a producing/consuming idiom I try to use a concurrent queue implementation (eg. tbb queue) or write a one for myself. But you should also consider making things right by using Active Object Pattern. But for simple solution we can use this:
template<typename T>
struct ProductionQueue
{
ProductionQueue()
{ }
void Enqueue(const T& value)
{
{
auto lock = event.AcquireLock();
q.push(value);
}
event.Set();
}
std::size_t GetCount()
{
auto lock = event.AcquireLock();
return q.size();
}
T Dequeue()
{
auto lock = event.AcquireLock();
event.Wait(lock);
T value = q.front();
q.pop();
return value;
}
private:
AutoResetEvent event;
std::queue<T> q;
};
This class has some exception safety issues and misses const-ness on the methods but like I said, for a simple solution this should fit.
So as a result your modified code looks like this:
int main(int argc, char* argv[])
{
ProductionQueue<int> produced_nums;
unsigned int count = 0;
std::thread producer([&]() {
int i = 0;
while (1) {
std::this_thread::sleep_for(std::chrono::microseconds(1500));
produced_nums.Enqueue(i);
qcount = produced_nums.GetCount();
i++;
}
});
std::thread consumer([&]() {
while (1) {
int item = produced_nums.Dequeue();
{
if ((count++ % 1000) == 0)
std::cout << "consuming " << item << '\n';
qcount = produced_nums.GetCount();
}
}
});
producer.join();
consumer.join();
return 0;
}

Continuations/Coroutines/Generators in C++ / gcc / linux

Background: I'm trying to figure out how to implement continuations/coroutines/generators (whatever the following is called) by posing this toy problem. The environment is C++11 on gcc 4.6 and linux 3.0 x86_64. Non-portable is fine but using an external library (boost.coroutine, COROUTINE, etc) is not allowed. I think longjmp(3) and/or makecontext(2) and friends may help but not sure.
Description:
The following toy parser is supposed to parse sequences of as and bs of equal length. ie
((a+)(b+))+
such that the length of the second bracketed production equals the third.
When it finds a production (eg aaabbb) it outputs the number of as it finds (eg 3).
Code:
#include <stdlib.h>
#include <iostream>
using namespace std;
const char* s;
void yield()
{
// TODO: no data, return from produce
abort();
}
void advance()
{
s++;
if (*s == 0)
yield();
}
void consume()
{
while (true)
{
int i = 0;
while (*s == 'a')
{
i++;
advance();
}
cout << i << " ";
while (i-- > 0)
{
if (*s != 'b')
abort();
advance();
}
}
}
void produce(const char* s_)
{
s = s_;
// TODO: data available, continue into consume()
consume();
}
int main()
{
produce("aaab");
produce("bba");
produce("baa");
produce("aabbb");
produce("b");
// should print: 3 1 4
return 0;
}
Problem:
As you can see the state of the consume call stack must be saved when yield is called and then produce returns. When produce is called again, consume must be restarted by returning from yield. The challenge would be to modify the way produce calls consume, and implement yield so they function as intended.
(Obviously reimplementing consume so that it saves and rebuilds its state defeats the purpose of the exercise.)
I think what needs to be done is something like the example on the bottom of the makecontext man page: http://www.kernel.org/doc/man-pages/online/pages/man3/makecontext.3.html, but its not clear how to translate it onto this problem. (and I need sleep)
Solution:
(Thanks to Chris Dodd for design)
#include <stdlib.h>
#include <iostream>
#include <ucontext.h>
using namespace std;
const char* s;
ucontext_t main_context, consume_context;
void yield()
{
swapcontext(&consume_context, &main_context);
}
void advance()
{
s++;
if (*s == 0)
yield();
}
void consume()
{
while (true)
{
int i = 0;
while (*s == 'a')
{
i++;
advance();
}
cout << i << " ";
while (i-- > 0)
{
advance();
}
}
}
void produce(const char* s_)
{
s = s_;
swapcontext(&main_context, &consume_context);
}
int main()
{
char consume_stack[4096];
getcontext(&consume_context);
consume_context.uc_stack.ss_sp = consume_stack;
consume_context.uc_stack.ss_size = sizeof(consume_stack);
makecontext(&consume_context, consume, 0);
produce("aaab");
produce("bba");
produce("baa");
produce("aabbb");
produce("b");
// should print: 3 1 4
return 0;
}
Its fairly straight-forward to use makecontext/swapcontext for this -- you use makecontext to create a new coroutine context and swapcontext to swap between them. In you case, you need one additional coroutine to run the consume infinite loop, and you run main and produce in the main context.
So main should call getcontext+makecontext to create a new context that will run the consume loop:
getcontext(&consume_ctxt);
// set up stack in consume_context
makecontext(&consume_ctxt, consume, 0);
and then produce will switch to it instead of calling consume directly:
void produce(const char* s_)
{
s = s_;
swapcontext(&main_ctxt, &consume_ctxt);
}
and finally yield just calls swapcontext(&consume_ctxt, &main_ctxt); to switch back to the main context (which will continue in produce and immediately return).
Note that since consume is an infinite loop, you don't need to worry too much about what happens when it returns (so the link will never be used)