이 페이지에서

QMutexLocker Class

template <typename Mutex> class QMutexLocker

QMutexLocker 클래스는 뮤텍스의 잠금 및 잠금 해제를 간편하게 해주는 편의 클래스입니다. 더 보기...

헤더: #include <QMutexLocker>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

참고: 이 클래스의 모든 함수는 스레드 안전합니다.

공개 함수

QMutexLocker(Mutex *mutex)
(since 6.4) QMutexLocker(QMutexLocker<Mutex> &&other)
~QMutexLocker()
(since 6.4) bool isLocked() const
Mutex *mutex() const
void relock()
(since 6.4) void swap(QMutexLocker<Mutex> &other)
void unlock()
(since 6.4) QMutexLocker<Mutex> &operator=(QMutexLocker<Mutex> &&other)

상세 설명

QMutexLocker는 템플릿 매개 변수 Mutex 가 잠금 및 잠금 해제가 이루어질 뮤텍스 유형(예: QMutex 또는 QRecursiveMutex)을 지정하는 템플릿 클래스입니다.

복잡한 함수 및 문장이나 예외 처리 코드에서 QMutex 또는 QRecursiveMutex 를 잠그거나 잠금을 해제하는 작업은 오류가 발생하기 쉬우며 디버깅하기 어렵습니다. 이러한 상황에서 QMutexLocker를 사용하면 뮤텍스의 상태가 항상 명확하게 정의되도록 할 수 있습니다.

QMutexLocker는 QMutex 를 잠글 필요가 있는 함수 내에서 생성해야 합니다. QMutexLocker가 생성되면 뮤텍스가 잠깁니다. unlock() 및 relock() 를 사용하여 뮤텍스의 잠금을 해제하고 다시 잠글 수 있습니다. 잠겨 있는 경우, QMutexLocker가 소멸되면 뮤텍스의 잠금이 해제됩니다.

예를 들어, 이 복잡한 함수는 함수 진입 시 QMutex 를 잠그고, 모든 종료 지점에서 뮤텍스를 잠금 해제합니다:

int complexFunction(int flag)
{
    mutex.lock();

    int retVal = 0;

    switch (flag) {
    case 0:
    case 1:
        retVal = moreComplexFunction(flag);
        break;
    case 2:
        {
            int status = anotherFunction();
            if (status < 0) {
                mutex.unlock();
                return -2;
            }
            retVal = status + flag;
        }
        break;
    default:
        if (flag > 10) {
            mutex.unlock();
            return -1;
        }
        break;
    }

    mutex.unlock();
    return retVal;
}

이 예제 함수는 개발이 진행됨에 따라 더욱 복잡해질 것이며, 이로 인해 오류가 발생할 가능성도 높아집니다.

QMutexLocker를 사용하면 코드가 크게 단순화되고 가독성이 향상됩니다:

int complexFunction(int flag)
{
    QMutexLocker locker(&mutex);

    int retVal = 0;

    switch (flag) {
    case 0:
    case 1:
        return moreComplexFunction(flag);
    case 2:
        {
            int status = anotherFunction();
            if (status < 0)
                return -2;
            retVal = status + flag;
        }
        break;
    default:
        if (flag > 10)
            return -1;
        break;
    }

    return retVal;
}

이제 QMutexLocker 객체가 소멸될 때( locker 는 auto 변수이므로 함수가 반환될 때) 뮤텍스는 항상 잠금 해제됩니다.

이 같은 원리는 예외를 발생시키고 처리하는 코드에도 적용됩니다. 뮤텍스를 잠근 함수 내에서 처리되지 않은 예외는, 예외가 스택을 따라 호출 함수로 전달되기 전에 뮤텍스를 해제할 방법이 없습니다.

QMutexLocker는 또한 QMutexLocker가 조작 중인 뮤텍스를 반환하는 mutex() 멤버 함수를 제공합니다. 이는 QWaitCondition::wait()과 같이 뮤텍스에 접근해야 하는 코드에 유용합니다. 예를 들어:

