Files
phs_v1.0.1.0/commonlibrary/c_utils/docs/zh-cn/c-utils-guide-safeMap.md
2024-09-27 19:16:49 +08:00

2.8 KiB
Raw Blame History

线程安全map

概述

简介

提供了一个线程安全的map实现。SafeMap在STL map基础上封装互斥锁以确保对map的操作安全。

涉及功能

class SafeMap

接口说明

返回类型 名称
SafeMap()
SafeMap(const SafeMap& rhs)
~SafeMap()
void Clear()
删除map中存储的所有键值对。
void EnsureInsert(const K& key, const V& value)
在map中插入元素。
void Erase(const K& key)
删除map中键为key的键值对。
bool Find(const K& key, V& value)
在map中查找元素。
bool FindOldAndSetNew(const K& key, V& oldValue, const V& newValue)
在map中查找元素并将key对应的oldValue替换为newValue
bool Insert(const K& key, const V& value)
在map中插入新元素。
bool IsEmpty()
判断map是否为空。
void Iterate(const SafeMapCallBack& callback)
遍历map中的元素。
SafeMap& operator=(const SafeMap& rhs)
V& operator[](const K& key)
int Size()
获取map的size大小。

使用示例

  1. 示例代码(伪代码)
#include <thread>
#include <iostream>
#include <functional>
#include "../include/safe_map.h"

using namespace OHOS;
using namespace std;

constexpr int THREAD_NUM = 5;
thread threads[THREAD_NUM * 2];
SafeMap<int, string> sm;

bool InsertHandler(const int& key, const string& value)
{
    if (sm.Insert(key,value)) {
        cout << "Insert key: " << key << endl;
        return true;
    } else {
        return false;
    }
}

bool FindHandler(const int& key, string& value)
{
    if (sm.Find(key,value)) {
        cout << "Find key: " << key << " with value: " << value << endl;
        return true;
    } else {
        return false;
    }
}


int main()
{
    for (int i = 0; i < THREAD_NUM; i++) {
        string name = "Thread" + to_string(i);
        threads[i] = thread(&InsertHandler, i, name);
    }

    for (int i = 0; i < THREAD_NUM; i++) {
        string out;
        threads[i + THREAD_NUM] = thread(&FindHandler, i, ref(out));
    }

    this_thread::sleep_for(chrono::milliseconds(300));

    for (int i = 0; i < 2 * THREAD_NUM; i++) {
        threads[i].join();
    }


    if (sm.Size() == THREAD_NUM) {
        cout << "Insert to SafeMap success!" << endl;
    }
}
  1. 测试用例编译运行方法
  • 测试用例代码参见base/test/unittest/common/utils_safe_map_test.cpp

  • 使用开发者自测试框架,使用方法参见:开发自测试执行框架-测试用例执行

  • 使用以下具体命令以运行safe_map.h对应测试用例

run -t UT -tp utils -ts UtilsSafeMapTest