단어 수

지도 축소 알고리즘을 사용하는 방법을 보여줍니다.

Qt Concurrent 단어 수 예제는 파일 모음에서 단어 수를 세는 문제에 맵 축소 알고리즘을 적용할 때 사용하는 방법을 보여줍니다.

먼저 애플리케이션이 QFileDialog를 시작하여 시작 경로를 선택한 다음 콘솔에 출력을 인쇄합니다.

예제 실행하기

에서 예제를 실행하려면 Qt Creator에서 예제를 실행하려면 Welcome 모드를 열고 Examples 에서 예제를 선택합니다. 자세한 내용은 예제 빌드 및 실행하기를 참조하세요.

작업 비교하기

텍스트 파일의 단어를 순차적으로 계산하는 단일 스레드 접근 방식과 맵핑된Reduced()를 사용하는 멀티 스레드 접근 방식을 비교합니다:

    ...
    WordCount total = singleThreadedWordCount(files);
    ...
    WordCount total = QtConcurrent::mappedReduced(files, countWords, reduce).result();
    ...

mappedReduced 함수의 첫 번째 인수는 연산할 컨테이너입니다. 두 번째 인수는 매핑 함수 countWords() 입니다. 이 함수는 여러 스레드에서 병렬로 호출됩니다. 세 번째 인수는 환원 함수 reduce() 입니다. 이 함수는 매핑 함수가 반환하는 각 결과에 대해 한 번씩 호출되며 최종 계산 결과를 생성합니다.

이 함수는 WordCount 유형의 QFuture 객체를 반환합니다. 이 QFuture 에서 즉시 result 함수를 호출하여 결과를 사용할 수 있을 때까지 추가 실행을 차단합니다.

참고: 매핑 함수는 여러 스레드에서 호출되므로 스레드 안전해야 합니다.

예제 프로젝트 @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.