LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Multi threading with parallel functions

Hello

 

I have been reading information and examples for multi threading but I find a bit hard to understand the information

 

I have a test process that I need to run in 2 parallel batches, I have 2 identical sets of hardware in the computer as each set will be used for each unit under test, then there is the process of communicating (rs232 and LIN) to each product, send data and receive data, compare the returned data to what is expected to receive and go to next command until all commands are finished, right now I have serialized the testing but it takes a lot of time, so I have been investigating of running both process in Parallel

 

First, both process does not begin at the same time but after a certain quantity of steps have been completed, the one that is ahead will have to wait for the other to sincronize the rest of the testing, also currently my code for the serialized test calls a lot of functions so I am trying to simply it but first I need to understand a couple of things

 

Let's say I have a function like this:

 

#######

int CVICALLBACK prepCycle(void *functionData)
{
int status=0;
int mySide=0;
char myTimestamp[20]="";

mySide=(int)functionData;
mySide--;
partStatus[mySide].part=1;

// first phase
for(int i=0; i<PCBASTEPS; i++)
{
status = simulatePCB();
testResults[mySide][i].result=status;
if(status)
{
strcpy(testResults[mySide][i].returnData, "PCBA PASS");
}
else
{
strcpy(testResults[mySide][i].returnData, "PCBA FAIL");
}
getTimestamp(myTimestamp);
strcpy(testResults[mySide][i].timestamp,myTimestamp);
sideStepCounter[mySide]++;
}
Sleep(10);
partStatus[mySide].part=2;
// Transition to next phase

for(int i=0; i<WRTESTEPS; i++)
{
int index=0;
index=sideStepCounter[mySide];

status = simulateWrite();
testResults[mySide][index].result=status;
if(status)
{
strcpy(testResults[mySide][index].returnData, "WRITING PASS");
}
else
{
strcpy(testResults[mySide][index].returnData, "WRITING FAIL");
}
getTimestamp(myTimestamp);
strcpy(testResults[mySide][index].timestamp,myTimestamp);
sideStepCounter[mySide]++;
}

Sleep(200);
partStatus[mySide].part=3;
// preprare for simultaneous phase

return 0;
}

##########

 

I think I have succesfull called this function in separate threads using the "CmtScheduleThreadPoolFunction" function, then I notice a strange behavior beause the simulated results seem to overlap between threads when they are running at the same time, currently I am using local variables that are storing the results in global array (the "mySide" variable tells which array to use), Is there a way to tell the thread to use separate memory area or to localize the local variables in a different stack? Also I want to know what happens to variables used in "For Loops", do I need to declare all those variables with "CmtNewThreadLocalVar"?

My second question is: when these functions I am multithreading need to access to another function (both of them) what is the safe way to access that function? I think it is best if I upload the current code I am using for testing

 

Any help is appreciated

 

 

edit: can I use structs with "CmtNewThreadLocalVar"?

 

 

Happy Holidays!

 

 

0 Kudos
Message 1 of 1
(1,116 Views)