4.2 信号量的使用
<p>下面给出了一个如何使用二值信号量的例子</p>
<pre><code>#include <string.h>
#include "wm_include.h"
#include "lwip/netif.h"
#include "wm_netif.h"
tls_os_task_t tskHdl1 = NULL;
tls_os_task_t tskHdl2 = NULL;
static tls_os_sem_t *sem;
static void user_task1(void *data)
{
tls_os_status_t res;
while (1) {
res = tls_os_sem_acquire(sem, 0);
if (res == TLS_OS_SUCCESS) {
printf("task1 got sem\n");
tls_os_sem_release(sem);
tls_os_time_delay(1000);
}
}
}
static void user_task2(void *data)
{
tls_os_status_t res;
while (1) {
res = tls_os_sem_acquire(sem, 0);
if (res == TLS_OS_SUCCESS) {
printf("task2 got sem\n");
tls_os_sem_release(sem);
tls_os_time_delay(1000);
}
}
}
static void timerHandler(UINT64 param)
{
printf("time up\n");
}
void UserMain(void)
{
printf("\n User task \n");
#if 0//DEMO_CONSOLE
CreateDemoTask();
#endif
tls_os_status_t res = tls_os_sem_create(&sem, 1);
tls_os_task_create(&tskHdl1, "user_tsk1",
user_task1,
(void *)0,
(void *)NULL,
1024, // 任务栈的大小
7,
0);
tls_os_task_create(&tskHdl2, "user_tsk2",
user_task2,
(void *)0,
(void *)NULL,
1024,
8,
0);
}</code></pre>