| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- /*
- * Copyright (c) 2006-2022, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2018-08-24 yangjie the first version
- */
- /* 程序清单:关闭中断进行全局变量的访问 */
- #include <rthw.h>
- #include <rtthread.h>
- #define THREAD_PRIORITY 20
- #define THREAD_STACK_SIZE 512
- #define THREAD_TIMESLICE 5
- /* 同时访问的全局变量 */
- static rt_uint32_t cnt;
- static void thread_entry(void *parameter)
- {
- rt_uint32_t no;
- rt_uint32_t level;
- no = (rt_uint32_t) parameter;
- while (1)
- {
- /* 关闭中断 */
- level = rt_hw_interrupt_disable();
- cnt += no;
- /* 恢复中断 */
- rt_hw_interrupt_enable(level);
- rt_kprintf("protect thread[%d]'s counter is %d\n", no, cnt);
- rt_thread_mdelay(no * 10);
- }
- }
- int interrupt_sample(void)
- {
- rt_thread_t thread;
- /* 创建thread1线程 */
- thread = rt_thread_create("thread1", thread_entry, (void *)10,
- THREAD_STACK_SIZE,
- THREAD_PRIORITY, THREAD_TIMESLICE);
- #ifdef RT_USING_SMP
- /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */
- rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void*)0);
- #endif
- if (thread != RT_NULL)
- rt_thread_startup(thread);
- /* 创建thread2线程 */
- thread = rt_thread_create("thread2", thread_entry, (void *)20,
- THREAD_STACK_SIZE,
- THREAD_PRIORITY, THREAD_TIMESLICE);
- #ifdef RT_USING_SMP
- /* 绑定线程到同一个核上,避免启用多核时的输出混乱 */
- rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void*)0);
- #endif
- if (thread != RT_NULL)
- rt_thread_startup(thread);
- return 0;
- }
- /* 导出到 msh 命令列表中 */
- MSH_CMD_EXPORT(interrupt_sample, interrupt sample);
|