关于多线程中代理的一个小例子


    最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档。于是我也觉的有这个必要。写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博客。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 代理引用
{
    class Program
    {
        delegate int MyDelegate(int p, int q);

        static int Add(int a, int b)
        {
            return a + b;
        }

        static void Main(string[] args)
        {
            MyDelegate arithMethod = new MyDelegate(Add);

            int r = arithMethod(3, 4);
            Console.WriteLine("3和4的结果是:{0}", r);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用 `QSemaphore` 实现多线程加载图片的示例: ```cpp #include <QApplication> #include <QWidget> #include <QVBoxLayout> #include <QHBoxLayout> #include <QLabel> #include <QImage> #include <QPixmap> #include <QFile> #include <QSemaphore> #include <QThread> #include <QDebug> const int kNumThreads = 4; // 线程数量 const int kMaxImages = 8; // 最大图片数量 class ImageLoader : public QObject { Q_OBJECT public: ImageLoader(QSemaphore *semaphore, const QString &filename, QLabel *label, QObject *parent = nullptr) : QObject(parent), semaphore_(semaphore), filename_(filename), label_(label) {} signals: void finished(); public slots: void load() { QFile file(filename_); if (file.open(QIODevice::ReadOnly)) { QImage image; if (image.load(&file)) { QPixmap pixmap = QPixmap::fromImage(image); emit imageLoaded(pixmap); } } file.close(); emit finished(); semaphore_->release(); } void setImage(const QPixmap &pixmap) { label_->setPixmap(pixmap); } private: QSemaphore *semaphore_; QString filename_; QLabel *label_; }; class ImageLoaderThread : public QThread { Q_OBJECT public: ImageLoaderThread(QSemaphore *semaphore, const QString &filename, QLabel *label, QObject *parent = nullptr) : QThread(parent), semaphore_(semaphore), filename_(filename), label_(label) {} signals: void imageLoaded(const QPixmap &pixmap); protected: void run() override { ImageLoader loader(semaphore_, filename_, label_); connect(&loader, &ImageLoader::imageLoaded, this, &ImageLoaderThread::imageLoaded); connect(&loader, &ImageLoader::finished, this, &ImageLoaderThread::quit); loader.load(); } private: QSemaphore *semaphore_; QString filename_; QLabel *label_; }; class MainWindow : public QWidget { public: MainWindow(QWidget *parent = nullptr) : QWidget(parent) { QHBoxLayout *layout = new QHBoxLayout(this); for (int i = 0; i < kMaxImages; ++i) { QLabel *label = new QLabel(this); label->setFixedSize(200, 200); layout->addWidget(label); labels_.append(label); } QThread::currentThread()->setObjectName("Main"); } ~MainWindow() { for (auto thread : threads_) { thread->quit(); thread->wait(); delete thread; } } void startLoading() { // 初始化信号量 semaphore_.release(kMaxImages); // 启动线程 for (int i = 0; i < kNumThreads; ++i) { ImageLoaderThread *thread = new ImageLoaderThread(&semaphore_, QString(":/images/%1.png").arg(i + 1), labels_.at(i)); threads_.append(thread); connect(thread, &ImageLoaderThread::imageLoaded, this, &MainWindow::onImageLoaded, Qt::QueuedConnection); thread->start(); } } public slots: void onImageLoaded(const QPixmap &pixmap) { // 查找空闲的标签 int index = -1; for (int i = 0; i < kMaxImages; ++i) { if (labels_.at(i)->pixmap() == nullptr) { index = i; break; } } // 设置图片 if (index >= 0 && index < kMaxImages) { labels_.at(index)->setPixmap(pixmap); } } private: QList<QLabel *> labels_; QList<ImageLoaderThread *> threads_; QSemaphore semaphore_; }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow window; window.show(); window.startLoading(); return app.exec(); } #include "main.moc" ``` 在这个例子,我们创建了一个 `ImageLoader` 类,负责从文件加载图片,并将加载完成的图片转换成 `QPixmap` 类型,然后通过信号将图片传递给线程。`ImageLoaderThread` 类继承自 `QThread`,负责在子线程创建 `ImageLoader` 对象,并连接信号槽,将加载完成的图片发送给主线程。主线程创建了多个 `ImageLoaderThread` 对象,并启动线程,等待图片加载完成后将图片显示在 QLabel 控件。为了控制最大并发数,我们使用了一个 `QSemaphore` 对象,每个线程在加载图片前申请一个信号量,加载完成后释放信号量。如果信号量已经全部被占用,则线程会阻塞在 `acquire()` 函数处,等待有空闲的信号量。这样就可以限制线程的并发数量,防止过多的线程同时加载图片导致系统资源耗尽。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值