I have an application that makes a number of calls to an external library. One of these calls is not thread-safe so everything will crash if I don't guard it somehow.
I tried to use the following pattern, i.e. wrapping a mutex inside a shared_ptr. Note that this class may be instantiated at many different places in the application from different threads.
class MyClass
{
public:
MyClass(const std::shared_ptr<std::mutex>& mutex) :
_mutex(mutex)
{
}
MyClass(MyClass&& other) :
_mutex(other._mutex)
{
}
void run()
{
std::lock_guard<std::mutex> lock(*_mutex);
ExtLib::runNonThreadSafeFunction();
}
private:
std::shared_ptr<std::mutex> _mutex;
};
Are there any risks using this approach? I'm suspecting that it causes dead-locks but I'm not sure.
If this is not a good approach, how would you implement it?
Note: I am also considering using a static mutex inside the run-function, but I believe Visual Studio 2013 doesn't support it: https://connect.microsoft.com/VisualStudio/feedback/details/808030/runtime-crash-with-static-std-mutex).
Related
A common design pattern is to have a "manager" object that maintains a set of "managed" objects. In C++11 and later, the Manager likely keeps shared_ptrs to the Managed objects. If the Managed objects need a reference back to the Manager, they wisely do so by storing a weak_ptr<Manager>. The Manager can establish this relationship itself by constructing each Managed object directly (through a factory function, for example), and passing its own shared_ptr to the Managed object. The Manager can obtain its own shared_ptr by using shared_from_this(). None of these choices are required, but they are common and reasonable.
Now consider a Manager that maintains its Managed objects in a separate thread. A user of the Manager-Managed system may ask the Manager to create Managed objects, then run() the Manager so that it maintains those objects in the background until stop() is called. Still seems perfectly reasonable, right?
But now consider the Manager's destructor. It would be a nasty error to allow its background thread to continue past destruction. So we call stop() from the destructor.
Yet this raises a serious issue. Because the Manager is owned by shared_ptrs, its destructor will be called precisely when no shared_ptr references it. At that point, all weak_ptrs to the Manager will be expired(). Therefore all of the Managed objects' manager pointers will be invalid. And since the Managed objects are being "worked" (their member functions called) in a separate thread, they may suddenly find themselves with a null manager. If they assume their manager is non-null, the result is an error of one (severe) kind or another.
I see three potential solutions to the problem.
Add explicit checks for non-null manager everywhere it's used in Managed object code. Yet depending on the complexity of Managed objects, these checks are likely to be fiddly and error-prone.
Ensure that stop() is called prior to the manager being destroyed. Yet this violates the semantics of shared_ptr. There is no single owner of Manager: it is shared. So no single object knows when it will die or when it should stop updating. Moreover, it's simply bad form to leave Manager's destructor without a call to stop(): RAII implies that the Manager must deal with its own thread.
Make the Manager detect its own imminent death and stop calling Managed objects when it's dying. This has the benefit of centralizing the burden: the Manager should be able to detect its own death in a few places (for example, loops over all Managed objects) and refuse to deal with Managed objects in those places. Since the Managed objects won't be called, they won't attempt to use their expired weak_ptr<Manager>s and therefore won't fail (or need to check them constantly).
Is there a standard, correct way of dealing with this problem? Is the problem as I've framed it in violation of some well-understood principle for design, use of shared_ptr/weak_ptr, or use of threads?
The following code illustrates the problem.
#include <memory>
#include <thread>
#include <vector>
#include <cassert>
using namespace std;
class Manager;
class Managed {
public:
explicit Managed( shared_ptr< Manager > manager )
: m_manager( manager )
{}
void doStuff() {
// Fails because Manager::work() may be continuing to traverse Managed objects in a separate thread
// while m_manager is in its destructor (and therefore dead).
assert( m_manager.expired() == false );
// ...
}
private:
weak_ptr< Manager > m_manager;
};
class Manager : public enable_shared_from_this< Manager > {
public:
~Manager() {
stop(); // Problematic: all weak_ptrs to me are now expired(), yet work() continues a moment.
}
shared_ptr< Managed > create() {
assert( !m_thread.joinable() ); // Mustn't be running, to avoid concurrency issues.
auto managed = make_shared< Managed >( shared_from_this() );
m_managed.push_back( managed );
return managed;
}
void run() {
m_continue = true;
m_thread = thread{ bind( &Manager::work, this ) };
}
void stop() {
m_continue = false;
if( m_thread.joinable() ) {
m_thread.join();
}
}
private:
vector< shared_ptr< Managed >> m_managed;
thread m_thread;
atomic_bool m_continue{ true };
void work() {
while( m_continue ) {
for( const auto& managed : m_managed ) {
managed->doStuff();
}
}
}
};
int main() {
// Create the manager and a bunch of managed objects.
auto manager = make_shared< Manager >();
for( size_t i = 0; i < 10000; ++i ) {
manager->create();
}
// Run for a while.
manager->run();
this_thread::sleep_for( chrono::seconds{ 1 } );
manager.reset(); // Calls manager->stop() indirectly.
return 0;
}
Shared pointers do not solve every resource problem. They solve one particular one that happens to be easy to fix: blindly using shared pointers causes more problems than it fixes in my experience.
Shared pointers are about distributing the right to extend the lifetime of some object to an unbounded set of clients. Clients who hold weak pointers are people who want to be able to passively know when the object has gone away. This means they must always check, and once it goes away they have no rights to get it back.
If you have singular ownership, then use a unique pointer not a shared pointer.
If you guarantee the workers do not outlive the manager, give them a raw pointer not a weak pointer.
If you want to clean up before you destroy the object, give the unique pointer a custom deleter that does cleanup before delete.
Then, by delete, your workers should all be gone. Assert, and abort if you fail.
If you want non-unique-pointer semenatics, wrap up your actual manager as a unique pImpl within a wrapper type that offers pseudo-value semantics, and either use a custom deleter or just have the wrapper's destructor call the pre-destruction cleanup code.
shared_from_this is no longer involved.
I've been having some issues with threading in monotouch. My app makes use of an external library which I've linked with and it works fine. Because of the nature of the app and the library I have to make all the calls to it on a single separate thread.These calls will generally be :
Random non deterministic caused by user
Every t miliseconds(around 20ms). Like an update function
After reading for a bit I decided to try out NSThread. I've managed to call the Update function by attaching an NSTimer to the thread's RunLoop and it's all working fine. The problem that I'm having now is calling other methods on the same thread. I read somewhere that using PerformSelector on the RunLoop adds the selector invocation to the RunLoop's queue and invokes it when available, which is basically exactly what I need. However the methods that I need to call :
Can have multiple paramteres
Have callbacks, which I need to invoke on the main thread, again with multiple parameters
For the multiple parameters problem I saw that NSInvocation can be a solution, but the life of me I can't figure out how to do it with monotouch and haven't found any relevant examples.
For the actuals calls that I need to make to the library, I tried doing a generic way in which I can call any function I choose via delegates on a particular thread, which sort of works until I'm hit with the multiple parameters and/or callbacks to the main thread again with multiple parameters. Should I maybe just register separate selectors for each (wrapped)function that I need to call from the library?
I'm not hellbent on using this approach, if there is a better way I'm open to it, it's just that after searching for other options I saw that they don't fit my case:
GCD(not even sure I have it in monotouch) spawns threads on it's own whenever necessary. I need a single specific thread to schedule my work on
NSInvocationQueue(which uses GCD internally from what I read) does the same thing.
pThreads, seem overkill and managing them will be a pain(not even sure I can use them in monotouch)
I'm not an iOS developer, the app works fine with monodroid where I had Runnables and Handlers which make life easier :) . Maybe I'm not looking at this the right way and there is a simple solution to this. Any input would be appreciated.
Thanks
UPDATE
I was thinking of doing something along these lines :
Have a simple wrapper :
class SelectorHandler : NSObject
{
public static Selector Selector = new Selector("apply");
private Action execute;
public SelectorHandler(Action ex)
{
this.execute = ex;
}
[Register("apply")]
private void Execute()
{
execute();
}
}
Extend NSThread
public class Daemon : NSThread
{
public void Schedule(Action action)
{
SelectorHandler handler = new SelectorHandler(action);
handler.PerformSelector(SelectorHandler.Selector, this, null, true);
}
}
Then, when I want to call something I can do it like this :
private Daemon daemon;
public void Call_Library_With_Callback(float param, Action<int> callback)
{
daemon.Schedule(() =>
{
int callbackResult = 0;
//Native library calls
//{
// Assign callback result
//}
daemon.InvokeOnMainThread(() =>
{
callback(callbackResult);
});
});
}
(Pseudo-)Code
Here is a non-compilable code-sketch of the concepts I am having trouble with:
struct Data {};
struct A {};
struct B {};
struct C {};
/* and many many more...*/
template<typename T>
class Listener {
public:
Listener(MyObject* worker):worker(worker)
{ /* do some magic to register with RTI DDS */ };
public:
// This function is used ass a callback from RTI DDS, i.e. it will be
// called from other threads when new Data is available
void callBackFunction(Data d)
{
T t = extractFromData(d);
// Option 1: direct function call
// works somewhat, but shows "QObject::startTimer: timers cannot be started
// from another thread" at the console...
worker->doSomeWorkWithData(t); //
// Option 2: Use invokeMethod:
// seems to fail, as the macro expands including '"T"' and that type isn't
// registered with the QMetaType system...
// QMetaObject::invokeMethod(worker,"doSomeGraphicsWork",Qt::AutoConnection,
// Q_ARG(T, t)
// );
// Option 3: use signals slots
// fails as I can't make Listener, a template class, a QObject...
// emit workNeedsToBeDone(t);
}
private:
MyObject* worker;
T extractFromData(Data d){ return T(d);};
};
class MyObject : public QObject {
Q_OBJECT
public Q_SLOTS:
void doSomeWorkWithData(A a); // This one affects some QGraphicsItems.
void doSomeWorkWithData(B b){};
void doSomeWorkWithData(C c){};
public:
MyObject():QObject(nullptr){};
void init()
{
// listeners are not created in the constructor, but they should have the
// same thread affinity as the MyObject instance that creates them...
// (which in this example--and in my actual code--would be the main GUI
// thread...)
new Listener<A>(this);
new Listener<B>(this);
new Listener<C>(this);
};
};
main()
{
QApplication app;
/* plenty of stuff to set up RTI DDS and other things... */
auto myObject = new MyObject();
/* stuff resulting in the need to separate "construction" and "initialization" */
myObject.init();
return app.exec();
};
Some more details from the actual code:
The Listener in the example is a RTI DataReaderListener, the callback
function is onDataAvailable()
What I would like to accomplish
I am trying to write a little distributed program that uses RTI's Connext DDS for communication and Qt5 for the GUI stuff--however, I don't believe those details do matter much as the problem, as far as I understood it, boils down to the following:
I have a QObject-derived object myObject whose thread affinity might or might not be with the main GUI thread (but for simplicity, let's assume that is the case.)
I want that object to react to event's which happen in another, non-Qt 3rd-party library (in my example code above represented by the functions doSomeWorkWithData().
What I understand so far as to why this is problematic
Disclaimer: As usual, there is always more than one new thing one learns when starting a new project. For me, the new things here are/were RTI's Connext and (apparently) my first time where I myself have to deal with threads.
From reading about threading in Qt (1,2,3,4, and 5 ) it seems to me that
QObjects in general are not thread safe, i.e. I have to be a little careful about things
Using the right way of "communicating" with QObjects should allow me to avoid having to deal with mutexes etc myself, i.e. somebody else (Qt?) can take care of serializing access for me.
As a result from that, I can't simply have (random) calls to MyClass::doSomeWorkWithData() but I need to serialize that. One, presumably easy, way to do so is to post an event to the event queue myObject lives in which--when time is available--will trigger the execution of the desired method, MyClass::doSomeWorkWithData() in my case.
What I have tried to make things work
I have confirmed that myObject, when instantiated similarly as in the sample code above, is affiliated with the main GUI thread, i.e. myObject.thread() == QApplication::instance()->thread().
With that given, I have tried three options so far:
Option 1: Directly calling the function
This approach is based upon the fact that
- myObject lives in the GUI thread
- All the created listeners are also affiliated with the GUI thread as they are
created by `myObject' and inherit its thread that way
This actually results in the fact that doSomeWorkWithData() is executed. However,
some of those functions manipulate QGraphicsItems and whenever that is the case I get
error messages reading: "QObject::startTimer: timers cannot be started from another
thread".
Option 2: Posting an event via QMetaObject::invokeMethod()
Trying to circumvent this problem by properly posting an event for myObject, I
tried to mark MyObject::doSomeWorkWithData() with Q_INVOKABLE, but I failed at invoking the
method as I need to pass arguments with Q_ARG. I properly registered and declared my custom types
represented by struct A, etc. in the example), but I failed at the fact the
Q_ARG expanded to include a literal of the type of the argument, which in the
templated case didn't work ("T" isn't a registered or declared type).
Trying to use conventional signals and slots
This approach essentially directly failed at the fact that the QMeta system doesn't
work with templates, i.e. it seems to me that there simply can't be any templated QObjects.
What I would like help with
After spending about a week on attempting to fix this, reading up on threads (and uncovering some other issues in my code), I would really like to get this done right.
As such, I would really appreciate if :
somebody could show me a generic way of how a QObject's member function can be called via a callback function from another 3rd-party library (or anything else for that matter) from a different, non QThread-controlled, thread.
somebody could explain to me why Option 1 works if I simply don't create a GUI, i.e. do all the same work, just without a QGraphcisScene visualizing it (and the project's app being a QCoreApplication instead of a QApplication and all the graphics related work #defineed out).
Any, and I mean absolutely any, straw I could grasp on is truly appreciated.
Update
Based on the accepted answer I altered my code to deal with callbacks from other threads: I introduced a thread check at the beginning of my void doSomeWorkWithData() functions:
void doSomeWorkWithData(A a)
{
if( QThread::currentThread() != this->thread() )
{
QMetaObject::invokeMethod( this,"doSomeWorkWithData"
,Qt::QueuedConnection
,Q_ARG(A, a) );
return;
}
/* The actual work this function does would be below here... */
};
Some related thoughts:
I was contemplating to introduce a QMutexLocker before the if statement, but decided against it: the only part of the function that is potentially used in parallel (anything above the return; in the if statement) is--as far as I understand--thread safe.
Setting the connection type manually to Qt::QueuedConnection: technically, if I understand the documentation correctly, Qt should do the right thing and the default, Qt::AutoConnection, should end up becoming a Qt::QueuedConnection. But since would always be the case when that statement is reached, I decided to put explicitly in there to remind myself about why this is there.
putting the queuing code directly in the function and not hiding it in an interim function: I could have opted to put the call to invokeMethod in another interim function, say queueDoSomeWorkWithData()', which would be called by the callback in the listener and then usesinvokeMethodwith anQt::AutoConnection' on doSomeWorkWithData(). I decided against this as there seems no way for me to auto-code this interim function via templates (templates and the Meta system was part of the original problem), so "the user" of my code (i.e. the person who implements doSomeWorkWithData(XYZ xyz)) would have to hand type the interim function as well (as that is how the templated type names are correctly resolved). Including the check in the actual function seems to me to safe typing an extra function header, keeps the MyClass interface a little cleaner, and better reminds readers of doSomeWorkWithData() that there might be a threading issue lurking in the dark.
It is ok to call a public function on a subclass of QObject from another thread if you know for certain that the individual function will perform only thread-safe actions.
One nice thing about Qt is that it will handle foreign threads just as well as it handles QThreads. So, one option is to create a threadSafeDoSomeWorkWithData function for each doSomeWorkWithData that does nothing but QMetaMethod::invoke the non-threadsafe one.
public:
void threadSafeDoSomeWorkWithData(A a) {
QMetaMethod::invoke("doSomeWorkWithData", Q_ARG(A,a));
}
Q_INVOKABLE void doSomeWorkWithData(A a);
Alternatively, Sergey Tachenov suggests an interesting way of doing more or less the same thing in his answer here. He combines the two functions I suggested into one.
void Obj2::ping() {
if (QThread::currentThread() != this->thread()) {
// not sure how efficient it is
QMetaObject::invoke(this, "ping", Qt::QueuedConnection);
return;
}
// thread unsafe code goes here
}
As to why you see normal behaviour when not creating a GUI? Perhaps you're not doing anything else that is unsafe, aside from manipulating GUI objects. Or, perhaps they're the only place in which your thread-safety problems are obvious.
I'm recently programming a very simple logger class in Visual C++ 2010, but I have a problem. Everytime I run the program, a debug assertion failure appears.
Expression: _CrtIsValidHeapPointer(pUserData)
This is how my class looks like (basically it's only a little modified from an answer here C++ Singleton design pattern):
class Logger
{
public:
// Returns static instance of Logger.
static Logger& getInstance()
{
static Logger logger; // This is where the assertion raises.
return logger;
}
void logError(std::string errorText);
// Destructor.
~Logger();
private:
std::ofstream logFileStream;
// The constructor is private to prevent class instantiating.
Logger();
// The copy constructor and '=' operator need to be disabled.
Logger(Logger const&) { };
Logger& operator=(Logger other) { };
};
And the constructor is:
Logger::Logger()
: logFileStream(Globals::logFileName, std::ios_base::trunc)
{
// (Tries to open the file specified in Globals for (re)writing.)
}
I found out that I can solve it by using static variables or methods somehow, but I don't understand what's wrong with this code. Does anyone know, where the problem is?
EDIT: FYI, the failure raises when this code is called (for the first time):
Logger::getInstance().logError("hello");
EDIT 2: This is the definition of logFileName in Globals:
static const std::string logFileName = "errorLog.log";
My guess is that you're calling getInstance() from the constructor of another global variable, and encountering the infamous initialisation order fiasco - it's unspecified whether or not Globals::logFileName has been initialised before any globals in other translation units.
One fix is to use an old-school C string, which will be statically initialised before any global constructor is called:
static const char * logFileName = "errorLog.log";
Another possibility is to access it via a function:
static std::string logFileName() {return "errorLog.log";}
My favoured solution would be to remove the global instance altogether, and pass a reference to whatever needs it; but some might find that rather tedious, especially if you already have a large amount of code that uses the global.
C++/CLI is not standard C++ and plays by slightly different rules. Are you using C++/CLI managed code at all? (/clr compiler option?) This looks like a common problem when mixing C++ (unmanaged) code with C++/CLI (managed) code. It has to do with the way managed and unmanaged construction and destruction happens at program initialization and program exit. Removing the destructor works for me - can you do that in your Logger class?
For more details and possible workarounds:
http://www.codeproject.com/Articles/442784/Best-gotchas-of-Cplusplus-CLI
http://social.msdn.microsoft.com/Forums/vstudio/en-US/fa0e9340-619a-4b07-a86b-894358d415f6/crtisvalidheappointer-fails-on-globally-created-object-within-a-static-llibrary?forum=vcgeneral
http://forums.codeguru.com/showthread.php?534537-Memory-leaks-when-mixing-managed-amp-native-code&p=2105565
I'm a QT newbie. I have a class extend from widget like:
class myclass: public Qwidget
{
Q_OBJECT
public:
void myfunction(int);
slots:
void myslot(int)
{
//Here I want to put myfunction into a thread
}
...
}
I don't know how to do it. Please help me.
Add a QThread member then in myslot move your object to the thread and run the function.
class myclass: public Qwidget
{
QThread thread;
public:
slots:
void myfunction(int); //changed to slot
void myslot(int)
{
//Here I want to put myfunction into a thread
moveToThread(&thread);
connect(&thread, SIGNAL(started()), this, SLOT(myfunction())); //cant have parameter sorry, when using connect
thread.start();
}
...
}
My answer is basically the same as from this post: Is it possible to implement polling with QThread without subclassing it?
Your question is very broad . Please find some alternatives that could be beneficial to you :
If you want to use signal/slot mechanism and execute your slot within a thread context you can use moveToThread method to move your object into a thread (or create it directly within the run method of QThread) and execute your slot within that thread's context. But Qt Docs says that
The object cannot be moved if it has a
parent.
Since your object is a widget, I assume that it will have a parent.
So it is unlikely that this method will be useful for you.
Another alternative is using QtConcurrent::run() This allows a method to be executed by another thread. However this way you can not use signal/slot mechanism. Since you declared your method as a slot. I assumed that you want to use this mechanism. If you don't care then this method will be useful for you.
Finally you can create a QThread subclass within your slot and execute whatever your like there.
This is all I could think of.
I hope this helps.