| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Illustrates how to use worker threads that issue completion callbacks | 5 // Illustrates how to use worker threads that issue completion callbacks |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 ExampleEmployer::~ExampleEmployer() { | 105 ExampleEmployer::~ExampleEmployer() { |
| 106 } | 106 } |
| 107 | 107 |
| 108 bool ExampleEmployer::DoSomething(const CompletionCallback& callback) { | 108 bool ExampleEmployer::DoSomething(const CompletionCallback& callback) { |
| 109 DCHECK(!request_.get()) << "already in use"; | 109 DCHECK(!request_.get()) << "already in use"; |
| 110 | 110 |
| 111 request_ = new ExampleWorker(this, callback); | 111 request_ = new ExampleWorker(this, callback); |
| 112 | 112 |
| 113 // Dispatch to worker thread... | 113 // Dispatch to worker thread... |
| 114 if (!base::WorkerPool::PostTask( | 114 if (!base::WorkerPool::PostTask( |
| 115 FROM_HERE, | 115 FROM_HERE, base::Bind(&ExampleWorker::DoWork, request_), true)) { |
| 116 base::Bind(&ExampleWorker::DoWork, request_.get()), | |
| 117 true)) { | |
| 118 NOTREACHED(); | 116 NOTREACHED(); |
| 119 request_ = NULL; | 117 request_ = NULL; |
| 120 return false; | 118 return false; |
| 121 } | 119 } |
| 122 | 120 |
| 123 return true; | 121 return true; |
| 124 } | 122 } |
| 125 | 123 |
| 126 } // namespace | 124 } // namespace |
| 127 | 125 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 147 EXPECT_TRUE(queued); | 145 EXPECT_TRUE(queued); |
| 148 | 146 |
| 149 EXPECT_FALSE(did_check_result); | 147 EXPECT_FALSE(did_check_result); |
| 150 closure.WaitForResult(); | 148 closure.WaitForResult(); |
| 151 EXPECT_TRUE(did_check_result); | 149 EXPECT_TRUE(did_check_result); |
| 152 } | 150 } |
| 153 | 151 |
| 154 // TODO: test deleting ExampleEmployer while work outstanding | 152 // TODO: test deleting ExampleEmployer while work outstanding |
| 155 | 153 |
| 156 } // namespace net | 154 } // namespace net |
| OLD | NEW |