과제질문입니다 // TODO: use promise
안녕하세요
마지막에
cout << "Future" << endl;
{
const auto sta = chrono::steady_clock::now();
unsigned long long sum = 0;
vector<std::future<int>> futures;
futures.resize(n_threads);
const unsigned n_per_thread = n_data / n_threads; // assumes remainder = 0
for (unsigned t = 0; t < n_threads; ++t)
futures[t] = std::async(dotProductFuture, std::ref(v0), std::ref(v1),
t * n_per_thread, (t + 1) * n_per_thread);
for (unsigned t = 0; t < n_threads; ++t)
sum += futures[t].get();
const chrono::duration<double> dur = chrono::steady_clock::now() - sta;
cout << dur.count() << endl;
cout << sum << endl;
cout << endl;
}
이 코드를 쓰레드와 함께해서 사용해보고 promise 사용해보라고 하셨는데
// TODO: use divide and conquer strategy for std::thread
// TODO: use promise
cout << "Thread future" << endl;
{
const auto sta = chrono::steady_clock::now();
vector<thread> threads;
threads.resize(n_threads);
std::promise<int> p;
auto f = p.get_future();
const unsigned n_per_thread = n_data / n_threads; // assumes remainder = 0
for (unsigned t = 0; t < n_threads; ++t)
threads[t] = std::thread(dotProductEx1, std::ref(v0), std::ref(v1),
t * n_per_thread, (t + 1) * n_per_thread, move(p));
for (unsigned t = 0; t < n_threads; ++t)
threads[t].join();
const chrono::duration<double> dur = chrono::steady_clock::now() - sta;
int i = f.get();
cout << dur.count() << endl;
cout << i << endl;
cout << endl;
}
void dotProductEx1(const vector<int> &v0, const vector<int> &v1,
const unsigned i_start, const unsigned i_end, std::promise<int> &&p)
{
int sum = 0; // local sum
for (unsigned i = i_start; i < i_end; ++i)
sum += v0[i] * v1[i]; //
p.set_value(sum);
}
이렇게 해봤는데
런타임에러가 나네요 ㅜㅜ
어떻게 해야할지 좀 알려주시면 감사하겠습니다
너무헷갈리네요..
답변 1
6
스레드는 vector 사용해서 n_thread 개수 만큼의 스레드들을 사용하고 계신데
이에 반하여 promise와 future 객체는 딱 한개씩만 선언하셨어요 std::promise<int> p; auto f = ..이렇게요..!
스레드는 여러개인데 각 스레드들에 단 하나의 promise, future 객체를 적용하셔서 런타임 에러가 난 것 같아요.
스레드가 여러개라면 promise 객체도 여러개여야겠죠? 각 스레드마다 약속할 수 있는 것들이 다 다를테니까요. 하는일도 각각 다 다르니까!
이렇게 고쳐보시면 어떨까 싶어요. promise, future 객체도 numThread 개수의 배열(vector)로 선언해주고 각 스레드에게 배정할 수 있게 해주세요.
변수가 메모리에 저장되는 것을 알려주는 강의가 어떤강의였죠
1
481
1
메모리 주소 10진수로 출력
1
672
1
클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.
1
515
1
여러가지 리턴 타입에 관한 강의가 어떤 걸까요?
1
549
1
메모리 주소에 관한 질분
0
687
1
인터페이스 클래스에서 reportError의 매개변수에 대해 궁금한 것이 있습니다.
0
558
1
형변환 오버로딩에서 const 관련 질문이 있습니다.
0
448
1
Digit 뒤에 reference를 사용하는 이유
0
512
1
4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결
0
325
1
dat파일이...
0
540
1
TODO:대입 연산자 오버로딩에 대한 소스코드입니다.
0
651
1
복사 생성자 관련 질문이 있습니다.
0
456
1
수업 중 궁금한점이 있습니다.
1
392
1
라이브러리자체가 이해가 되지 않습니다.
0
565
1
마지막 예제 질문
0
305
1
증감연산자 위치에 따른 수행 순서 질문입니다.
0
382
1
단항 연산자 오버로딩에서 return 부분에 질문이 있습니다.
1
416
1
friend함수 관련 질문이 있습니다.
0
313
1
operator+ 정의부분에서 궁금한 것이 있습니다.
0
448
1
3분 17초 질문
0
354
1
함수에 값을 대입한다는 개념이 이해가 되지 않습니다.
0
450
1
int getvalue() const에서 const는 왜 뒤에 붙는건가요?
0
453
2
const Something &st에서 const를 빼면 안되나요?
0
304
1
friend함수는 다른 클래스의 멤버함수로 쓸 수 없나요??
1
495
1





