Boost thread v1.53 segmentation fault - c++11

The following program produces a segmentation fault, although I don't see any undefined behaviour in the code. It has been compiled with GCC 4.7.3. Do you know the reason of the fault or a possible work-around? Also, it seems boost::future does not exist in v1.53 yet, so I should probably rely on boost::unique_future. I cannot upgrade to any version above > 1.53 and I really need the "make_ready_at_thread_exit()" feature.
#define BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>
namespace th = boost;
struct S {
th::packaged_task<void()> task;
th::unique_future<void> future;
void start();
void stop();
};
void S::start() {
task = th::packaged_task<void()>{ [this] () {}};
future = task.get_future();
task.make_ready_at_thread_exit();
}
void S::stop() {
future.wait();
}
int main() {
S s;
s.start();
s.stop();
}

Related

Trying to implement Observer Patterns and got Segmentation Fault (pls help) c++

I'm trying to learn c++ so, I try to implement the observer patterns from the book Game Progamming Patterns but I'm always getting Segmentation Fault.
Searching arround I saw that Segmentation fault happens when a program try to access a non allocated memory. So I tried hard to fix it, but I can't. Someone can help me?
Here is my code:
Observer.h
#ifndef OBSERVER_H
#define OBSERVER_H
#include "EntityAndEvent.h"
#include "Subjects.h"
class Observer
{
private:
Observer* next_;
public:
Observer()
: next_(nullptr)
{}
virtual void onNotify(const Entity& entity, Event::Type event) = 0;
// Other stuff...
friend class Subjects;
};
#endif
Achievements.cpp
#ifndef ACHIEVEMENTS_CPP
#define ACHIEVEMENTS_CPP
#include "Observer.h"
#include <vector>
class Achievements : public Observer
{
public:
enum Type{
FELL_OFF,
AWAKE_ON
};
private:
std::vector<Type> done{};
public:
virtual void onNotify(const Entity&, Event::Type);
void unlock(Achievements::Type);
void printDone();
};
void Achievements::onNotify(const Entity& entity, const Event::Type event)
{
switch (event)
{
case Event::Type::ENTITY_FELL:
if(entity.isHero())
unlock(Achievements::Type::FELL_OFF);
break;
case Event::Type::ENTITY_AWAKE:
if(entity.isHero())
unlock(Achievements::Type::AWAKE_ON);
break;
default:
break;
}
};
void Achievements::unlock(Achievements::Type achiev)
{
done.push_back(achiev);
}
void Achievements::printDone()
{
// assert(done.size());
for(size_t i{0}; i < done.size(); i++)
{
std::cout << done[i] << "\n";
}
};
EntityAndEvent.cpp (i create this just to work with observer and subjects)
#ifndef ENTITY_AND_EVENT_H
#define ENTITY_AND_EVENT_H
class Entity
{
public:
inline const bool isHero() const {return true;}
};
class Event
{
public:
enum Type{
ENTITY_FELL,
ENTITY_AWAKE
};
};
#endif
Subjects.h
#ifndef SUBJECT_H
#define SUBJECT_H
#include "Observer.h"
#include <vector>
class Observer;
class Subjects
{
private:
// when implements this, prefer to use linked list or another optimized algorithms
Observer* head_{};
protected:
void notify(const Entity& Entity, Event::Type event);
public:
Subjects() : head_(NULL)
{};
~Subjects() {}
void addObserver(Observer* observer);
// const int getNumObs() const {return num_obs_;}
};
#endif
Subjects.cpp
#include "Subjects.h"
#include <cassert>
void Subjects::notify(const Entity& entity, Event::Type event)
{
Observer* observer = head_;
while (observer != NULL)
{
observer->onNotify(entity, event);
observer = observer->next_;
};
};
void Subjects::addObserver(Observer* observer)
{
if (head_ != nullptr)
observer->next_ = head_;
head_ = observer;
};
Physics.cpp (i create this just to populates the subject)
#ifndef PHYSICS_H
#define PHYSICS_H
#include "Subjects.h"
#include "EntityAndEvent.h"
class Physics: public Subjects
{
private:
public:
void update(const Entity& entity)
{
if (entity.isHero())
{
notify(entity, Event::Type::ENTITY_AWAKE);
};
};
};
#endif
main.cpp
#include "Observer.h"
#include "Achievements.h"
#include "Achievements.cpp"
#include "EntityAndEvent.h"
#include "Physics.h"
#include "Subjects.h"
#include "Subjects.cpp"
int main()
{
Physics *p{};
Entity e{};
Achievements *achiev{};
p->addObserver(achiev);
p->update(e);
return 0;
}
With Physics *p{}; you are declaring a pointer, but it currently does not point to a valid object (it is being initialized as nullptr here). Therefore, calling p->addObserver results in a segmentation fault. You should be able to confirm this by running the program in a debugger.
Why are you using a pointer here? Change Physics *p{}; to Physics p{};.
Disclaimer: Since your code is definitely not a minimum example I didn't look into the whole code or even tried to compile it. I'm only posting an answer because there is a clear error in the main function (use of an invalid pointer) that results in a segmentation fault.
first, tks for the reply. #darcamo that works. I dont know why it was declared as pointer.
I fixed that, so if anyone else have the same doubt check below:
main.cpp
#include "Observer.h"
#include "Achievements.h"
#include "EntityAndEvent.h"
#include "Physics.h"
#include "Subjects.h"
int main()
{
Physics p{};
Entity e{};
Achievements achiev{};
Observer* ptr_achiev{&achiev};
p.addObserver(ptr_achiev);
p.update(e);
return 0;
}

