File tree Expand file tree Collapse file tree 8 files changed +21
-26
lines changed Expand file tree Collapse file tree 8 files changed +21
-26
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,6 @@ void print(T x) {
17
17
}
18
18
19
19
void foo () {
20
- // 時間のかかる処理
21
20
for (int i = 0 ; i < 3 ; ++i) {
22
21
print (i);
23
22
}
Original file line number Diff line number Diff line change @@ -25,8 +25,7 @@ void bar() {
25
25
} // bar()を抜けてもfooは別スレッドで実行中
26
26
27
27
int main () {
28
- std::promise<void > p;
29
- waiter_ = p.get_future ();
28
+ waiter_ = setter_.get_future ();
30
29
31
30
bar ();
32
31
Original file line number Diff line number Diff line change 10
10
11
11
using namespace std ;
12
12
13
- void foo (stringstream& ss ) {
14
- ss << " foo() : " << this_thread::get_id () << endl;
13
+ void foo (ostringstream& os ) {
14
+ os << " foo() : " << this_thread::get_id () << endl;
15
15
}
16
16
17
- void bar (stringstream& ss ) {
18
- ss << " bar() : " << this_thread::get_id () << endl;
17
+ void bar (ostringstream& os ) {
18
+ os << " bar() : " << this_thread::get_id () << endl;
19
19
}
20
20
21
21
int main () {
22
- stringstream ss_foo ;
23
- stringstream ss_bar ;
24
- thread th1 (foo, ref (ss_foo ));
25
- thread th2 (bar, ref (ss_bar ));
22
+ ostringstream os_foo ;
23
+ ostringstream os_bar ;
24
+ thread th1 (foo, ref (os_foo ));
25
+ thread th2 (bar, ref (os_bar ));
26
26
27
27
// それぞれのスレッドのIDを取得する
28
28
thread::id id1 = th1.get_id ();
@@ -36,7 +36,7 @@ int main() {
36
36
th1.join ();
37
37
th2.join ();
38
38
39
- cout << ss_foo .str ();
40
- cout << ss_bar .str ();
39
+ cout << os_foo .str ();
40
+ cout << os_bar .str ();
41
41
cout << " main thread : " << this_thread::get_id () << endl;
42
42
}
Original file line number Diff line number Diff line change @@ -19,9 +19,9 @@ int main() {
19
19
// 1スレッドだけ起動する
20
20
}
21
21
22
- vector<thread> ths;
23
- for (size_t i = 0 ; i < mp; ++i ) {
24
- ths[i] = thread (worker);
22
+ vector<thread> ths (mp) ;
23
+ for (thread& th : ths ) {
24
+ th = thread (worker);
25
25
}
26
26
27
27
for (thread& th : ths) {
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ using namespace std;
12
12
13
13
template <class T >
14
14
struct LockedQueue {
15
- LockedQueue (int capacity)
15
+ explicit LockedQueue (int capacity)
16
16
: capacity(capacity)
17
17
{}
18
18
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ using namespace std;
13
13
14
14
int worker (const vector<int >& data) {
15
15
int sum = 0 ;
16
- for (auto i : data) {
16
+ for (int i : data) {
17
17
// 重たい処理
18
18
this_thread::sleep_for (chrono::milliseconds (100 ));
19
19
sum += i;
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ using namespace std;
14
14
15
15
int worker (const vector<int >& data) {
16
16
int sum = 0 ;
17
- for (int i : data) {
17
+ for (auto i : data) {
18
18
// 重たい処理
19
19
this_thread::sleep_for (chrono::milliseconds (100 ));
20
20
sum += i;
Original file line number Diff line number Diff line change 8
8
#include < utility>
9
9
#include < vector>
10
10
11
- // todo gcc 4.8で検証する
12
- #define thread_local __thread
13
-
14
11
using namespace std ;
15
12
16
13
static mutex mtx;
@@ -25,9 +22,9 @@ void bar(size_t n) {
25
22
tn += n;
26
23
an += n;
27
24
28
- cout << " static int : " << sn << endl;
29
- cout << " thread_local int : " << tn << endl;
30
- cout << " int : " << an << endl;
25
+ cout << " static int : " << sn << " , "
26
+ << " thread_local int : " << tn << " , "
27
+ << " int : " << an << endl;
31
28
mtx.unlock ();
32
29
}
33
30
@@ -40,7 +37,7 @@ void foo() {
40
37
int main () {
41
38
vector<thread> ths;
42
39
43
- for (size_t i = 0 ; i < 4 ; ++i) {
40
+ for (size_t i = 0 ; i < 2 ; ++i) {
44
41
ths.push_back (thread (foo));
45
42
}
46
43
You can’t perform that action at this time.
0 commit comments