-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.cc
73 lines (57 loc) · 1.62 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/threading/thread.h"
#include <iostream>
using namespace std;
class Foo
: public base::RefCountedThreadSafe<Foo> {
public:
Foo(int t)
: a_(t),
worker_pool_(new base::SequencedWorkerPool(3, "fangr_")) {
}
~Foo() {
worker_pool_->Shutdown();
}
void work() {
worker_pool_->PostWorkerTask(FROM_HERE, base::Bind(&Foo::DoWork, this));
}
void ordered_tasks() {
base::SequencedWorkerPool::SequenceToken token = worker_pool_->GetSequenceToken();
worker_pool_->PostSequencedWorkerTask(token, FROM_HERE, base::Bind(&Foo::DoTask1, this));
worker_pool_->PostSequencedWorkerTask(token, FROM_HERE, base::Bind(&Foo::DoTask2, this));
}
private:
void DoWork() {
for (int i = -10; i < a_; ++i) {
cout << i << ",\t";
}
}
void DoTask1() {
for (int i = 0; i < 10; ++i) {
cout << 'a' + i << "|\t";
}
}
void DoTask2() {
for (int i = 1; i < 10; ++i) {
cout << "foo" << "$\t";
}
}
private:
scoped_refptr<base::SequencedWorkerPool> worker_pool_;
int a_;
};
scoped_refptr<Foo> test;
int main(int argc, char* argv[]) {
cout << "test!!!" << endl;
test = new Foo(10);
while (true) {
test->work();
test->ordered_tasks();
}
return 0;
}