Like Tree1Likes
  • 1 Post By grumpy

calling same function simultaneously

This is a discussion on calling same function simultaneously within the C Programming forums, part of the General Programming Boards category; Hi all, I need a help about function operation. I wrote a function that measures the passed time and returns ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    calling same function simultaneously

    Hi all,

    I need a help about function operation. I wrote a function that measures the passed time and returns 1 when reaches the input value using timer of the microcontroller. Timer of microcontroller always counts up and function gets timer value when it is called first time, calculates when to terminate and returns 1 when it reaches the input value. Can I use the same function simultaneously to measure another passed time if the variables of the function are all local?

    measure_time (t1000);
    measure_time (t250);

    I want the function return 1 for the t250 when it reaches 250ms and return 1 for t1000 when it reaches 1000ms. Does this function store the local values at different locations of the memory and operate well?

    Code:
    unsigned char measure_time (unsigned short miliseconds)
    {
     unsigned long first_ent,ftimer_first, ftimer_last,fcount_first, fcount_last, fcount_before ; 
    	if (!first_ent)
    	{
    		ftimer_first = TMR1;					// get first timer value
    		fcount_first = t_count;				// get ms begin value
    		fcount_last = fcount_first + miliseconds;		// last ms value
    		first_ent = 1;
    		if (fcount_last > 0xFFFF)			// if last value is bigger than max t_count value
    		{
    			fcount_before = 0xFFFF - fcount_first;
    			fcount_last = miliseconds - fcount_before;
    		}
    	}
    	ftimer_last = TMR1;				// get last timer value
    	if (t_count == fcount_last && ftimer_last >= ftimer_first)	// exit loop 
    	{
    		first_ent = 0;
    		return 1;
    	}	
    	return 0;
    }
    If not how can I deal with it? Would like to see your answers soon, thanks in advance.

    Erhan

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    I doubt it.

    I'm not even convinced that the function would operate as intended if only called once.

    first_ent is declared as a local auto variable (i.e. one instance every time the function is called) but it is not initialised before it is tested. That is undefined behaviour.

    t_count and TMR1 are not even declared in the function. If your code even compiles, that means they are either global variables or some other global resources that should not be accessed simultaneously by different sections of code, unless they are specifically protected from multiple simultaneous accesses (or able to cope with such accesses) by some other means.
    stahta01 likes this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    6
    Sorry this is not the original programme. I modified it in order to be clear but even made it worse. Actually all the variables are global in this function. t_count is a global variable of timer interrupt and TMR1 is timer register. They are not necessary to be understood. But see this code, function is called 2 times in a loop and each time it should keep values at different memory locations to solve the issue.
    Code:
    while(1)
    {
    measure_time (t1000);
    measure_time (t250);
    if (measure_time (t1000) ==1) do job1;  ------ > do job1 is anything, just an algorithm to be clear
    if (measure_time (t250) ==1) do job2;  ------ > do job2 is anything, just an algorithm to be clear
    }
    Function can measure time for 1 call, but 2 calls in a loop won't work. Because global variables would get messed. That's why I made them local but I don't think it also work. Each time programme leaves the function, locals would be resetted, won't they? Should I write another function same as this one but different in global variables? That doesn't seem elegant.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    locals would be resetted
    Just use static variables.


    Why not leave out the first call (so, conceptually):
    Code:
    measure_time (t1000); <--leave out
    measure_time (t250); <--leave out
    if (measure_time (t1000) ==1) do job1;  ------ > do job1 is anything, just an algorithm to be clear
    if (measure_time (t250) ==1) do job2;  ------ > do job2 is anything, just an algorithm to be clear

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    6
    Quote Originally Posted by mike65535 View Post
    Just use static variables.
    Why not leave out the first call (so, conceptually):
    [/CODE]
    time is critical ... the whole program does many things simultaneously, there is no time to wait for one part of the program, that's why I use timer interrupt of the microcontroller and calculate the time interval since first call of the function. would static variables store the values for t1000 and t250 at different locations of the memory respectively?

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    I don't understand what you are trying accomplish. Why call measure_time twice each for t1000 and t250? Does it do something besides return a value? I ask because you don't use any return value the first time you call. Saying "time is critical" doesn't help me.

    As to static - look it up.

    Code:
    int foo( void ) {
        static int temp_int = 0;
        temp_int++;
        return temp_int;
    }
    In that code, foo will return 1, then 2, then 3
    It keeps temp_int permanently - this has the characteristics of a global in that the value held in temp_int transcends the calling of the function foo, but also has the benefits of keeping that variable "local" (hidden) from other functions.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,553
    Remember using "static" variables makes a function not reusable from two different threads. And, would likely make it not work for what you want.

    But, what you want is not really very clear; and, what you have is even less clear.

    Is the function "measure_time" supposed to be a blocking time delay function? (One that wait the amount of time and then returns)
    Is it supposed to be an non-blocking time delay function? (One that returns different values depending on if delay is over or not.)

    Tim S.
    Last edited by stahta01; 05-04-2011 at 01:34 PM.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    6
    Quote Originally Posted by mike65535 View Post
    I don't understand what you are trying accomplish. Why call measure_time twice each for t1000 and t250? Does it do something besides return a value? I ask because you don't use any return value the first time you call. Saying "time is critical" doesn't help me.

    As to static - look it up.

    Code:
    int foo( void ) {
        static int temp_int = 0;
        temp_int++;
        return temp_int;
    }
    In that code, foo will return 1, then 2, then 3
    It keeps temp_int permanently - this has the characteristics of a global in that the value held in temp_int transcends the calling of the function foo, but also has the benefits of keeping that variable "local" (hidden) from other functions.
    dear mike,
    measure_time does only measure the time from it is first called to the input value. lets say t1000 = 1000. So measure time measures 1000ms of time. But while it is measuring, the program should do other stuff so measure_time is non-blocking time delay function (@stahta01). You shouldn't get confused of the serial calls of measure_time(t1000) and measure_time(t250). This is not how they are used in the original program but I wanted to simplify the fact and focus on the thing I wanted to deal.

    Lets try to explain in a different way,

    I have an endless while loop in main. I have a function. Function is called 2 times with different inputs a and b in a loop. Here is the important part. Function can't return the output in 1 cycle of the loop, it should cycle and count many times. It has to hold and store some values and use them each time it is called. Here is an other important info. Function understands when did it first time called. When the input a calls the function first time, function calculates and stores some variables. In the second loop the function remembers it is called by input a before and do jump some job and calculate some other. It does the job until it reaches the requested number of calls. ... The same applies for the input b. I just want the function store variable values for a and b at different memory locations and understand which variables it should use ( of input a or input b ).

    I suppose I am more clear now, but my english is restricted and this is how I can explain with all I have got... If you still got confused thanks for your replies anyway.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int foo( int x )
    {
        if( x < 1 )
            do_output();
        else
            do_otherstuff();
    }
    
    ...
    
    int x = 500, y = 1000;
    while( whatever )
    {
        foo( x-- );
        foo( y-- );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    6
    quzah thats a clever one ...

    However the function doesn't count number of cycles in the while loop. There is a hardware timer which counts 1ms of time and resets, also does this endlessly every 1ms. The timer operates independently. The function actually reads the timer value when it is first called and calculates how many times the timer should reset in order to reach input ( a and b) values. The while loop of main function contains and does many job. So when function is called by input a in a new loop may see the timer has lets say resetted 3 times since last called. But in the next loop lets say it has reseted 7 times ... Here I am trying to measure passed real time. I don't have time to wait so the measurement must be nonblocking so that rest of the code should operate.
    Last edited by xeratule; 05-05-2011 at 03:12 AM. Reason: lack of explanation

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int lapse( int start, int *stop )
    {
        return start == *stop++;
    }
    
    ...
    
    x = y = 0;
    while( something )
    {
        timer();
        if( lapse( 500, &x ) ) dox_output();
        if( lapse( 1000, &y ) ) doy_output();
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    not reusable from two different threads
    OK, I don't believe it was mentioned that this was the case

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    6
    quzah that's not what I am looking for but shoved me a vision, maybe I can go on from here ...

    It is obvious that I can't explain my situation clearly. So I have to close the topic here.
    To all who responded: Thanks for your valuable replies.

    Erhan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  2. calling function & called function
    By Sajal in forum C Programming
    Replies: 4
    Last Post: 06-05-2009, 09:37 AM
  3. function calling within another function error
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 12:40 AM
  4. Calling Cdocument function in Cview function
    By RancidWannaRiot in forum Windows Programming
    Replies: 5
    Last Post: 09-22-2005, 12:09 PM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21