标准库

OpenHarmony内核使用musl libc库,支持标准POSIX接口,开发者可基于POSIX标准接口开发内核之上的组件及应用。

框架流程

图 1 POSIX接口框架
标准库 - 图1

musl libc库支持POSIX标准,涉及的系统调用相关接口由OpenHarmony内核适配支持 ,以满足接口对外描述的功能要求。

开发指导

标准库支持接口的详细情况请参考C库的API文档,其中也涵盖了与POSIX标准之间的差异说明。开发者可根据已经提供的接口,开发组件及应用等。

操作实例

在本示例中,主线程创建了THREAD_NUM个子线程,每个子线程启动后等待被主线程唤醒,主线程成功唤醒所有子线程后,子线程继续执行直至生命周期结束,同时主线程通过pthread_join方法等待所有线程执行结束。


1. #include <stdio.h>
2. #include <unistd.h>
3. #include <pthread.h>

5. #ifdef __cplusplus
6. #if __cplusplus
7. extern "C" {
8. #endif /* __cplusplus */
9. #endif /* __cplusplus */

11. #define THREAD_NUM 3
12. int g_startNum = 0; /* 启动的线程数 */
13. int g_wakenNum = 0; /* 唤醒的线程数 */

15. struct testdata {
16. pthread_mutex_t mutex;
17. pthread_cond_t cond;
18. } g_td;

20. /* *
21. * 子线程入口函数
22. */
23. static void *ChildThreadFunc(void *arg)
24. {
25. int rc;
26. pthread_t self = pthread_self();

28. /* 获取mutex锁 */
29. rc = pthread_mutex_lock(&g_td.mutex);
30. if (rc != 0) {
31. printf("ERROR:take mutex lock failed, error code is %d!\n", rc);
32. goto EXIT;
33. }

35. /* g_startNum计数加一,用于统计已经获得mutex锁的子线程个数 */
36. g_startNum++;

38. /* 等待cond条件变量 */
39. rc = pthread_cond_wait(&g_td.cond, &g_td.mutex);
40. if (rc != 0) {
41. printf("ERROR: pthread condition wait failed, error code is %d!\n", rc);
42. (void)pthread_mutex_unlock(&g_td.mutex);
43. goto EXIT;
44. }

46. /* 尝试获取mutex锁 */
47. rc = pthread_mutex_trylock(&g_td.mutex);
48. if (rc == 0) {
49. /* 失败则退出 */
50. printf("ERROR: mutex trylock failed, error code is %d!\n", rc);
51. goto EXIT;
52. }

54. /* g_wakenNum计数加一,用于统计已经被cond条件变量唤醒的子线程个数 */
55. g_wakenNum++;

57. /* 释放mutex锁 */
58. rc = pthread_mutex_unlock(&g_td.mutex);
59. if (rc != 0) {
60. printf("ERROR: mutex release failed, error code is %d!\n", rc);
61. goto EXIT;
62. }
63. EXIT:
64. return NULL;
65. }

67. static int testcase(void)
68. {
69. int i, rc;
70. pthread_t thread[THREAD_NUM];

72. /* 初始化mutex锁 */
73. rc = pthread_mutex_init(&g_td.mutex, NULL);
74. if (rc != 0) {
75. printf("ERROR: mutex init failed, error code is %d!\n", rc);
76. goto ERROROUT;
77. }

79. /* 初始化cond条件变量 */
80. rc = pthread_cond_init(&g_td.cond, NULL);
81. if (rc != 0) {
82. printf("ERROR: pthread condition init failed, error code is %d!\n", rc);
83. goto ERROROUT;
84. }

86. /* 批量创建THREAD_NUM个子线程 */
87. for (i = 0; i < THREAD_NUM; i++) {
88. rc = pthread_create(&thread[i], NULL, ChildThreadFunc, NULL);
89. if (rc != 0) {
90. printf("ERROR: pthread create failed, error code is %d!\n", rc);
91. goto ERROROUT;
92. }
93. }

95. /* 等待所有子线程都完成mutex锁的获取 */
96. while (g_startNum < THREAD_NUM) {
97. usleep(100);
98. }

100. /* 获取mutex锁,确保所有子线程都阻塞在pthread_cond_wait上 */
101. rc = pthread_mutex_lock(&g_td.mutex);
102. if (rc != 0) {
103. printf("ERROR: mutex lock failed, error code is %d\n", rc);
104. goto ERROROUT;
105. }

107. /* 释放mutex锁 */
108. rc = pthread_mutex_unlock(&g_td.mutex);
109. if (rc != 0) {
110. printf("ERROR: mutex unlock failed, error code is %d!\n", rc);
111. goto ERROROUT;
112. }

114. for (int j = 0; j < THREAD_NUM; j++) {
115. /* 在cond条件变量上广播信号 */
116. rc = pthread_cond_signal(&g_td.cond);
117. if (rc != 0) {
118. printf("ERROR: pthread condition failed, error code is %d!\n", rc);
119. goto ERROROUT;
120. }
121. }

123. sleep(1);

125. /* 检查是否所有子线程都已被唤醒 */
126. if (g_wakenNum != THREAD_NUM) {
127. printf("ERROR: not all threads awaken, only %d thread(s) awaken!\n", g_wakenNum);
128. goto ERROROUT;
129. }

131. /* join所有子线程,即等待其结束 */
132. for (i = 0; i < THREAD_NUM; i++) {
133. rc = pthread_join(thread[i], NULL);
134. if (rc != 0) {
135. printf("ERROR: pthread join failed, error code is %d!\n", rc);
136. goto ERROROUT;
137. }
138. }

140. /* 销毁cond条件变量 */
141. rc = pthread_cond_destroy(&g_td.cond);
142. if (rc != 0) {
143. printf("ERROR: pthread condition destroy failed, error code is %d!\n", rc);
144. goto ERROROUT;
145. }
146. return 0;
147. ERROROUT:
148. return -1;
149. }

151. /*
152. * 示例代码主函数
153. */
154. int main(int argc, char *argv[])
155. {
156. int rc;

158. /* 启动测试函数 */
159. rc = testcase();
160. if (rc != 0) {
161. printf("ERROR: testcase failed!\n");
162. }

164. return 0;
165. }
166. #ifdef __cplusplus
167. #if __cplusplus
168. }
169. #endif /* __cplusplus */
170. #endif /* __cplusplus */

常见问题

无。