OpenHarmony开发-线程安全阻塞队列

概述

简介

线程安全阻塞队列SafeBlockQueue类,提供阻塞和非阻塞版的入队入队和出队接口,并提供可最追踪任务完成状态的的SafeBlockQueueTracking类。

#include <safe_block_queue.h>

涉及功能

接口说明

OHOS::SafeBlockQueue

img

OHOS::SafeBlockQueueTracking

class SafeBlockQueueTracking : public SafeBlockQueue

img

使用示例

  1. 示例代码(伪代码)
  • SafeBlockQueue的示例代码
#include <thread>
#include <functional>
#include <iostream>
#include "../include/safe_block_queue.h"

using namespace OHOS;
using namespace std;

constexpr int SIZE = 10;

class ProductsLine
{
public:
    ProductsLine(int maxSize) : que(maxSize) {}

    void Produce()
    {
        for (int i = 0; i < SIZE + 1; i++) {
            que.Push(i);
            cout << "Add " << i << " to the line" << endl;
        }
    }

    void Consume()
    {
        for (int i = 0; i < SIZE + 1; i++) {
            int out = que.Pop();
            cout << "Get " << out << " from the line" << endl;
        }
    }

    int remains()
    {
        return que.Size();
    }

private:
    SafeBlockQueue<int> que;
};

int main()
{
    ProductsLine line(SIZE);

    thread producer(bind(&ProductsLine::Produce, ref(line)));
    this_thread::sleep_for(chrono::milliseconds(1));

    thread consumer(bind(&ProductsLine::Consume, ref(line)));
    this_thread::sleep_for(chrono::milliseconds(1));

    producer.join();
    consumer.join();

    if (line.remains()==0) {
         cout << line.remains() << " elements remains in the queue. Synchronizing success." <<endl;
    }
}
  • SafeBlockQueueTracking的示例代码
#include <thread>
#include <functional>
#include <iostream>
#include "../include/safe_block_queue.h"

using namespace OHOS;
using namespace std;

constexpr int SIZE = 10;

class ProductsLine
{
public:
    ProductsLine(int maxSize) : que(maxSize) {}

    void Produce()
    {
        for (int i = 0; i < SIZE + 1; i++) {
            que.Push(i);
            cout << "Add " << i << " to the line" << endl;
        }
    }

    void Consume()
    {
        for (int i = 0; i < SIZE + 1; i++) {
            int out = que.Pop();
            cout << "Get " << out << " from the line" << endl;
            que.OneTaskDone();
        }
    }

    void Join()
    {
        que.Join();
    }

    int UnfinishTaskNum()
    {
        return que.GetUnfinishTaskNum();
    }

private:
    SafeBlockQueueTracking<int> que;
};

int main()
{
    ProductsLine line(SIZE);

    thread producer(bind(&ProductsLine::Produce, ref(line)));
    this_thread::sleep_for(chrono::milliseconds(1));

    thread consumer(bind(&ProductsLine::Consume, ref(line)));
    this_thread::sleep_for(chrono::milliseconds(1));

    line.Join();

    producer.join();
    consumer.join();

    if (line.UnfinishTaskNum()==0) {
         cout << line.UnfinishTaskNum() << " elements remains in the queue. Synchronizing success." <<endl;
    }
}
  1. 测试用例编译运行方法
  • 测试用例代码参见base/test/unittest/common/utils_safe_block_queue_test.cpp 和 base/test/unittest/common/utils_safe_block_queue_tracking.cpp
  • 使用开发者自测试框架,使用方法参见:开发自测试执行框架-测试用例执行
  • 使用以下具体命令以运行safe_block_queue.h对应测试用例
run -t UT -tp utils -ts UtilsSafeBlockQueueTest

# or

run -t UT -tp utils -ts UtilsSafeBlockQueueTrackingTest

写在最后

最后分享一份鸿蒙(HarmonyOS)开发学习指南需要的可以扫码免费领取!!!

《鸿蒙(HarmonyOS)开发学习指南》

第一章 快速入门

1、开发准备

2、构建第一个ArkTS应用(Stage模型)

3、构建第一个ArkTS应用(FA模型)

4、构建第一个JS应用(FA模型)

5、…

图片

第二章 开发基础知识

1、应用程序包基础知识

2、应用配置文件(Stage模型)

3、应用配置文件概述(FA模型)

4、…

图片

第三章 资源分类与访问

1、 资源分类与访问

2、 创建资源目录和资源文件

3、 资源访问

4、…

图片

第四章 学习ArkTs语言

1、初识ArkTS语言

2、基本语法

3、状态管理

4、其他状态管理

5、渲染控制

6、…

图片

第五章 UI开发

1.方舟开发框架(ArkUI)概述

2.基于ArkTS声明式开发范式

3.兼容JS的类Web开发范式

4…

图片

第六章 Web开发

1.Web组件概述

2.使用Web组件加载页面

3.设置基本属性和事件

4.在应用中使用前端页面JavaScript

5.ArkTS语言基础类库概述

6.并发

7…

图片

11.网络与连接

12.电话服务

13.数据管理

14.文件管理

15.后台任务管理

16.设备管理

17…

图片

第七章 应用模型

1.应用模型概述

2.Stage模型开发指导

3.FA模型开发指导

4…

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值