Cant initialise thread in Xcode , C++11

I am trying to implement spsc_queue of boost.
But initialising thread throws error. I cant use both std::thread as well as boost thread.
sharedQueue.hpp
`
#include <stdio.h>
#include <iostream>
#include <queue>
#include <thread>
#include <mutex>
#include <boost/thread.hpp>
#include <boost/lockfree/spsc_queue.hpp>
using namespace std;
class sharedQueue
{
boost::lockfree::spsc_queue<int> lockFreeQ{100};
std::queue<int> comQue;
int head =0;;
int tail = 0;
public:
sharedQueue();
std::mutex lockForQueue;
void write(int writeValue);
int read();
void startTesting();
void TestWrite(int MaxElement);
void lockFreeProduce();
void lockFreeConsume();
void TestLockFreeQueue();
};`
Following is the sharedQueue.cpp
#include "sharedQueue.hpp"
int sharedQueue :: read(){
int readValue;
lockForQueue.lock();
if(!(comQue.empty()))
{
readValue = comQue.front();
comQue.pop();
}
lockForQueue.unlock();
return readValue;
}
void sharedQueue :: write(int writeValue){
lockForQueue.lock();
comQue.push(writeValue);
tail++;
lockForQueue.unlock();
}
void sharedQueue:: startTesting(){
std::cout<<"Size of the que --"<<comQue.size()<<std::endl;
}
void sharedQueue:: TestWrite(int maxEle ){
for(int i = 0 ; i < maxEle; i ++){
write(i);
}
}
void sharedQueue::lockFreeProduce(){
for(int i = 0; i < 10; i++){
cout <<“Produced-- "<< i<<endl;
lockFreeQ.push(i);
}
}
void sharedQueue::lockFreeConsume(){
for(int i = 0; i <10; i++){
lockFreeQ.front();
cout << “ Consume-- "<<lockFreeQ.pop();
}
}
void sharedQueue:: TestLockFreeQueue(){
std::thread t1(lockFreeProduce);
std::thread t2(lockFreeConsume);
t1.join();
t2.join();
} `
I am using Xcode. I have tried changing
C++ Language dialect to c++11 from GNU++11
Standard Library to libc++11 from libstdC++
Please help.
Where am I doing wrong?
You are trying to run a member function as a new thread, not a plain old function. The syntax for member function is different.
void sharedQueue:: TestLockFreeQueue(){
std::thread t1(std::bind(&sharedQueue::lockFreeProduce, this));
std::thread t2(std::bind(&sharedQueue::lockFreeConsume, this));
t1.join();
t2.join();
}
Below answer assumes that we are talking about non-static member function. static member function behaves kind of same way as that of a normal function pointer.
A member function pointer is complex than a plain old function pointer and it cannot be invoked in standalone manner i.e it can only be called when there is an object instance of that class.
See this for an example and read this for better understanding of member function pointers.
An easier way to do it i.e instead of using bind to create a callable object is to use a lambda, C++11 onwards and you should prefer lambda over bind whenever and however possible.
Your example using a lambda:
void sharedQueue:: TestLockFreeQueue(){
std::thread t1([this]() { this->lockFreeProduce(); });
std::thread t2([this]() { this->lockFreeConsume(); });
t1.join();
t2.join();
}
Here I am passing a lambda to the constructor of the thread which creates an anonymous functor structure. The square bracket [...] is the capture list which copies this pointer so that it can be used inside a lambda.
More about lambda can be found here and here.

Arduino 1.6.9/1.610 build fails with "'constexprint' does not name a type"

Supposedly Arduino's IDE > 1.6.2 has C++11 support.
I have just freshly downloaded and run version 1.6.9 on OSX (and as others have reported, this repros on Windows as well, with 1.6.9/1.6.10).
I cannot get this simple program to compile:
constexpr int get_five() { return 5; }
void setup() {
Serial.begin(9600);
Serial.println(get_five());
}
void loop() {
}
I receive this error when I try to build or upload:
sketch_jul25a:1: error: 'constexprint' does not name a type
constexpr int get_five() { return 5; }
^
exit status 1
'constexprint' does not name a type
I've looked at this question and answer, but it is supposedly no longer applicable in 1.6.9 version of the IDE that I am using - error: 'constexpr' does not name a type m- arduino ide
I have dug into the temporary files that are output by the IDE when building, and it seems it is trying to automatically generate headers for functions (I assume for multi-file sketch support), and does the wrong thing when it encounters constexpr:
#include <Arduino.h>
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
constexprint get_five(); // **** <- This looks to be the culprit
#line 3 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
void setup();
#line 9 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
void loop();
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
constexpr int get_five() { return 5; }
void setup() {
Serial.begin(9600);
Serial.println(get_five());
}
void loop() {
}
Is this a bug in the Arduino IDE? Is it unique to OSX? Is there a workaround that allows constexpr to work?
In googling I have found that some Arduino libraries are using constexpr, so I am assuming it could be made to work in some cases.
This is a known limitation of the arduino-builder.
Until it is fixed, you can add a prototype yourself above the function. This will prevent the IDE from incorrectly generating its own.
constexpr int get_five();
constexpr int get_five() { return 5; }
void setup() {
Serial.begin(9600);
Serial.println(get_five());
}
void loop() {
}

How to write a C++ API class that will allow a user to register its own callback functions?

Let's say a user links his app against a library I wrote and I want to let him specify a callback function that I will call whenever an error occurs in my library. The implementation below works but I want to double check that I'm not missing something here:
Thread safety
DLL initialization issues
Public API considerations (I'm giving away a reference to an instance from the DLL is that OK?)
Anything that could be done better to hide implementation details from the public API?
errordispatcher.h:
#pragma once
#include <functional>
#include <memory>
#include <string>
namespace WE
{
class ErrorDispatcher
{
public:
ErrorDispatcher()
{}
explicit ErrorDispatcher(std::function<void(std::string)> user_func)
: error_callback_func{user_func}
{}
virtual ~ErrorDispatcher(){}
static ErrorDispatcher& getInstance()
{
return instance_;
}
void setErrorCallback(std::function<void(std::string)> user_func)
{
error_callback_func = nullptr;
if (user_func)
error_callback_func = user_func;
}
void dispatchError(std::string message)
{
if (error_callback_func)
error_callback_func(message);
}
private:
explicit ErrorDispatcher(const ErrorDispatcher&) = delete;
explicit ErrorDispatcher(ErrorDispatcher&&) = delete;
ErrorDispatcher& operator = (const ErrorDispatcher&) = delete;
ErrorDispatcher& operator = (ErrorDispatcher&&) = delete;
static ErrorDispatcher instance_;
std::function<void(std::string)> error_callback_func = nullptr;
};
}
NOTE: above I have inline implementation details in the public header to make this post shorter but they will be moved to a .cpp and won't be part of the public header
errordispatcher.cpp:
#include "errordispatcher.h"
namespace WE
{
ErrorDispatcher ErrorDispatcher::instance_;
}
apitest.h
namespace WE
{
void dllFunctionThatMightGiveError();
}
apitest.cpp
#include "errordispatcher.h"
#include "apitest.h"
namespace WE
{
void dllFunctionThatMightGiveError()
{
// Some error happens in dll so call user function and give a message to the user!
ErrorDispatcher::getInstance().dispatchError("Error in DLL!");
}
}
main.cpp (USER APP)
#include "errordispatcher.h"
#include "apitest.h"
#include <iostream>
#include <string>
void error_callback(std::string message)
{
std::cout << message << "\n";
}
int main(void)
{
WE::ErrorDispatcher::getInstance().setErrorCallback(error_callback);
WE::ErrorDispatcher::getInstance().dispatchError("Error in APP!");
WE::dllFunctionThatMightGiveError();
return 0;
}
Output is:
Error in APP!
Error in DLL!

.dll error on boost compiled app

simple app, just learning to setup my boost environment...
program compiles, but I get a libboost_thread-mgw44-mt-1_51.dll is missing error
on a sidenote, I had to change thread1.stop() to t1.stop() as boost said there was no function for stop().
I have the dll, placing the dll in the same folder as the app doesn't do anything.
#include <boost/bind.hpp>
#include <boost/thread.hpp>
class Threadable
{
public:
void stop()
{
running = false;
}
int run()
{
running = true;
while(running)
{
//Do Something Meaningful
}
return 0;
}
private:
bool running;
};
int main()
{
Threadable t1, t2;
boost::thread thread1(boost::bind(&Threadable::run, t1));
boost::thread thread2(boost::bind(&Threadable::run, t2));
// Let the threads run for half a second
boost::this_thread::sleep(boost::posix_time::milliseconds(500));
// Signal them to stop
t1.stop();
t2.stop();
//thread1.stop();
//thread2.stop();
// Wait for them to gracefully exit
thread1.join();
thread2.join();
return 0;
}

Resources