C++ 멀티스레드 프로그래밍 아주 쉬워졌음.
$ clang++ thread_test.cpp -std=c++11 -o thrad_test
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
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <thread>
void executebuf (std::vector<int>& buf, int i)
//void executebuf (int buf[], int i)
{
buf[i] = i*10;
if (i==2) // do something
for (int j=0; j<1000000; j++)
int x = j*i*i*i;
fprintf (stderr, "ebuf= %d\n", i);
}
void execute()
{
std::cout << "Hello Concurrent world" << std::endl;
}
std::string filename;
void execute2()
{
for (int i=0; i<1000000; i++) // do something
int x = i*i*i*i;
std::cout << "Hello Concurrent world 2 " << filename << std::endl;
filename = std::string("printed");
}
int main()
{
std::thread th(execute);
filename = "this is filename.";
std::thread th2(execute2);
th.join();
th2.join();
std::cout << filename << std::endl;
std::vector<int> buf(10);
std::vector<std::thread> t;
for (int i=0; i<4; i++)
t.push_back( std::thread (executebuf, std::ref(buf), i) );
for (int i=0; i<4; i++)
t[i].join();
for (int i=0; i<4; i++)
printf ("%d \n", buf[i]);
return(0);
}
//EOF//
|
End.
댓글 없음:
댓글 쓰기