CVI多线程数据保护(安全变量)

DefineThreadSafeScalarVar(,,);此函数应用
#include 
#include   
#include 
int CVICALLBACK ThreadFunction (void *functionData);
int CVICALLBACK ThreadFunction1 (void *functionData);
int CVICALLBACK ThreadFunction2 (void *functionData);
//int CVICALLBACK ThreadFunction3 (void *functionData);
int threadID;
int threadID1;
int threadID2;
//int threadID3;
long g_nNum = 0;
long g_nNum1 = 0;
long g_nNum2 = 0;
static int startflag = 0;
static int startflag1 = 0;
static int startflag2 = 0;
//static int startflag3 = 0; 
DefineThreadSafeScalarVar(long,g_nNum,0);
DefineThreadSafeScalarVar(long,g_nNum1,0);
DefineThreadSafeScalarVar(long,g_nNum2,0);
int main()  
{   
	Initializeg_nNum();
	Initializeg_nNum1();
	Initializeg_nNum2();
	CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE,ThreadFunction1 ,NULL ,&threadID1);

	CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE,ThreadFunction ,NULL ,&threadID);


	
	CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE,ThreadFunction2 ,NULL ,&threadID2);
	
	startflag1 = 1; 
	startflag = 1; 
	startflag2 = 1; 
    
	CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE,threadID, OPT_TP_PROCESS_EVENTS_WHILE_WAITING);  
	CmtReleaseThreadPoolFunctionID (DEFAULT_THREAD_POOL_HANDLE, threadID);
	CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE,threadID1, OPT_TP_PROCESS_EVENTS_WHILE_WAITING);  
	CmtReleaseThreadPoolFunctionID (DEFAULT_THREAD_POOL_HANDLE, threadID1);
	CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE,threadID2, OPT_TP_PROCESS_EVENTS_WHILE_WAITING);  
	CmtReleaseThreadPoolFunctionID (DEFAULT_THREAD_POOL_HANDLE, threadID2);
	Uninitializeg_nNum();
	Uninitializeg_nNum1();
	Uninitializeg_nNum2();
	return 0;
}
int CVICALLBACK ThreadFunction (void *functionData)
{
	long *g_nNumPtr;
	long *g_nNumPtr2;
	while(startflag)
	{
		Sleep(0);//some work should to do  
	    g_nNumPtr = GetPointerTog_nNum();
        (*g_nNumPtr)++;
		ReleasePointerTog_nNum();
		
		g_nNumPtr2 = GetPointerTog_nNum2();
        (*g_nNumPtr2)++;
		ReleasePointerTog_nNum2();
	//	printf("全局资源值1为%d 全局资源值1为%d\n", g_nNum,g_nNum2);
    	Sleep(100);//some work should to do  
		  
	}
    return 0;  
}
int CVICALLBACK ThreadFunction2 (void *functionData)
{
	long *g_nNumPtr1; 
	while(startflag2)
	{
		Sleep(0);//some work should to do  
        g_nNumPtr1 = GetPointerTog_nNum1();
        (*g_nNumPtr1)++;
		ReleasePointerTog_nNum1();
	//	printf("全局资源值2为%d\n", g_nNum1);
    	Sleep(200);//some work should to do  
	}
    return 0;  
}
int CVICALLBACK ThreadFunction1 (void *functionData)
{
	while(startflag1)  
	{
		Sleep(0);//some work should to do  
 		printf("全局资源值1*********为%d %d \n", Getg_nNum(),Getg_nNum2());
 		printf("全局资源值2*********为%d\n", Getg_nNum1());
		Sleep(100);//some work should to do 
	}   
    return 0;  
}

CVI多线程数据保护(安全变量)_第1张图片

你可能感兴趣的:(CVI编程)