一个线程发送信号量,另外一个线程接收信号量
更多...
一个线程发送信号量,另外一个线程接收信号量
源码
#include <rtthread.h>
#define THREAD_PRIORITY 25
#define THREAD_TIMESLICE 5
ALIGN(RT_ALIGN_SIZE)
static char thread1_stack[1024];
static void rt_thread1_entry(void *parameter)
{
while (1)
{
if (count <= 100)
{
count++;
}
else
return;
if (0 == (count % 10))
{
rt_kprintf(
"thread1 release a dynamic semaphore.\n");
}
}
}
ALIGN(RT_ALIGN_SIZE)
static char thread2_stack[1024];
static void rt_thread2_entry(void *parameter)
{
while (1)
{
{
rt_kprintf(
"thread2 take a dynamic semaphore, failed.\n");
return;
}
else
{
number++;
rt_kprintf(
"thread2 take a dynamic semaphore. number = %d\n", number);
}
}
}
int semaphore_sample()
{
{
return -1;
}
else
{
rt_kprintf(
"create done. dynamic semaphore value = 0.\n");
}
"thread1",
rt_thread1_entry,
&thread1_stack[0],
sizeof(thread1_stack),
THREAD_PRIORITY, THREAD_TIMESLICE);
"thread2",
rt_thread2_entry,
&thread2_stack[0],
sizeof(thread2_stack),
THREAD_PRIORITY - 1, THREAD_TIMESLICE);
return 0;
}