Skip to content

Commit 156b648

Browse files
Fix source files.
1 parent 51f9601 commit 156b648

8 files changed

+21
-26
lines changed

008_thread/003_join.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ void print(T x) {
1717
}
1818

1919
void foo() {
20-
// 時間のかかる処理
2120
for (int i = 0; i < 3; ++i) {
2221
print(i);
2322
}

008_thread/004_detach.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ void bar() {
2525
} // bar()を抜けてもfooは別スレッドで実行中
2626

2727
int main() {
28-
std::promise<void> p;
29-
waiter_ = p.get_future();
28+
waiter_ = setter_.get_future();
3029

3130
bar();
3231

008_thread/005_get_id.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@
1010

1111
using namespace std;
1212

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;
1515
}
1616

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;
1919
}
2020

2121
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));
2626

2727
// それぞれのスレッドのIDを取得する
2828
thread::id id1 = th1.get_id();
@@ -36,7 +36,7 @@ int main() {
3636
th1.join();
3737
th2.join();
3838

39-
cout << ss_foo.str();
40-
cout << ss_bar.str();
39+
cout << os_foo.str();
40+
cout << os_bar.str();
4141
cout << "main thread : " << this_thread::get_id() << endl;
4242
}

008_thread/008_hardware_concurrency.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ int main() {
1919
// 1スレッドだけ起動する
2020
}
2121

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);
2525
}
2626

2727
for (thread& th : ths) {

008_thread/014_condition_variable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using namespace std;
1212

1313
template <class T>
1414
struct LockedQueue {
15-
LockedQueue(int capacity)
15+
explicit LockedQueue(int capacity)
1616
: capacity(capacity)
1717
{}
1818

008_thread/016_01_async.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using namespace std;
1313

1414
int worker(const vector<int>& data) {
1515
int sum = 0;
16-
for (auto i : data) {
16+
for (int i : data) {
1717
//重たい処理
1818
this_thread::sleep_for(chrono::milliseconds(100));
1919
sum += i;

008_thread/016_02_packaged_task.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using namespace std;
1414

1515
int worker(const vector<int>& data) {
1616
int sum = 0;
17-
for (int i : data) {
17+
for (auto i : data) {
1818
//重たい処理
1919
this_thread::sleep_for(chrono::milliseconds(100));
2020
sum += i;

008_thread/017_thread_local.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
#include <utility>
99
#include <vector>
1010

11-
// todo gcc 4.8で検証する
12-
#define thread_local __thread
13-
1411
using namespace std;
1512

1613
static mutex mtx;
@@ -25,9 +22,9 @@ void bar(size_t n) {
2522
tn += n;
2623
an += n;
2724

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;
3128
mtx.unlock();
3229
}
3330

@@ -40,7 +37,7 @@ void foo() {
4037
int main() {
4138
vector<thread> ths;
4239

43-
for (size_t i = 0; i < 4; ++i) {
40+
for (size_t i = 0; i < 2; ++i) {
4441
ths.push_back(thread(foo));
4542
}
4643

0 commit comments

Comments
 (0)