该程序是基于OpenHarmony标准系统的C++公共基础类库的线程处理:Sempahore。
本案例完成如下工作:
(1)无名信号量使用方法
(2)有名信号量使用方法
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 <semaphore_ex.h>
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个可执行程序,分别是:
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-build
sudo apt install generate-ninja
# 规范化BUILD.gn
gn format BUILD.gn
#include <semaphore_ex.h>
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 <semaphore_ex.h>
首先定义有名信号量,然后通过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_semaphore
thread_0: Sema Wait...
thread_0: Sema Wait Successful
thread_0: Sema Post
thread_1: Sema Wait...
thread_1: Sema Wait Successful
thread_1: Sema Post
thread_0: Sema Wait...
thread_0: Sema Wait Successful
thread_0: Sema Post
thread_2: Sema Wait...
thread_2: Sema Wait Successful
thread_2: Sema Post
thread_0: Sema Wait...
thread_0: Sema Wait Successful
thread_0: Sema Post
thread_1: Sema Wait...
thread_1: Sema Wait Successful
thread_1: Sema Post
thread_3: Sema Wait...
thread_3: Sema Wait Successful
thread_3: Sema Post
thread_2: Sema Wait...
thread_0: Sema Wait Successful
thread_2: Sema Wait...
thread_2: Sema Post
thread_1: Sema Wait...
thread_0: Sema Wait Successful
thread_0: Sema Post
thread_1: Sema Wait Successful
thread_1: Sema Post
thread_4: Sema Wait...
thread_4: Sema Wait Successful
thread_4: Sema Post
thread_3: Sema Wait...
thread_3: Sema Wait Successful
thread_3: Sema Post
thread_2: Sema Wait...
thread_2: Sema Wait Successful
thread_2: Sema Post
thread_0: Sema Wait...
thread_0: Sema Wait Successful
thread_0: Sema Post
thread_1: Sema Wait...
thread_1: Sema Wait Successful
thread_1: Sema Post
thread_4: Sema Wait...
thread_4: Sema Wait Successful
thread_4: Sema Post
thread_3: Sema Wait...
thread_3: Sema Wait Successful
thread_3: Sema Post
thread_2: Sema Wait...
thread_2: Sema Wait Successful
thread_2: Sema Post
thread_1: Sema Wait...
thread_1: Sema Wait Successful
thread_1: Sema Post
thread_4: Sema Wait...
thread_4: Sema Wait Successful
thread_4: Sema Post
thread_3: Sema Wait...
thread_3: Sema Wait Successful
thread_3: Sema Post
thread_2: Sema Wait...
thread_2: Sema Wait Successful
thread_2: Sema Post
thread_4: Sema Wait...
thread_4: Sema Wait Successful
thread_4: Sema Post
thread_3: Sema Wait...
thread_3: Sema Wait Successful
thread_3: Sema Post
thread_4: Sema Wait...
thread_4: Sema Wait Successful
thread_4: Sema Post
threads stop
#
运行结果如下:
# utils_name_semaphore
2017-8-5 19:43:24, thread_a: start
2017-8-5 19:43:24, thread_b: start
2017-8-5 19:43:24, thread_c: start
2017-8-5 19:43:24, thread_a: sema wait...
2017-8-5 19:43:24, thread_a: sema wait success
2017-8-5 19:43:24, thread_b: sema trywait...
2017-8-5 19:43:24, thread_b: sema tryWait failed and sleep 1 sec
2017-8-5 19:43:24, thread_c: sema timedwait 1 sec...
2017-8-5 19:43:25, thread_a: sema count = 1
2017-8-5 19:43:25, thread_c: sema timedwait failed and sleep 1 sec
2017-8-5 19:43:25, thread_a: sema post and sleep 1 sec
2017-8-5 19:43:25, thread_b: sema trywait...
2017-8-5 19:43:25, thread_b: sema trywait success
2017-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 = 11
2017-8-5 19:43:26, thread_b: sema post and sleep 1 sec
2017-8-5 19:43:26, thread_a: sema wait success
2017-8-5 19:43:27, thread_c: sema timedwait failed and sleep 1 sec
2017-8-5 19:43:27, thread_b: sema trywait...
2017-8-5 19:43:27, thread_b: sema tryWait failed and sleep 1 sec
2017-8-5 19:43:27, thread_a: sema count = 12
2017-8-5 19:43:27, thread_a: sema post and sleep 1 sec
2017-8-5 19:43:28, thread_c: sema timedwait 1 sec...
2017-8-5 19:43:28, thread_c: sema timedwait success
2017-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 sec
2017-8-5 19:43:28, thread_c: sema count = 112
2017-8-5 19:43:28, thread_c: sema post
2017-8-5 19:43:28, thread_c: sema timedwait 1 sec...
2017-8-5 19:43:28, thread_a: sema wait success
2017-8-5 19:43:29, thread_b: sema trywait...
2017-8-5 19:43:29, thread_b: sema tryWait failed and sleep 1 sec
2017-8-5 19:43:29, thread_c: sema timedwait failed and sleep 1 sec
2017-8-5 19:43:29, thread_a: sema count = 113
2017-8-5 19:43:29, thread_a: sema post and sleep 1 sec
2017-8-5 19:43:30, thread_b: sema close
2017-8-5 19:43:30, thread_c: sema timedwait 1 sec...
2017-8-5 19:43:30, thread_c: sema timedwait success
2017-8-5 19:43:30, thread_a: sema wait...
2017-8-5 19:43:31, thread_c: sema count = 213
2017-8-5 19:43:31, thread_c: sema post
2017-8-5 19:43:31, thread_c: sema close
2017-8-5 19:43:31, thread_a: sema wait success
2017-8-5 19:43:32, thread_a: sema count = 214
2017-8-5 19:43:32, thread_a: sema post and sleep 1 sec
2017-8-5 19:43:33, thread_a: sema wait...
2017-8-5 19:43:33, thread_a: sema wait success
2017-8-5 19:43:34, thread_a: sema count = 215
2017-8-5 19:43:34, thread_a: sema post and sleep 1 sec
2017-8-5 19:43:35, thread_a: sema close
threads stop
#