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

照亮你的路灯
发布于 2024-4-19 23:38
浏览
0收藏

概述

简介

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

#include <safe_block_queue.h>

涉及功能

接口说明

OHOS::SafeBlockQueue

OpenHarmony开发-线程安全阻塞队列-鸿蒙开发者社区

OHOS::SafeBlockQueueTracking

class SafeBlockQueueTracking : public SafeBlockQueue

OpenHarmony开发-线程安全阻塞队列-鸿蒙开发者社区

使用示例

  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;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

1
收藏
回复
举报
1


回复
    相关推荐