[RTT routine exercise] 2.2 basic use of semaphores (Dynamic/static semaphores)

Source: Internet
Author: User

Semaphores:

From Baidu Encyclopedia:

Semaphore (semaphore) is a facility used in a multi-threaded environment. It can be used to ensure that two or more key code segments are not concurrently called. Before entering a key code segment, the thread must obtain a semaphore.CodeThe thread must release the semaphore.

The semaphores of RT-thread are static and dynamic. Static and Dynamic signals of the same thread mean the same. Two operations are available for semaphores: Take and release.

ProgramIn, first initialize the semaphore to 0, then first use take, and only wait for 10 tick, so it will certainly time out, because the initial semaphore value is 0, not take. Then, when release is performed once, the semaphores increase by 1. Then, take again and use the wait forever method to obtain the semaphores.

Program:

# Include <rtthread. h> static struct static_sem; static rt_sem_t dynamic_sem = rt_null; static unknown thread1_stack [1024]; struct rt_thread thread1; static void Merge (void * parameter) {rt_err_t result; invalid tick; /* Static semaphore demo */tick = rt_tick_get ();/* try to take the SEM, wait 10 ticks */result = rt_sem_take (& static_sem, 10 ); if (result =-rt_etimeout) {I F (rt_tick_get ()-tick! = 10) {rt_sem_detach (& static_sem); return;} rt_kprintf ("Take semaphore timeout \ n");} else {rt_kprintf ("Take a static semaphore, failed. \ n "); rt_sem_detach (& static_sem); return;}/* release the semaphore */rt_sem_release (& static_sem ); /* wait the semaphore forever */result = rt_sem_take (& static_sem, rt_waiting_forever); If (result! = Rt_eok) {rt_kprintf ("Take a static semaphore, failed. \ n "); rt_sem_detach (& static_sem); return;} rt_kprintf (" Take a static semaphore, done. \ n ");/* detach the semaphore object */rt_sem_detach (& static_sem); //}/* dynamic thread pointer * // static void thread2_entry (void * parameter) // {// rt_err_t result; // rt_tick_t tick; tick = rt_tick_get ();/* try to take the semaphore, wait for 10 ticks */Re Sult = rt_sem_take (dynamic_sem, 10); If (result =-rt_etimeout) {If (rt_tick_get ()-tick! = 10) {rt_sem_delete (dynamic_sem); return;} rt_kprintf ("Take semaphore timeout \ n");} else {rt_kprintf ("Take a dynamic semaphore, failed. \ n "); rt_sem_delete (dynamic_sem); return;}/* release the dynamic semaphore */rt_sem_release (dynamic_sem);/* wait forever */result = rt_sem_take (dynamic_sem, rt_waiting_forever); If (result! = Rt_eok) {rt_kprintf ("Take a dynamic semaphore, failed. \ n "); rt_sem_delete (dynamic_sem); return;} rt_kprintf (" Take a dynamic semaphore, done. \ n ");/* Delete the semaphore */rt_sem_delete (dynamic_sem);} // static rt_thread_t tid = rt_null; int rt_application_init () {rt_err_t result; result = rt_sem_init (& static_sem, "ssem", 0, rt_ipc_flag_fifo); If (result! = Rt_eok) {rt_kprintf ("init static semaphore failed. \ n "); Return-1;} dynamic_sem = rt_sem_create (" dsem ", 0, rt_ipc_flag_fifo); If (dynamic_sem = rt_null) {rt_kprintf (" Create dynamic semaphore failed. \ n "); Return-1;}/* thread1 init */rt_thread_init (& thread1," T1 ", rt_thread_entry1, rt_null, & thread1_stack [0], sizeof (thread1_stack ), 11, 5); rt_thread_startup (& thread1); Return 0 ;}

Result:

 
Take semaphore timeouttake A Staic semaphore, done. Take semaphore timeouttake a dynamic semaphore, done.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.