Repository navigation
Separate objects from Thread management - #1250
Conversation
Thread<T> is a generic thread management object that starts a thread, creates an object T in that thread and calls its .start() function. When the Thread<T> object is destructed (which must happen in the thread that created it), it'll call the object's .stop() function, and wait for thread termination. The .stop() function must somehow get the thread to terminate. Note that object T's constructor/destructor/start()/stop() must be protected/private, and Thread<T> must be declared as a friend class. All public functions may be called from any thread and are exposed through operator->(). RunLoop encapsulates a libuv runloop and has a facility of dispatching function objects to the thread. It can be used either as a worker thread class by itself, or it can be derived from. This commit converts SQLiteCache to derive from RunLoop and removes the custom code used previously for managing the cache thread.
Adds more documentation, and guarantees that the Object in the managed thread stays around until the Thread<> destructor is called. This might happen if .start() returns before .stop() is called, e.g. when a run loop in Object() terminates prematurely.
|
This is cool, I have an alternate factoring of these additions to suggest that I'll push up in a separate branch. |
c8df336 to
3d166db
Compare
|
@kkaefer - GCC 4.9 doesn't look like it's going to pan out. It produces: Also, installing the gcc-4.9 package breaks compilation with clang, due to a bug in clang (issue is for clang 3.5, but also exists in 3.4 apparently, which is what we're using). |
3d166db to
278e6bd
Compare
|
The reason I prefer to merge |
|
@kkaefer - I looked at this deeper this afternoon and I
I think we can accomplish all of these simultaneously. I've pushed up a new implementation to Summary of changes vs. your branch:
Then the remaining need for access to the underlying loop is only for doing other libuv things, and can be accomplished by adding a Let me know what you think, and if you can figure out how to fix the remaining compile errors. |
Does that mean we can only invoke function on thread loops iff the current thread also has a runloop? That'd mean that we couldn't use the
I like this change, much easier interface and we avoid all the errors with accidentally not calling the callback function. However, this also means that we can't pass on the callback if the current function can't satisfy the request; it essentially forces all invoke requests to be synchronous. |
The underlying issue here is that it's really hard to forward & move the arguments correctly; |
It means we can invoke functions with a callback iff the current thread has a runloop. Fire-and-forget invocations are okay. If we needed callbacks for the mean thread, we'd have to have some (platform-specific) means of dispatching on the native event loop of the main thread.
Not being able to pass on the callback directly is good -- the callback needs to be invoked on the invoking thread, and passing it on would hide this requirement. Maybe we could define another overload for Not sure if that actually works though.
Someone has to have solved the problem of generic cross-thread move semantics. I'll research this more today. |
|
Best reference so far: this article by Herb Sutter (this comment starts to touch on the problem we're facing). |
|
Asked a Stack Overflow question. |
|
So close! This is working if you compile in C++14 mode. I should be able to replace these C++14 move-capturing lambdas with this trick but for some reason it doesn't work (runtime crash). |
|
I think @artemp and @kkaefer ruled this out, but its still worth mentioning that |
|
This is how I would expect to be able to define But this fails to compile. The outer std::bind apparently winds up not having a eligible |
|
Looks like we don't need to move-capture there anyway. A copying capture works fine. 82c6bdb04d7ee426dd27f6303a3e3da6309ea214 |
f7f3bac to
aa6edaa
Compare
|
Storage tests are now failing because of an attempt to dispatch a result back to the main test thread, which doesn't have a run loop. Not sure how this used to work... |
67be25b to
708df31
Compare
Implements a Thread<>::invoke interface that only permits calling member functions with bound arguments. Also introduce invokeWithResult, which accepts a callback and invokes it, passing the result, on the invoking thread's RunLoop. This closes the loop of two evented threads doing message passing.
708df31 to
ac25d32
Compare
Separate objects from Thread management
We should separate objects like the SQLiteCache, DefaultFileSource and the Map object from thread management considerations.