페이지 선택
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Search in pages

Thread

 

The mxos thread control API can be used in the system to define, create, control and destory threads.

The thread priority is divided into 10 levels from 0 ~9. The lower the number, the higher the priority.

High-priority threads can preempt low-priority threads.

If high_priority threads cannot hang/suspend, low-priority threads will not get time to run.

Each thread of the same priority runcs in a time-sharing manner, making these threads appear to run simultaneously.

 

 

 

 

1. Thread API List

 

Thread API Description
mxos_rtos_create_thread Create and start a new thread
mxos_rtos_delete_thread Delete the thread, put it into the termination state, and clear the resource in the IDLE thread
mxos_rtos_suspend_thread Hang a thread
mxos_rtos_suspend_all_thread Suspend all other threads, always execute the current thread, and interrupt OS scheduling
mxos_rtos_resume_all_thread Restore OS scheduling
mxos_rtos_thread_join Cause the current thread to hang and wait for another thread to terminate
mxos_rtos_thread_force_awake Force wake up a suspended thread
mxos_rtos_is_current_thread Query if a thread is currently running
mxos_thread_sleep Suspend a thread for a period of time, the unit of time is: seconds
mxos_thread_msleep Suspend a thread for a period of time, the unit of time is: milliseconds
When the mxos system is initialized, a function is created: int application_start(void)

 

The thread of the main executable, the thread’s priority is 7(mxos_APPLICATION_PRIORITY).

A thread can be in the following states:

 

State Description
Running The thread is running. At the same time, only one thread in the mxos RTOS may be running.
Ready

The thread is ready and waiting to run. Once the current running thread is terminated/suspended,

the thread with the highest priority among all ready threads will become operational.

Suspend

The thread is waiting for an event (a period of time, semaphore, mutex, message queue)

to be converted to a ready state.

Terminate

The thread is inactive. In the IDLE thread of the mxos system,

the private resources owned by all inactive threads will be automatically destroyed.

Shared resources (such as memory blocks created by malloc) need to be destroyed manually.

2. os_thread.c code

 

#include "mxos.h" 
#define os_thread_log(M, ...) custom_log("OS", M, ##__VA_ARGS__)

void thread_1(mxos_thread_arg_t arg)
{
    UNUSED_PARAMETER( arg );
    while ( 1 )
    {
        os_thread_log( "This is thread 1" );
        mxos_thread_sleep( 2 );
    }
}

void thread_2( mxos_thread_arg_t arg )
{
    UNUSED_PARAMETER( arg );
    os_thread_log( "This is thread 2" );
    mxos_thread_sleep( 4 );
    /* Make with terminate state and IDLE thread will clean resources */
    mxos_rtos_delete_thread( NULL );
}

int application_start( void )
{
    OSStatus err = kNoErr;
    mxos_thread_t t_handler = NULL;

    /* Create a new thread */
    err = mxos_rtos_create_thread( NULL, mxos_APPLICATION_PRIORITY, "Thread 1", thread_1, 0x500, 0 );
    require_noerr_string( err, exit, "ERROR: Unable to start the thread 1." );
    while ( 1 )
    {
        /* Create a new thread, and this thread will delete its self and clean its resource */
        err = mxos_rtos_create_thread( &t_handler, mxos_APPLICATION_PRIORITY, "Thread 2", thread_2, 0x500, 0 );
        require_noerr_string( err, exit, "ERROR: Unable to start the thread 2." );
        mxos_rtos_thread_join( &t_handler );
    }

    exit:
    if ( err != kNoErr )
        os_thread_log( "Thread exit with err: %d", err );

    mxos_rtos_delete_thread( NULL );
    return err;
}

 

 

 

♦ Achieve

  • First create two threads in the mxos body thread;
  • Thread 1 sleeps for 2 seconds after printing the log each time;
  • Thread 2 sleeps for 4 seconds each time the log is printed, and destroys the thread;
  • The overall result of two threads printing alternately, thread 1 prints 2 times and thread 2 prints 1 time.

 

 

♦ Main function code annotations:

1) Call the mxos API to create a new thread:

  err = mxos_rtos_create_thread( NULL, mxos_APPLICATION_PRIORITY, "Thread 1", thread_1, 0x500, 0 );

2) The current thread hangs and waits for other threads to terminate:

  mxos_rtos_thread_join( &t_handler );

3) Delete the current thread and clear the resource: mxos_rtos_delete_thread( NULL );

4) The current thread sleeps for 2 seconds:mxos_thread_sleep( 2 );

 

 

 

 

 

이상 끝 ~~~

Viewed Page List