LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

computer frozen in secondary thread

Hello,
 
I noticed my computer will freeze when I put one of the following functions in the secondary thread:
1. Timer ()
2. Delay ()
3. Fmt ()
 
The program will run for a while, then it freezes. I cannot use terminate button to stop it. I can only stop it by "tast manager" in windows. Anyone knows the reason? Thanks.
 
0 Kudos
Message 1 of 13
(4,952 Views)

Here is the code you can try. I feel the Dealy (0.1) in main thread is the reason for causing the problems. I just don't know why and how.

#include <formatio.h>
#include <dataacq.h>
#include <utility.h>
#include <ansi_c.h>
#include <cvirte.h>  
#include <userint.h>
#include "real time.h"


static int panelHandle, tsqHandle;

int CVICALLBACK WriteQueue (void *functionData);

int threadID, j;
int stopFlag, plotFlag;

int main (int argc, char *argv[])
{
 if (InitCVIRTE (0, argv, 0) == 0)
  return -1; /* out of memory */
 if ((panelHandle = LoadPanel (0, "real time.uir", PANEL)) < 0)
  return -1;
 DisplayPanel (panelHandle);
 RunUserInterface ();
 DiscardPanel (panelHandle);
 return 0;
}

int CVICALLBACK Start (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int number, readID, i;

 switch (event)
  {
  case EVENT_COMMIT:
   
   
   CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, WriteQueue,
                               0, &threadID);
            stopFlag = 0;
            plotFlag = 0;
            j = 0;
           
            while (1)
            {
             if (stopFlag == 1) break;
    
             Delay (0.1);
             SetCtrlVal (panel, PANEL_Z, (double) j++);
             ProcessSystemEvents ();
            } 

   CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, threadID, 0);
   CmtReleaseThreadPoolFunctionID (DEFAULT_THREAD_POOL_HANDLE, threadID);
   break;
  }
 return 0;
}

int CVICALLBACK Stop (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
  {
  case EVENT_COMMIT:
   
   stopFlag = 1;
   break;
  }
 return 0;
}

 

int CVICALLBACK WriteQueue (void *functionData)
{

 double dsp;
 char read_string [300];
 
// while (1) if (stopFlag == 1) break;     /*This is a correct one to show mulithread working.*/

 while (1)
 {                                                          /*Frozen1: Z can not go beyond 100*/
  if (stopFlag == 1) break;
  Delay (0.1);
 }

// while (1)
// {                                                       /*Frozen2: Z can not go beyond 100*/
//  if (stopFlag == 1) break;
//  Timer ();
// }


// while (1)                                          /*Forzen3: Z can not go beyond 200.*/
 //{
// if (stopFlag == 1) break;
//  strcpy (read_string, "123");
//  Fmt (&dsp, "%f<%s", read_string);
// }
      
 
 return 0;
  
}

 

0 Kudos
Message 2 of 13
(4,933 Views)
I cannot see in your code a clear reason explaining why the computer freezes.
Anyway, something can be tried to approach the problem.
 
I suppose the Deay () functions you inserted in your code take the place of actual different code that performs something: you could try replacing Delay with SDK Sleep funcion and see what's happening. Anoter trial could be to substitute your Dealy with close loops that take some machine times (e.g. cunting a variable up to a given limit)
 
Additionally, it seems to me that your stopFlag variable should be defined as VOLATILE: VOLATILE qualifier tells the compiler that the object at this location may change, so that compiler should always perform an actual read of this value instead of using possibly previously cached values.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 13
(4,898 Views)

Hello,

Did you really try to run the codes? I want to know your result. If same thing happens, I would like to know the solution for it. Thanks.

 

0 Kudos
Message 4 of 13
(4,898 Views)
I will run it tomorrow in the office.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 5 of 13
(4,895 Views)

Thank you very much.

 

0 Kudos
Message 6 of 13
(4,894 Views)

Hi,

I did run you code on CVI 6.0. It did not freeze, but it did error when I started a second thread and then try to stop a thread.

Have you looked at the examples for MultiThreading, that come with CVI.

Regards

Ray Farmer

Regards
Ray Farmer
0 Kudos
Message 7 of 13
(4,884 Views)

Hello,

Thanks. That's what I want to demonstrate there are some Timer () related problems for multithread in CVI. When you just run the single while (1) loop, the code runs correct. However, if you replace that with the other commented codes, the computer will freeze.

Before I wrote the codes, I have already carefully studied many examples in CVI regarding mulithreading. I am using CVI7.1.

 

0 Kudos
Message 8 of 13
(4,861 Views)

Well, I tried your code building a simple project without errors in none of your stated conditions: secondary tread has always been runnig counting up to 3000 and more... Nor I got any errors when stopping threads.

I am attaching my project for you to test.

System: Win XP-PRO with SP2 - CVI 7.1.1 (313)



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 9 of 13
(4,851 Views)

Hello,

Thank you very much. I run your code in my computer. My computer froze again when Z hits 14. I can not stop it either. I am using CVI7.1

Thanks.

 

0 Kudos
Message 10 of 13
(4,831 Views)