mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-06 17:25:24 +08:00
qt 6.5.1 original
This commit is contained in:
29
examples/corelib/threads/semaphores/CMakeLists.txt
Normal file
29
examples/corelib/threads/semaphores/CMakeLists.txt
Normal file
@ -0,0 +1,29 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(semaphores LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/threads/semaphores")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(semaphores
|
||||
semaphores.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(semaphores PRIVATE
|
||||
Qt6::Core
|
||||
)
|
||||
|
||||
install(TARGETS semaphores
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
65
examples/corelib/threads/semaphores/semaphores.cpp
Normal file
65
examples/corelib/threads/semaphores/semaphores.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtCore>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
//! [0]
|
||||
const int DataSize = 100000;
|
||||
|
||||
const int BufferSize = 8192;
|
||||
char buffer[BufferSize];
|
||||
|
||||
QSemaphore freeBytes(BufferSize);
|
||||
QSemaphore usedBytes;
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
class Producer : public QThread
|
||||
//! [1] //! [2]
|
||||
{
|
||||
public:
|
||||
void run() override
|
||||
{
|
||||
for (int i = 0; i < DataSize; ++i) {
|
||||
freeBytes.acquire();
|
||||
buffer[i % BufferSize] = "ACGT"[QRandomGenerator::global()->bounded(4)];
|
||||
usedBytes.release();
|
||||
}
|
||||
}
|
||||
};
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
class Consumer : public QThread
|
||||
//! [3] //! [4]
|
||||
{
|
||||
public:
|
||||
void run() override
|
||||
{
|
||||
for (int i = 0; i < DataSize; ++i) {
|
||||
usedBytes.acquire();
|
||||
fprintf(stderr, "%c", buffer[i % BufferSize]);
|
||||
freeBytes.release();
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
};
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
int main(int argc, char *argv[])
|
||||
//! [5] //! [6]
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
Producer producer;
|
||||
Consumer consumer;
|
||||
producer.start();
|
||||
consumer.start();
|
||||
producer.wait();
|
||||
consumer.wait();
|
||||
return 0;
|
||||
}
|
||||
//! [6]
|
8
examples/corelib/threads/semaphores/semaphores.pro
Normal file
8
examples/corelib/threads/semaphores/semaphores.pro
Normal file
@ -0,0 +1,8 @@
|
||||
SOURCES += semaphores.cpp
|
||||
QT = core
|
||||
|
||||
CONFIG += cmdline
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/corelib/threads/semaphores
|
||||
INSTALLS += target
|
Reference in New Issue
Block a user