CC26xx &&CC13xx 多线程

来源:互联网 发布:怎么使用淘宝钻石展位 编辑:程序博客网 时间:2024/06/10 00:50

/* XDC module Headers */#include <xdc/std.h>#include <xdc/runtime/System.h>/* BIOS module Headers */#include <ti/sysbios/BIOS.h>#include <ti/sysbios/knl/Clock.h>#include <ti/sysbios/knl/Task.h>#include <ti/sysbios/knl/Semaphore.h>/* Example/Board Header files */#include "Board.h"#define TASKSTACKSIZE   512Void task1Fxn(UArg arg0, UArg arg1);Void task2Fxn(UArg arg0, UArg arg1);Int resource = 0;Int finishCount = 0;UInt32 sleepTickCount;Task_Struct task1Struct, task2Struct;Char task1Stack[TASKSTACKSIZE], task2Stack[TASKSTACKSIZE];Semaphore_Struct semStruct;Semaphore_Handle semHandle;/* *  ======== main ======== */Int main(){    /* Construct BIOS objects */    Task_Params taskParams;    Semaphore_Params semParams;    /* Call board init functions */    Board_initGeneral();    /* Construct writer/reader Task threads */    Task_Params_init(&taskParams);    taskParams.stackSize = TASKSTACKSIZE;    taskParams.stack = &task1Stack;    taskParams.priority = 1;    Task_construct(&task1Struct, (Task_FuncPtr)task1Fxn, &taskParams, NULL);    taskParams.stack = &task2Stack;    taskParams.priority = 2;    Task_construct(&task2Struct, (Task_FuncPtr)task2Fxn, &taskParams, NULL);    /* Construct a Semaphore object to be use as a resource lock, inital count 1 */    Semaphore_Params_init(&semParams);    Semaphore_construct(&semStruct, 1, &semParams);    /* Obtain instance handle */    semHandle = Semaphore_handle(&semStruct);    /* We want to sleep for 10000 microseconds */    sleepTickCount = 10000 / Clock_tickPeriod;    BIOS_start();    /* Does not return */    return(0);}/* *  ======== task1Fxn ======== */Void task1Fxn(UArg arg0, UArg arg1){    UInt32 time;    for (;;) {        System_printf("Running task1 function\n");        if (Semaphore_getCount(semHandle) == 0) {            System_printf("Sem blocked in task1\n");        }        /* Get access to resource */        Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);        /* Do work by waiting for 2 system ticks to pass */        time = Clock_getTicks();        while (Clock_getTicks() <= (time + 1)) {            ;        }        /* Do work on locked resource */        resource += 1;        /* Unlock resource */        Semaphore_post(semHandle);        Task_sleep(sleepTickCount);    }}/* *  ======== task2Fxn ======== */Void task2Fxn(UArg arg0, UArg arg1){    for (;;) {        System_printf("Running task2 function\n");        if (Semaphore_getCount(semHandle) == 0) {            System_printf("Sem blocked in task2\n");        }        /* Get access to resource */        Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);        /* Do work on locked resource */        resource += 1;        /* Unlock resource */        Semaphore_post(semHandle);        Task_sleep(sleepTickCount);        finishCount++;        if (finishCount == 5) {            System_printf("Calling BIOS_exit from task2\n");            BIOS_exit(0);        }    }}

特别注意的一点是很多人用多线程之后会出现BLE不能工作等问题,一般是优先级不够的导致的,注意优先级的设置以及用信号量去控制线程的控制权的获取。


 
原创粉丝点击