daehan21
자율주행/로봇 - 공부기록
daehan21
  • 분류 전체보기 (9)
    • SLAM (0)
    • ROS (3)
      • Tutorials (1)
      • Multi-Threading (1)
    • C++ (5)
      • Smart Pointer (0)
      • Multi-Threading (3)
      • Bit-Flag (2)
    • Mathematics (0)
      • Linear Algebra (0)
    • git github (0)
    • Algorithm (0)
    • JOSM (1)
    • Simulator (0)

블로그 메뉴

    공지사항

    인기 글

    태그

    • thread
    • lock_guard
    • Future
    • JOSM
    • bit flag
    • 비트플래그
    • c++
    • async
    • 깃허브
    • multi
    • multi threading
    • Git
    • HDmap
    • ROS
    • github
    • Vector
    • 멀티스레딩
    • 깃
    • 모션플래닝
    • threading

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    daehan21

    자율주행/로봇 - 공부기록

    C++/Multi-Threading

    Task-Based Parallelism (작업 기반 비동기 프로그래밍)

    2022. 2. 11. 16:19

    이전에 배웠던 Multi-Threading는 Thread 기반으로 multi-threading을 하기 때문에

    'Thread-based multi programming'이라고 불린다.

     

    이와 다른 개념으로 Task를 기반으로 multi-threading을 하는 경우도 있는데,

    이를 'Task-based Parallelism'이라고 부른다.

     

     

     

    Thread-based multi-threading

     

    - thread 생성, 관리에 focus를 둔다

    - 아래와 같이 thread에서 다루려는 변수 result의 선언이 thread와 따로 떨어져 있어서

    다른 thread에서 의도치 않게 변경될 우려가 있다.

     

    #include <thread>
    int main() 
    {
    	int result;
    	std::thread t([&] {result = 1 + 2; });
    	t.join();
    }

     

     

    Task-based multi-threading

     

    - thread의 생성 및 관리에 초점을 두는 것이 아니라, 작업을 기반으로 thread를 나누는 데에 초점을 둔다.

    - 즉, 어떤 작업을 할 것인가에 focus를 둔다

    - 복잡한 작업을 수행해서 값이 바로 return되지 않고 언제 끝날지 모를 때 사용한다

    - <future>를 사용해서 async()로 병렬로 작업이 수행되게 한다.

    - 작업이 끝날때까지 fut.get()으로 대기한다.

    - std::async가 thread를 관리하는 옵션이 std::thread 보다 더 많아서 선호된다.

    - 즉, task-based multi-threading가 thread-based multi-threading보다 더 선호된다.

     

    #include <future>
    using namespace std;
    int main()
    {
    	// std::future<int> fut = ...
    	auto fut = std::async([] {return 1 + 2; });
    	cout << fut.get() << "\n";
    }

     

     

    코드 비교

     

    #include <iostream>
    #include <thread>
    #include <future>
    using namespace std;
    
    int main()
    {
    	// Thread-based multi-threading
    	{
    		int result;
    		std::thread t([&] {result = 1 + 2; });
    		t.join(); //thread가 위의 작업이 끝날 때까지 대기
    		cout << result << endl;
    	}
    
    
    	// std::future<int> fut = ...
    	{
    		auto fut = std::async([] {return 1 + 2; });
    		cout << fut.get() << endl; //위의 async가 끝나기 전에 이 순서가 오면, async 작업이 끝날 때 까지 대기
    	}
    }

     

    멀티스레딩 관련 글

    1. Multi-threading
    https://daehan21.tistory.com/7

    2. Task-Based Parallelism (작업 기반 비동기 프로그래밍) 
    https://daehan21.tistory.com/9

    3. Multi-threading 예제코드
    https://daehan21.tistory.com/10

    'C++ > Multi-Threading' 카테고리의 다른 글

    Multi-threading 예제코드  (0) 2022.02.12
    Multi-threading  (0) 2022.02.10
      'C++/Multi-Threading' 카테고리의 다른 글
      • Multi-threading 예제코드
      • Multi-threading
      daehan21
      daehan21

      티스토리툴바