동시 작업
QtConcurrent::task 는 별도의 스레드에서 작업을 실행하기 위한 대체 인터페이스를 제공합니다. 함수의 반환 값은 QFuture API를 통해 제공됩니다.
매개변수를 조정하지 않고 별도의 스레드에서 함수만 실행하려면 QtConcurrent::run 을 사용하면 코드를 더 적게 작성할 수 있습니다. QtConcurrent::task 은 추가 구성 단계를 수행해야 하는 경우를 위해 설계되었습니다.
이 함수는 Qt Concurrent 프레임워크의 일부입니다.
유창한 인터페이스
QtConcurrent::task 은 QtConcurrent::QTaskBuilder 이라는 보조 클래스의 인스턴스를 반환합니다. 일반적으로 이 클래스의 인스턴스를 수동으로 만들 필요는 없습니다. QtConcurrent::QTaskBuilder 은 다양한 작업 매개변수를 체인처럼 조정할 수 있는 인터페이스를 제공합니다. 이 접근 방식을 유창한 인터페이스라고 합니다.
필요한 매개변수를 설정한 다음 작업을 시작하기만 하면 됩니다. 작업 구성을 완료하려면 QtConcurrent::QTaskBuilder::spawn 을 호출해야 합니다. 이 함수는 비블록킹(즉, 미래의 객체를 즉시 반환)이지만 작업이 즉시 시작된다는 보장은 없습니다. QFuture 및 QFutureWatcher 클래스를 사용하여 작업의 상태를 모니터링할 수 있습니다.
아래에서 더 많은 예와 설명을 참조하세요.
별도의 스레드에서 작업 실행하기
다른 스레드에서 함수를 실행하려면 QtConcurrent::QTaskBuilder::spawn 을 사용하세요:
QtConcurrent::task([]{ qDebug("Hello, world!"); }).spawn();
이렇게 하면 기본값 QThreadPool 에서 얻은 별도의 스레드에서 람다 함수가 실행됩니다.
태스크에 인수 전달하기
인수가 있는 함수를 호출하려면 QtConcurrent::QTaskBuilder::withArguments 에 인수를 전달하면 됩니다:
auto task = [](const QString &s){ qDebug() << ("Hello, " + s); }; QtConcurrent::task(std::move(task)) .withArguments("world!") .spawn();
QtConcurrent::QTaskBuilder::withArguments 이 호출되는 지점에서 각 인수의 복사본이 만들어지고, 이 값은 작업 실행을 시작할 때 스레드에 전달됩니다. QtConcurrent::QTaskBuilder::withArguments 호출 후 인수를 변경한 내용은 스레드에 표시되지 않습니다.
참조로 인수를 받는 함수를 실행하려면 std::ref/cref 보조 함수를 사용해야 합니다. 이러한 함수는 전달된 인수를 얇게 감싸는 래퍼를 생성합니다:
QString s("Hello, "); QtConcurrent::task([](QString &s){ s.append("world!"); }) .withArguments(std::ref(s)) .spawn();
래핑된 모든 객체가 충분히 오래 살 수 있는지 확인하세요. 작업의 수명이 std::ref/cref로 래핑된 객체보다 길면 정의되지 않은 동작이 발생할 수 있습니다.
태스크에서 값 반환하기
QFuture API로 태스크의 결과를 얻을 수 있습니다:
auto future = QtConcurrent::task([]{ return 42; }).spawn(); auto result = future.result(); // result == 42
QFuture::result()는 차단 호출이므로 결과를 사용할 수 있을 때까지 기다립니다. 작업 실행이 완료되어 결과를 사용할 수 있게 되면 QFutureWatcher 을 사용하여 알림을 받습니다.
결과를 다른 비동기 작업으로 전달하려는 경우 QFuture::then()를 사용하여 종속 작업 체인을 만들 수 있습니다. 자세한 내용은 QFuture 문서를 참조하세요.
추가 API 기능
다양한 유형의 호출 가능한 객체 사용
엄밀히 말하면 다음 조건을 만족하는 모든 유형의 작업과 인수를 사용할 수 있습니다:
std::is_invocable_v<std::decay_t<Task>, std::decay_t<Args>...>
자유 함수를 사용할 수 있습니다:
QVariant value(42); auto result = QtConcurrent::task([](const QVariant &var){return qvariant_cast<int>(var);}) .withArguments(value) .spawn() .result(); // result == 42
멤버 함수를 사용할 수 있습니다:
QString result("Hello, world!"); QtConcurrent::task(&QString::chop) .withArguments(&result, 8) .spawn() .waitForFinished(); // result == "Hello"
연산자()와 함께 호출 가능한 객체를 사용할 수 있습니다:
auto result = QtConcurrent::task(std::plus<int>()) .withArguments(40, 2) .spawn() .result() // result == 42
기존 호출 가능한 객체를 사용하려면 QtConcurrent::task 으로 복사/이동하거나 std::ref/cref로 래핑해야 합니다:
struct CallableWithState { void operator()(int newState) { state = newState; } // ... }; // ... CallableWithState object; QtConcurrent::task(std::ref(object)) .withArguments(42) .spawn() .waitForFinished(); // The object's state is set to 42
사용자 정의 스레드 풀 사용
사용자 지정 스레드 풀을 지정할 수 있습니다:
QThreadPool pool; QtConcurrent::task([]{ return 42; }).onThreadPool(pool).spawn();
작업의 우선순위 설정하기
작업의 우선순위를 설정할 수 있습니다:
QtConcurrent::task([]{ return 42; }).withPriority(10).spawn();
미래 객체가 필요하지 않은 경우 QtConcurrent::QTaskBuilder::spawn(QtConcurrent::FutureResult::Ignore)를 호출할 수 있습니다:
QtConcurrent::task([]{ qDebug("Hello, world!"); }).spawn(FutureResult::Ignore);
함수 안에 QPromise<T> &
유형의 추가 인수를 정의하여 작업과 연결된 프라미스 개체에 액세스할 수 있습니다. 이 추가 인수는 함수에 전달되는 첫 번째 인수가 되어야 하며, 약속과 함께 동시 실행 모드와 마찬가지로 함수는 무효 유형을 반환할 것으로 예상됩니다. 결과 보고는 QPromise API를 통해 이루어집니다:
void increment(QPromise<int> &promise, int i) { promise.addResult(i + 1); } int result = QtConcurrent::task(&increment).withArguments(10).spawn().result(); // result == 11
© 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.