MEI例程之 notify.c
来源:互联网 发布:数据库怎么定义主键 编辑:程序博客网 时间:2024/05/21 17:18
1、thread对象
An MPIThread object provides a platform-independent method to create a thread, set its priority, and obtain its current status.一个MPIThread对象提供一个平台独立的方法创建一个线程,设置它的优先级,并且获得当前的状态。
(1)方法:
A、创建、删除、验证方法
创建一个线程
mpiThreadCreate(MPIThread *thread,//指向一个MPIThread句柄
MPIThreadOptions *options);//thread线程的设置创建一个thread对象,这个函数并不开启一个新的线程。在创建一个thread线程之后,使用mpiThreadStart开启一个线程。
删除一个线程
MPI_DECL1 MPI_RESULT MPI_DECL2mpiThreadDelete(MPIThread thread);//指向一个MPIThread句柄
删除一个线程对象,并使线程句柄无效。这个函数并不终止一个线程。
验证一个线程对象和它的句柄:
MPI_DECL1 MPI_RESULT MPI_DECL2
mpiThreadValidate(MPIThread thread);//指向一个MPIThread句柄B、配置方法和信息方法
开始一个新的线程:
MPI_DECL1 MPI_RESULT MPI_DECL2mpiThreadStart(MPIThread thread,//指向一个MPIThread句柄
MPIThreadFunction function, //线程调用函数
void *argument);//传递给调用函数的参数
线程状态:
将线程的当前状态写到status中。
MPI_DECL1 MPI_RESULT MPI_DECL2mpiThreadStatus(MPIThread thread,//指向一个MPIThread句柄
MPIThreadStatus *status);//MPIThreadStatus指针
mpiThreadFunction :
将线程函数指针传送到mpiThreadStartMPI_DECL1 MPI_RESULT MPI_DECL2
mpiThreadFunction(MPIThread thread,
MPIThreadFunction *function);
mpiThreadArgument:
设置argument参数MPI_DECL1 MPI_RESULT MPI_DECL2
mpiThreadArgument(MPIThread thread,
void **argument); /* see mpiThreadStart() */
mpiThreadPriorityGet 获得当前线程的优先级
MPI_DECL1 MPI_RESULT MPI_DECL2mpiThreadPriorityGet(MPIThread thread,
MPIThreadPriority *priority);
mpiThreadPrioritySet 设置线程的优先级
MPI_DECL1 MPI_RESULT MPI_DECL2mpiThreadPrioritySet(MPIThread thread,
MPIThreadPriority priority);
(2)数据类型:
MPIThreadFunction
typedef MPI_RESULT (MPI_DECL2 *MPIThreadFunction)(void *threadData);这时一个函数指针的类型别名,MPIThreadFunction是在一个新的线程中发起的函数指针。
使用此类型别名相同的签名,可以用作线程函数。
在线程函数中发生的异常不能传送到MPI库,这导致主线程退出,程序崩溃。
threadArgs 对象句柄(与controller关联,生成事件)。
MPIThreadOptions
typedef struct MPIThreadOptions {int32_t stackSize;
} MPIThreadOptions;
设置新线程的选项。stackSize 新线程的栈大小。
MPIThreadPriority
设置新线程与调用线程的优先级差别。MPIThreadPriorityDEFAULT 将新线程的优先级设置为调用线程的优先级。MPIThreadPriorityHIGHEST将线程的优先级设置为最高。MPIThreadStatus
MPIThread对象的状态信息
typedef struct MPIThreadStatus {void *id; /* platform-specific thread id ,事件的类型*/
MPI_BOOL active; /* FALSE => thread not active ,线程的状态,如果是true,说明线程函数还没有完成,如果是false,说明线程函数已经完成。*/
MPI_RESULT returnValue; /* thread-specific return value */
} MPIThreadStatus;
(3)示例:
MPI_RESULT MPI_DECL2 helloThreadMain(void* threadData)//主线程{ /* print event type to screen. 将事件类型打印到屏幕*/ printf("Hello from another thread.\n" " threadData = 0x%08x", threadData); return MPIMessageOK;}/* Wait for a thread to end 等待线程结束*/MPI_RESULT threadJoin(MPIThread thread){ MPIThreadStatus status; MPI_RESULT returnValue = MPIMessageOK; while (returnValue == MPIMessageOK) { returnValue = mpiThreadStatus(thread, &status); if ((returnValue == MPIMessageOK) && (status.active == FALSE))//当为false时,说明次线程调用函数已经结束 { break; } mpiPlatformSleep(10); } return returnValue;}void sayHello(){ MPIThread thread; MPI_RESULT returnValue; returnValue = mpiThreadCreate(&thread, NULL);//创建线程 msgCHECK(returnValue); returnValue = mpiThreadStart(thread, helloThreadMain, NULL);//开始线程,helloThreadMain是线程调用函数 msgCHECK(returnValue); returnValue = threadJoin(thread);//等待线程结束,这个函数会一直循环,直到线程调用函数结束 msgCHECK(returnValue); returnValue = mpiThreadDelete(thread);//删除线程 msgCHECK(returnValue);}
主程序
/* notify1.c *//*:Create notify object and wait for events.Create a thread that uses a notify object to wait for user-defined eventspassed to the notify object from the main thread.创建一个线程,使用notify对象等待在主线程中传送过来的事件,这个事件是用户自定义的*/#include <stdlib.h>#include <stdio.h>#include <string.h>#include "stdmpi.h"#include "apputil.h"#if defined(ARG_MAIN_RENAME)#define main notify1MainargMainRENAME(main, notify1)#endif/* Command line arguments and defaults */Arg argList[] = {{ NULL, ArgTypeINVALID, NULL, }};Static int32_t NotifyWaitAwake;//线程调用函数完成标志Static int32_tnotifyWait(MPINotify notify);//线程调用函数intmain(int argc, char *argv[]){MPIControl control; /* Motion controller handle */MPINotify notify; /* Event notification object */MPIEventMask eventMask;MPI_RESULT returnValue;int32_t eventDone;MPIControlType controlType;MPIControlAddress controlAddress;int32_t argIndex;MPIThread thread;//定义线程MPIThreadStatus status;//线程状态argIndex =argControl(argc,argv,&controlType,&controlAddress);/* Parse command line for application-specific arguments */while (argIndex < argc) {int32_t argIndexNew;argIndexNew = argSet(argList, argIndex, argc, argv);if (argIndexNew <= argIndex) {argIndex = argIndexNew;break;}else {argIndex = argIndexNew;}}/* Check for unknown/invalid command line arguments */if (argIndex < argc) {mpiPlatformConsole("usage: %s %s\n",argv[0],ArgUSAGE);exit(MPIMessageARG_INVALID);}/* Create motion controller object */returnValue =mpiControlCreate(&control,controlType,&controlAddress);msgCHECK(returnValue);/* Request notification of ALL events */mpiEventMaskCLEAR(eventMask);mpiEventMaskALL(eventMask);/* Create event notification object */returnValue =mpiNotifyCreate(¬ify,eventMask,NULL);msgCHECK(returnValue);returnValue= mpiThreadCreate(control, &thread, NULL);//创建线程对象if (returnValue == MPIMessageOK) {returnValue =mpiThreadStart(thread,(MPIThreadFunction)notifyWait,//线程调用函数notify);//开始这个线程}eventDone = FALSE;//事件完成标志,false表示未完成while ((returnValue == MPIMessageOK) &&(eventDone == FALSE)) {char buffer[128];int32_t index;int32_t haveEventNumber;/* Wait for notifyWait() to call mpiNotifyEventWait() */while (NotifyWaitAwake != FALSE) {//线程调用函数唤醒标志mpiPlatformSleep(1);}/* Make sure notifyWait() thread is still executing */returnValue =mpiThreadStatus(thread,&status);if (returnValue != MPIMessageOK) {break;}if (status.active == FALSE) {//线程调用函数完成,退出循环break;}mpiPlatformConsole("Enter an event number, or ESC to exit ...\n");memset(buffer, 0, sizeof(buffer));//分配内存index = 0;haveEventNumber = FALSE;//有无事件标志while (haveEventNumber == FALSE) {MPIEventData eventStatus;int32_t key;int32_t eventNumber = 0;key = mpiPlatformKey(MPIWaitFOREVER);if (key <= 0) {mpiPlatformKey(MPIWaitFOREVER);continue;}switch (key) {case 0x1b: { /* ESC */eventNumber = key;haveEventNumber = TRUE;eventDone = TRUE;break; }case '\r':case '\n': {mpiPlatformConsole("\n");eventNumber = mpiPlatformAtol(buffer);haveEventNumber = TRUE;break; }case '\b': {if (index > 0) {mpiPlatformConsole("\b \b");buffer[--index] = '\0';}break; }case '-': {if (index == 0) {buffer[index++] = (char)key;mpiPlatformConsole("%c", key);}break; }case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9': {buffer[index++] = (char)key;mpiPlatformConsole("%c", key);break; }default: {break; }}if (haveEventNumber != FALSE) {NotifyWaitAwake = TRUE;eventStatus.eventType = MPIEventTypeSEQUENCE;eventStatus.objectIndex = eventNumber;returnValue =mpiNotifyEventWake(notify,&eventStatus);}}}if (thread != NULL) {while (returnValue == MPIMessageOK) {returnValue =mpiThreadStatus(thread,&status);msgCHECK(returnValue);if (status.active == FALSE) {returnValue = status.returnValue;break;}mpiPlatformSleep(1);}returnValue = mpiThreadDelete(thread);msgCHECK(returnValue);}fprintf(stderr,"%s exiting: returnValue 0x%x: %s\n",argv[0],returnValue,mpiMessage(returnValue, NULL));returnValue = mpiNotifyDelete(notify);msgCHECK(returnValue);returnValue = mpiControlDelete(control);msgCHECK(returnValue);return ((int)returnValue);}Static int32_tnotifyWait(MPINotify notify)//线程调用函数{MPI_RESULT returnValue;MPIWait timeout;int32_t eventDone;/* Validate the notify object and its handle */returnValue = mpiNotifyValidate(notify);timeout = MPIWaitFOREVER;eventDone = FALSE;while ((returnValue == MPIMessageOK) &&(eventDone == FALSE)) {MPIEventData eventStatus;NotifyWaitAwake = FALSE;/* Wait for notify event */returnValue =mpiNotifyEventWait(notify,&eventStatus,timeout);mpiPlatformConsole("mpiNotifyEventWait(0x%x, 0x%x, %d) returns 0x%x: %s\n",notify,&eventStatus,timeout,returnValue,mpiMessage(returnValue, NULL));switch (returnValue) {case MPIMessageOK: {mpiPlatformConsole("\teventStatus: type %d number %d\n",eventStatus.eventType,eventStatus.objectIndex);if (eventStatus.objectIndex == 0x1b) {eventDone = TRUE;}break; }case MPIMessageTIMEOUT: {mpiPlatformConsole("\tTIMEOUT\n");break;}default: {break; }}}mpiPlatformConsole("notifyWait(0x%x) returning 0x%x: %s\n",notify,returnValue,mpiMessage(returnValue, NULL));return (returnValue);}
阅读全文
0 0
- MEI例程之 notify.c
- mei fen
- mei wen
- 例程学习之SampleApp--SPIMgr.c(MT层) SPIMgr_ProcessZToolData()
- Java多线程之notify()
- 【BLE】CC2541之notify
- 液晶12864 C例程。
- 链表 例程(C++)
- C#_SOCKET通信例程
- opencv例程之cvGoodFeaturesToTrack
- Android之Service例程
- 通信例程之GenericApp
- STM32之串口例程
- STM32之RTC例程
- 小Mei。。。爱橘
- 手机的MEI号
- DES MEI号码加密
- MEI card Spec note
- 链接(二)
- 《C++ Primer》读书笔记 第14章:重载运算与类型转换
- IDEA代码提示快捷键Ctrl+Space不生效问题
- 内存分配与回收策略
- List一段代码去重
- MEI例程之 notify.c
- m*n拼图自动还原算法
- 浅析android的MVC模式
- const T、const T*、T *const、const T&、const T*& 的区别
- springboot如何使用velocity模版引擎
- 学习笔记——JAVA设计模式<7>桥接模式
- jsp菜鸟搭配sql server和ssms写BBS论坛(一)
- Preconditions优雅的检验参数
- 创建一个新闻客户端