diff options
Diffstat (limited to 'base/message_loop_proxy.h')
-rw-r--r-- | base/message_loop_proxy.h | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/base/message_loop_proxy.h b/base/message_loop_proxy.h index 9a856bf..decaa38 100644 --- a/base/message_loop_proxy.h +++ b/base/message_loop_proxy.h @@ -70,6 +70,51 @@ class BASE_EXPORT MessageLoopProxy // this proxy represents. virtual bool BelongsToCurrentThread() = 0; + // Executes |task| on the given MessageLoopProxy. On completion, |reply| + // is passed back to the MessageLoopProxy for the thread that called + // PostTaskAndReply(). Both |task| and |reply| are guaranteed to be deleted + // on the thread from which PostTaskAndReply() is invoked. This allows + // objects that must be deleted on the originating thread to be bound into the + // |task| and |reply| Closures. In particular, it can be useful to use + // WeakPtr<> in the |reply| Closure so that the reply operation can be + // canceled. See the following pseudo-code: + // + // class DataBuffer : public RefCountedThreadSafe<DataBuffer> { + // public: + // // Called to add data into a buffer. + // void AddData(void* buf, size_t length); + // ... + // }; + // + // + // class DataLoader : public SupportsWeakPtr<ReadToBuffer> { + // public: + // void GetData() { + // scoped_refptr<DataBuffer> buffer = new DataBuffer(); + // target_thread_.message_loop_proxy()->PostTaskAndReply( + // FROM_HERE, + // base::Bind(&DataBuffer::AddData, buffer), + // base::Bind(&DataLoader::OnDataReceived, AsWeakPtr(), buffer)); + // } + // + // private: + // void OnDataReceived(scoped_refptr<DataBuffer> buffer) { + // // Do something with buffer. + // } + // }; + // + // + // Things to notice: + // * Results of |task| are shared with |reply| by binding a shared argument + // (a DataBuffer instance). + // * The DataLoader object has no special thread safety. + // * The DataLoader object can be deleted while |task| is still running, + // and the reply will cancel itself safely because it is bound to a + // WeakPtr<>. + bool PostTaskAndReply(const tracked_objects::Location& from_here, + const Closure& task, + const Closure& reply); + template <class T> bool DeleteSoon(const tracked_objects::Location& from_here, T* object) { |