该程序是基于OpenHarmony标准系统的C++公共基础类库的线程处理:Sempahore。
本案例完成如下工作:
(1)无名信号量使用方法
定义1个无名信号量,1个供无名信号量管理的公共资源变量;
创建5个线程,每个线程做5次for循环,for循环的内容是获取无名信号量,并修改公共资源变量;
(2)有名信号量使用方法
定义1个有名信号量,1个供有名信号量管理的公共资源变量;
创建1个线程A,通过Open获取信号量,做5次for循环,for循环的内容是通过Wait获取有名信号量,如果获取成功则修改公共资源变量(即累加1),最后释放信号量;
创建1个线程B,通过Open获取信号量,做5次for循环,for循环的内容是通过TryWait获取有名信号量,如果获取成功则修改公共资源变量(即累加10),最后释放信号量;
创建1个线程C,通过Open获取信号量,做5次for循环,for循环的内容是通过TimedWait获取有名信号量,如果获取成功则修改公共资源变量(即累加100),最后释放信号量;
C++公共基础类库为标准系统提供了一些常用的C++开发工具类,包括:
文件、路径、字符串相关操作的能力增强接口
读写锁、信号量、定时器、线程增强及线程池等接口
安全数据容器、数据序列化等接口
各子系统的错误码相关定义
修改需调用模块的BUILD.gn,在external_deps或deps中添加如下:
ohos_shared_library("xxxxx") { ... external_deps = [ ... # 动态库依赖(可选) "c_utils:utils", # 静态库依赖(可选) "c_utils:utilsbase", # Rust动态库依赖(可选) "c_utils:utils_rust", ] ...}
一般而言,我们只需要填写"c_utils:utils"即可。
C++公共基础类库的Semaphore头文件在://commonlibrary/c_utils/base/include/semaphore_ex.h
可在源代码中添加如下:
#include
OpenHarmony信号量根据种类可以分为有名信号量和无名信号量,所以命令空间如下:
(1)无名信号量命名空间
OHOS::Semaphore
(2)有名信号量命名空间
OHOS::NamedSemaphore
Semaphore为无名信号量。
构造函数, 构造一个Samaphore对象。
Semaphore(int value = 1);
参数说明:
| 参数名称 | 类型 | 参数说明 |
|---|---|---|
| value | int | 信号量当前资源数量 |
析构函数。
~Semaphore();
等待/获取信号量(即信号量 -1)。
void Wait();
释放信号量(即信号量 +1)。
void Post();
NamedSemaphore为有名信号量。
构造函数, 构造NamedSemaphore对象。
NamedSemaphore(size_t size)NamedSemaphore(const std::string& name, size_t size)
参数说明:
| 参数名称 | 类型 | 参数说明 |
|---|---|---|
| name | std::string | 信号量名称 |
| size | size_t | 信号量有效资源数量 |
析构函数。
~NamedSemaphore();
创建并初始化有名信号量。
bool Create();
返回值说明:
| 类型 | 返回值说明 |
|---|---|
| bool | true表示成功,false表示失败 |
将有名信号量文件从系统中删除。
bool Unlink();
返回值说明:
| 类型 | 返回值说明 |
|---|---|
| bool | true表示成功,false表示失败 |
打开一个已经创建的有名信号量文件。
bool Open();
返回值说明:
| 类型 | 返回值说明 |
|---|---|
| bool | true表示成功,false表示失败 |
关闭有名信号量。
bool Close();
返回值说明:
| 类型 | 返回值说明 |
|---|---|
| bool | true表示成功,false表示失败 |
等待/获取信号量(信号量 -1)。
bool Wait();
返回值说明:
| 类型 | 返回值说明 |
|---|---|
| bool | true表示成功,false表示失败 |
等待/获取信号量(信号量 -1)的接口;非阻塞版。
bool TryWait();
返回值说明:
| 类型 | 返回值说明 |
|---|---|
| bool | true表示成功,false表示失败 |
等待/获取信号量(信号量 -1);指定阻塞时间版。
bool TimedWait(const struct timespec& ts);
参数说明:
| 参数名称 | 类型 | 参数说明 |
|---|---|---|
| ts | struct timespec | 绝对时间。注意:ts是utc时间,不是相对时间。 |
返回值说明:
| 类型 | 返回值说明 |
|---|---|
| bool | true表示成功,false表示失败 |
释放信号量(信号量 +1)。
bool Post();
返回值说明:
| 类型 | 返回值说明 |
|---|---|
| bool | true表示成功,false表示失败 |
获取信号的值。
int GetValue() const;
返回值说明:
| 类型 | 返回值说明 |
|---|---|
| int | 返回当前信号量的值 |
在//vendor/lockzhiner/rk3568/samples/BUILD.gn文件添加一行编译引导语句。
import("//build/ohos.gni")
group("samples") { deps = [ "a24_utils_semaphore:utils_semaphore", # 添加该行 ]}
"a24_utils_semaphore:utils_semaphore",该行语句表示引入参与编译。
创建a24_utils_semaphore目录,并添加如下文件:
a24_utils_semaphore├── utils_name_semaphore.cpp # 有名信号量案例├── utils_noname_semaphore.cpp # 无名信号量案例├── BUILD.gn # GN文件
编辑BUILD.gn文件。
添加2个可执行程序,分别是:
utils_noname_semaphore:无名信号量使用案例
utils_name_semaphore:有名信号量使用案例
import("//build/ohos.gni")
ohos_executable("utils_noname_semaphore") { sources = [ "utils_noname_semaphore.cpp" ] include_dirs = [ "//commonlibrary/c_utils/base/include", "//commonlibrary/c_utils/base:utils", "//third_party/googletest:gtest_main", "//third_party/googletest/googletest/include", ] external_deps = [ "c_utils:utils" ] part_name = "product_rk3568" install_enable = true}
ohos_executable("utils_name_semaphore") { sources = [ "utils_name_semaphore.cpp" ] include_dirs = [ "//commonlibrary/c_utils/base/include", "//commonlibrary/c_utils/base:utils", "//third_party/googletest:gtest_main", "//third_party/googletest/googletest/include", ] external_deps = [ "c_utils:utils" ] part_name = "product_rk3568" install_enable = true}
group("utils_semaphore") { deps = [ ":utils_noname_semaphore", ":utils_name_semaphore", ]}
注意:
(1)BUILD.gn中所有的TAB键必须转化为空格,否则会报错。如果自己不知道如何规范化,可以:
# 安装gn工具sudo apt-get install ninja-buildsudo apt install generate-ninja# 规范化BUILD.gngn format BUILD.gn
#include
static int m_count = 0; // 公共资源变量static OHOS::Semaphore m_sem(1); // 无名信号量
OHOS::ThreadPool threads("noname_semaphore_threads");
OHOS::ThreadPool threads("noname_semaphore_threads");
for (int i = 0; i < threads_start; i++) { string str_name = "thread_" + to_string(i); auto task = std::bind(func, str_name); threads.AddTask(task); sleep(1);}
循环5次,每次循环调用信号量Wait()等待获取信号量。如果获取信号量后,将公共资源变量累加,调用信号量Post()释放信号量。
void func(const string &name){ for (int i = 0; i < 5; i++) { cout << name << ": Sema Wait..." << endl; m_sem.Wait(); cout << name << ": Sema Wait Successful" << endl; m_count += 1; m_sem.Post(); cout << name << ": Sema Post" << endl; sleep(1); }}
#include
首先定义有名信号量,然后通过Create()创建全局有名信号量。
int main(int argc, char **argv){ OHOS::NamedSemaphore sem(STRING_NAME_SEMAPHORE, 1); ...... if (!sem.Create()) { cout << "NamedSemaphore.Create() failed\n"; return -1; } ......}
通过OHOS::ThreadPool定义线程池,调用SetMaxTaskNum()设置线程池最大线程数,并调用Start()设置当前启动多少个线程。
int main(int argc, char **argv){ OHOS::ThreadPool threads("name_semaphore_threads"); int threads_start = 3; ...... threads.SetMaxTaskNum(128); threads.Start(threads_start); ......}
int main(int argc, char **argv){ ...... // 启动线程A str_name = "thread_a"; auto task_a = std::bind(funcA, str_name); threads.AddTask(task_a); // 启动线程B str_name = "thread_b"; auto task_b = std::bind(funcB, str_name); threads.AddTask(task_b);
// 启动线程A str_name = "thread_c"; auto task_c = std::bind(funcC, str_name); threads.AddTask(task_c); threads.Stop(); cout << "threads stop" << endl; return 0;}
首先定义有名信号量,信号量数目可以随意设置。
其次,通过Open()打开有名信号量,可以与main()的有名信号量共享同一个信号量。
最后,通过Wait()和Post()来获取释放信号量。
static void funcA(const string &name){ OHOS::NamedSemaphore sem(STRING_NAME_SEMAPHORE, 1);
cout << get_curtime() << ", " << name << ": start\n";
// 打开一个已经创建的有名信号量文件 if (!sem.Open()) { cout << get_curtime() << ", " << name << ": sema open failed" << endl; return; } for (int i = 0; i < 5; i++) { cout << get_curtime() << ", " << name << ": sema wait..." << endl; sem.Wait(); cout << get_curtime() << ", " << name << ": sema wait success" << endl; m_count += 1; usleep(1000 * 1000 * 1); cout << get_curtime() << ", " << name << ": sema count = " << m_count << endl; sem.Post(); cout << get_curtime() << ", " << name << ": sema post and sleep 1 sec" << endl; sleep(1); }
cout << get_curtime() << ", " << name << ": end" << endl;}
首先定义有名信号量,信号量数目可以随意设置。
其次,通过Open()打开有名信号量,可以与main()的有名信号量共享同一个信号量。
最后,通过TryWait()和Post()来获取释放信号量。
static void funcB(const string &name){ OHOS::NamedSemaphore sem(STRING_NAME_SEMAPHORE, 1);
cout << get_curtime() << ", " << name << ": start\n"; // 打开一个已经创建的有名信号量文件 if (!sem.Open()) { cout << get_curtime() << ", " << name << ": sema open failed" << endl; return; }
for (int i = 0; i < 5; i++) { cout << get_curtime() << ", " << name << ": sema trywait..." << endl; if (sem.TryWait()) { cout << get_curtime() << ", " << name << ": sema trywait success" << endl; m_count += 10; usleep(1000 * 1000 * 1); cout << get_curtime() << ", " << name << ": sema count = " << m_count << endl; sem.Post(); cout << get_curtime() << ", " << name << ": sema post and sleep 1 sec" << endl; } else { cout << get_curtime() << ", " << name << ": sema tryWait failed and sleep 1 sec" << endl; } sleep(1); }
cout << get_curtime() << ", " << name << ": end" << endl;}
首先定义有名信号量,信号量数目可以随意设置。
其次,通过Open()打开有名信号量,可以与main()的有名信号量共享同一个信号量。
最后,通过TimedWait()和Post()来获取释放信号量。
static void funcC(const string &name){ OHOS::NamedSemaphore sem(STRING_NAME_SEMAPHORE, 1); struct timespec ts;
cout << get_curtime() << ", " << name << ": start\n";
// 打开一个已经创建的有名信号量文件 if (!sem.Open()) { cout << get_curtime() << ", " << name << ": sema open failed" << endl; return; } for (int i = 0; i < 5; i++) { clock_gettime(CLOCK_REALTIME, &ts); // 超时等待时间,1秒 ts.tv_sec += 1; cout << get_curtime() << ", " << name << ": sema timedwait 1 sec..." << endl; if (sem.TimedWait(ts)) { cout << get_curtime() << ", " << name << ": sema timedwait success" << endl; m_count += 100; usleep(1000 * 100); cout << get_curtime() << ", " << name << ": sema count = " << m_count << endl; sem.Post(); cout << get_curtime() << ", " << name << ": sema post" << endl; } else { cout << get_curtime() << ", " << name << ": sema timedwait failed and sleep 1 sec" << endl; sleep(1); } }
cout << get_curtime() << ", " << name << ": end" << endl;
}
进入OpenHarmony编译环境,运行如下命令:
hb build -f
将镜像烧录到开发板中。
运行结果如下:
# utils_noname_semaphorethread_0: Sema Wait...thread_0: Sema Wait Successfulthread_0: Sema Postthread_1: Sema Wait...thread_1: Sema Wait Successfulthread_1: Sema Postthread_0: Sema Wait...thread_0: Sema Wait Successfulthread_0: Sema Postthread_2: Sema Wait...thread_2: Sema Wait Successfulthread_2: Sema Postthread_0: Sema Wait...thread_0: Sema Wait Successfulthread_0: Sema Postthread_1: Sema Wait...thread_1: Sema Wait Successfulthread_1: Sema Postthread_3: Sema Wait...thread_3: Sema Wait Successfulthread_3: Sema Postthread_2: Sema Wait...thread_0: Sema Wait Successfulthread_2: Sema Wait...thread_2: Sema Postthread_1: Sema Wait...thread_0: Sema Wait Successfulthread_0: Sema Postthread_1: Sema Wait Successfulthread_1: Sema Postthread_4: Sema Wait...thread_4: Sema Wait Successfulthread_4: Sema Postthread_3: Sema Wait...thread_3: Sema Wait Successfulthread_3: Sema Postthread_2: Sema Wait...thread_2: Sema Wait Successfulthread_2: Sema Postthread_0: Sema Wait...thread_0: Sema Wait Successfulthread_0: Sema Postthread_1: Sema Wait...thread_1: Sema Wait Successfulthread_1: Sema Postthread_4: Sema Wait...thread_4: Sema Wait Successfulthread_4: Sema Postthread_3: Sema Wait...thread_3: Sema Wait Successfulthread_3: Sema Postthread_2: Sema Wait...thread_2: Sema Wait Successfulthread_2: Sema Postthread_1: Sema Wait...thread_1: Sema Wait Successfulthread_1: Sema Postthread_4: Sema Wait...thread_4: Sema Wait Successfulthread_4: Sema Postthread_3: Sema Wait...thread_3: Sema Wait Successfulthread_3: Sema Postthread_2: Sema Wait...thread_2: Sema Wait Successfulthread_2: Sema Postthread_4: Sema Wait...thread_4: Sema Wait Successfulthread_4: Sema Postthread_3: Sema Wait...thread_3: Sema Wait Successfulthread_3: Sema Postthread_4: Sema Wait...thread_4: Sema Wait Successfulthread_4: Sema Postthreads stop#
运行结果如下:
# utils_name_semaphore2017-8-5 1924, thread_a: start2017-8-5 19:43:24, thread_b: start2017-8-5 19:43:24, thread_c: start2017-8-5 19:43:24, thread_a: sema wait...2017-8-5 19:43:24, thread_a: sema wait success2017-8-5 19:43:24, thread_b: sema trywait...2017-8-5 19:43:24, thread_b: sema tryWait failed and sleep 1 sec2017-8-5 19:43:24, thread_c: sema timedwait 1 sec...2017-8-5 19:43:25, thread_a: sema count = 12017-8-5 19:43:25, thread_c: sema timedwait failed and sleep 1 sec2017-8-5 19:43:25, thread_a: sema post and sleep 1 sec2017-8-5 19:43:25, thread_b: sema trywait...2017-8-5 19:43:25, thread_b: sema trywait success2017-8-5 19:43:26, thread_a: sema wait...2017-8-5 19:43:26, thread_c: sema timedwait 1 sec...2017-8-5 19:43:26, thread_b: sema count = 112017-8-5 19:43:26, thread_b: sema post and sleep 1 sec2017-8-5 19:43:26, thread_a: sema wait success2017-8-5 19:43:27, thread_c: sema timedwait failed and sleep 1 sec2017-8-5 19:43:27, thread_b: sema trywait...2017-8-5 19:43:27, thread_b: sema tryWait failed and sleep 1 sec2017-8-5 19:43:27, thread_a: sema count = 122017-8-5 19:43:27, thread_a: sema post and sleep 1 sec2017-8-5 19:43:28, thread_c: sema timedwait 1 sec...2017-8-5 19:43:28, thread_c: sema timedwait success2017-8-5 19:43:28, thread_b: sema trywait...2017-8-5 19:43:28, thread_a: sema wait...2017-8-5 19:43:28, thread_b: sema tryWait failed and sleep 1 sec2017-8-5 19:43:28, thread_c: sema count = 1122017-8-5 19:43:28, thread_c: sema post2017-8-5 19:43:28, thread_c: sema timedwait 1 sec...2017-8-5 19:43:28, thread_a: sema wait success2017-8-5 19:43:29, thread_b: sema trywait...2017-8-5 19:43:29, thread_b: sema tryWait failed and sleep 1 sec2017-8-5 19:43:29, thread_c: sema timedwait failed and sleep 1 sec2017-8-5 19:43:29, thread_a: sema count = 1132017-8-5 19:43:29, thread_a: sema post and sleep 1 sec2017-8-5 19:43:30, thread_b: sema close2017-8-5 19:43:30, thread_c: sema timedwait 1 sec...2017-8-5 19:43:30, thread_c: sema timedwait success2017-8-5 19:43:30, thread_a: sema wait...2017-8-5 19:43:31, thread_c: sema count = 2132017-8-5 19:43:31, thread_c: sema post2017-8-5 19:43:31, thread_c: sema close2017-8-5 19:43:31, thread_a: sema wait success2017-8-5 19:43:32, thread_a: sema count = 2142017-8-5 19:43:32, thread_a: sema post and sleep 1 sec2017-8-5 19:43:33, thread_a: sema wait...2017-8-5 19:43:33, thread_a: sema wait success2017-8-5 19:43:34, thread_a: sema count = 2152017-8-5 19:43:34, thread_a: sema post and sleep 1 sec2017-8-5 19:43:35, thread_a: sema closethreads stop#
全部0条评论
快来发表一下你的评论吧 !