完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
本帖最后由 mengzhuhao 于 2014-11-1 11:07 编辑
系统自带例子MultiPanel中关于多线程处理的疑问 在看一些系统自带的多线程例子,例如一个叫MultiPanel的例子 按照单步debug的步骤观察程序执行的情况,在下面的时候感觉比较疑惑 例如在添加线程与删除进程的时候,是如何理解触发的? //---------------------------------------------------------------------------- // ScheduleFunctionCallback //---------------------------------------------------------------------------- int CVICALLBACK ScheduleFunctionCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: ScheduleNewFunction (); //点击触发创建新线程 break; } return 0; } 在执行添加线程后会跳转到: //---------------------------------------------------------------------------- // ScheduleNewFunction //---------------------------------------------------------------------------- static int ScheduleNewFunction (void) { static int threadCount = 0; int fnID; int error = 0; errChk (CmtScheduleThreadPoolFunctionAdv (poolHandle, ChartPanelMain, (void *)threadCount, THREAD_PRIORITY_NORMAL, NotifyThreadFunctionExecState, (EVENT_TP_THREAD_FUNCTION_BEGIN | EVENT_TP_THREAD_FUNCTION_END), 0, CmtGetCurrentThreadID(), &fnID));//这块不是很理解! // Add thread to list threadCount++;//这块是不是还只是累加,跟线程增加还没有发生关系吧? RefreshFunctionListBox (); Error: if (error < 0) { CmtGetErrorMessage (error, errorBuf); MessagePopup ("Error:", errorBuf); } return error; } 怎么看出线程在增加?是通过那个命令添加的呢?errChk 括号里面的NotifyThreadFunctionExecState, (EVENT_TP_THREAD_FUNCTION_BEGIN | EVENT_TP_THREAD_FUNCTION_END),是通过那个方式判断是新建还是删除?在上面程序中执行threadCount++;RefreshFunctionListBox ();其中threadCount是怎么传递到后面的程序的呢? 然后跳转至下面: //---------------------------------------------------------------------------- // RefreshFunctionListBox //---------------------------------------------------------------------------- static int RefreshFunctionListBox (void) { int numItems, index; int fnID; #ifdef _NI_mswin_ HANDLE threadHandle; #endif /* _NI_mswin_ */ int threadPriority; GetNumListItems (mainPanelHandle, MAINPANEL_FUNCTIONLISTBOX, &numItems); if (numItems != 0) { // Update the Thread Priority Control GetCtrlIndex (mainPanelHandle, MAINPANEL_FUNCTIONLISTBOX, &index);//这些都是获取列表中的信息 GetValueFromIndex (mainPanelHandle, MAINPANEL_FUNCTIONLISTBOX, index, &fnID); #ifdef _NI_mswin_ CmtGetThreadPoolFunctionAttribute (poolHandle, fnID, ATTR_TP_FUNCTION_THREAD_HANDLE, &threadHandle);//这个难度就是创建了新线程?之前的threadCount是怎样传递过来的呢? if ((threadPriority = GetThreadPriority (threadHandle)) == THREAD_PRIORITY_ERROR_RETURN) MessagePopup("Warning", "GetThreadPriority failed"); else SetCtrlVal (mainPanelHandle, MAINPANEL_THREADPRIORITY, threadPriority); #endif /* _NI_mswin_ */ } DimMainPanelCtrls (); return 0; } 执行删除线程的时候会触发下面的程序 //---------------------------------------------------------------------------- // TerminateThreadCallback //---------------------------------------------------------------------------- int CVICALLBACK ExitFunctionCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { int index; int fnID; unsigned int threadID; int error = 0; switch (event) { case EVENT_COMMIT: GetCtrlIndex (mainPanelHandle, MAINPANEL_FUNCTIONLISTBOX, &index); if (index < 0) { MessagePopup("Terminate Thread...", "There are no threads running"); break; } GetValueFromIndex (mainPanelHandle, MAINPANEL_FUNCTIONLISTBOX, index, &fnID); CmtGetThreadPoolFunctionAttribute (poolHandle, fnID, ATTR_TP_FUNCTION_THREAD_ID, &threadID); // Tell the thread to execute QuitUserInterface. PostDeferredCallToThread (QuitUICallback, 0, threadID);//这个应该就是执行一句删除线程的命令,但是会跳转到后面的一个程序,不知道是如何理解的 break; default: break; } Error: if (error < 0) { CmtGetErrorMessage (error, errorBuf); MessagePopup ("Error:", errorBuf); } return 0; } 这是按步骤跳转到的程序,这个程序应该是怎样去理解?在编程的过程中是执行了PostDeferredCallToThread (QuitUICallback, 0, threadID);命令就直接跟着这个吗?这个程序里面的switch (event)的event值是实际运行中是怎样获得的?是执行了PostDeferredCallToThread后event就是对应的EVENT_TP_THREAD_FUNCTION_END? //---------------------------------------------------------------------------- // NotifyThreadBegin //---------------------------------------------------------------------------- static void CVICALLBACK NotifyThreadFunctionExecState (CmtThreadPoolHandle poolHandle, CmtThreadFunctionID functionID, unsigned int event, int value, void *callbackData) { static char buf[256]; int index; unsigned int threadID; switch (event) { case EVENT_TP_THREAD_FUNCTION_BEGIN: // Insert the function information in the list box. CmtGetThreadPoolFunctionAttribute (poolHandle, functionID, ATTR_TP_FUNCTION_THREAD_ID, &threadID); sprintf (buf, "%d 33p65l0x%x", functionID, threadID); InsertListItem (mainPanelHandle, MAINPANEL_FUNCTIONLISTBOX, -1, buf, functionID); RefreshFunctionListBox (); break; case EVENT_TP_THREAD_FUNCTION_END: // Remove the function information from the list box. GetIndexFromValue (mainPanelHandle, MAINPANEL_FUNCTIONLISTBOX, &index, functionID); DeleteListItem (mainPanelHandle, MAINPANEL_FUNCTIONLISTBOX, index, 1); RefreshFunctionListBox (); CmtReleaseThreadPoolFunctionID (poolHandle, functionID); break; default: break; } } 希望有这方面经验的朋友帮忙看看 |
|
相关推荐
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
在只有一个电子负载仪的情况下,如何持续监控并记录太阳能充电板的全程充电电流?
1521 浏览 1 评论
5894 浏览 1 评论
9538 浏览 1 评论
8898 浏览 1 评论
1348 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-28 00:06 , Processed in 0.473274 second(s), Total 70, Slave 54 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号