class SignalWaiter
{
private:
    QMutexLocker<QMutex> locker;

public:
    SignalWaiter(QMutex *mutex)
        : locker(mutex)
    {
    }

    void waitForSignal()
    {
        //...
        while (!signalled)
            waitCondition.wait(locker.mutex());
        //...
    }
};

QReadLocker, QWriteLocker 및 QMutex도 참조하십시오 .

멤버 함수 문서

[explicit noexcept(...)] QMutexLocker::QMutexLocker(Mutex *mutex)

QMutexLocker 객체를 생성하고 mutex 을 잠급니다. QMutexLocker가 소멸되면 뮤텍스가 해제됩니다. mutex 가 nullptr 인 경우, QMutexLocker는 아무 작업도 수행하지 않습니다.

참고: LockIsNoexcept 가 true 인 경우,이 함수는 noexcept입니다.

참조: QMutex::lock() 함수도 참조하십시오 .

[noexcept, since 6.4] QMutexLocker::QMutexLocker(QMutexLocker<Mutex> &&other)

other 에서 QMutexLocker를 이동 생성합니다. 뮤텍스와 other 의 상태가 새로 생성된 인스턴스로 이전됩니다. 이동이 완료된 후, other 는 더 이상 어떤 뮤텍스도 관리하지 않게 됩니다.

이 함수는 Qt 6.4에서 도입되었습니다.

QMutex::lock()도 참조하십시오 .

QMutexLocker::~QMutexLocker()

QMutexLocker 를 파기하고, 생성자에서 잠겨 있던 뮤텍스를 해제합니다.

QMutex::unlock()도 참조하십시오 .

[noexcept, since 6.4] bool QMutexLocker::isLocked() const

이 ` QMutexLocker `가 현재 관련 뮤텍스를 잠그고 있는 경우 true를 반환하고, 그렇지 않은 경우 false를 반환합니다.

이 함수는 Qt 6.4에서 도입되었습니다.

Mutex *QMutexLocker::mutex() const

QMutexLocker 가 작동 중인 뮤텍스를 반환합니다.

[noexcept(...)] void QMutexLocker::relock()

잠금 해제된 뮤텍스 잠금 장치를 다시 잠급니다.

참고: LockIsNoexcept 가 true 인 경우,이 함수는 noexcept입니다.

unlock()도 참조하십시오 .

[noexcept, since 6.4] void QMutexLocker::swap(QMutexLocker<Mutex> &other)

이 ` QMutexLocker `의 뮤텍스와 상태를 ` other`와 교환합니다. 이 작업은 매우 빠르며 절대 실패하지 않습니다.

이 함수는 Qt 6.4에서 도입되었습니다.

QMutex::lock()도 참조하십시오 .

[noexcept] void QMutexLocker::unlock()

이 뮤텍스 락커의 잠금을 해제합니다. ` relock() `를 사용하여 다시 잠글 수 있습니다. 소멸될 때는 잠글 필요가 없습니다.

relock()도 참조하십시오 .

[noexcept, since 6.4] QMutexLocker<Mutex> &QMutexLocker::operator=(QMutexLocker<Mutex> &&other)

이동 할당(move-assign)을 통해 other 를 이 QMutexLocker 로 할당합니다. 만약 이 QMutexLocker 가 할당 전에 잠긴 뮤텍스를 보유하고 있었다면, 해당 뮤텍스는 잠금이 해제됩니다. 그런 다음 뮤텍스와 other 의 상태가 이 QMutexLocker 로 이전됩니다. 이동이 완료된 후, other 는 더 이상 어떤 뮤텍스도 관리하지 않게 됩니다.

이 함수는 Qt 6.4에서 도입되었습니다.

QMutex::lock()도 참조하십시오 .

© 2026 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.