본문 바로가기
Language/C++

[C++] std::packaged_task

by 어발 2024. 12. 18.
멀티쓰레드를 고려하지 않은 일반 함수에 대해서 값을 꺼내오고 싶을때 사용

std::packaged_task 예제

#include <iostream>
#include <thread>
#include <future>

int add(int a, int b)
{
	std::cout << "add" << std::endl;
    return a+b;
}

int main()
{
	std::packaged_task<int(int, int)> task(add);
    
    std::future<int> ft = task.get_future();
    
    std::thread t(std::move(task), 10, 20);
    
    int ret = ft.get();
    std::cout << ret << std::endl;
    t.join();
}

 

728x90

'Language > C++' 카테고리의 다른 글

[C++] std::jthread  (0) 2025.01.06
[C++] std::async  (0) 2024.12.20
[C++] std::promise, std::future  (0) 2024.12.04
[C++] std::ref, std::reference_wrapper  (1) 2023.09.01
[C++] std::thread  (1) 2023.09.01

댓글