Thread: Pointer to function and execute function n times

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    33

    Pointer to function and execute function n times

    Hi
    I have to fix the bug in code where function should be execute n times. Provided method is out of the times. Below code to fix
    Code:
    void execute (void (*action)(), int nTimes) {
      for (int i = 0; i < nTimes; i += 1) {
        action();
      }
    }
    I have to idea to fix it in belowe way
    Code:
    static int i=0;
    void pusty(void)
      {i++;}
    typedef void (*action)(void);
    void execute (action pusty, int nTimes) {
      for (; i < nTimes;) {
        pusty();
      }
    }
    but id doesn't work properly and I don't knowe where is mistake.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by gawiellus View Post
    but id doesn't work properly and I don't knowe where is mistake.
    Because you are doing nothing. Here's an example:
    Code:
    #include <stdio.h>
    
    typedef void ( *action )( void );
    
    static int i = 0;
    static void pusty( void ) { i++; }
    //          ^^^^^
    //            |
    //  This is "global"
    //
    
    static void execute( action pusty, int nTimes )
    //                          ^^^^^
    //                            |
    //  This is local ------------+
    //
    {
      for ( ; i < nTimes; )
        pusty();
    }
    
    int main( void )
    {
      execute( pusty, 10 );
    //         ^^^^^
    //           |
    // This is the pointer.
    //
    
      printf( "%d\n", i );
    }

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    33
    I don't understand your answer. Varialbe i is static. It shoul'd keep the value, between function "pusty()" calls.
    This is problem from codewars site: Fix a bug in provided method which should execute a passed action n (possibly hundreds) times. The method is timing out and not completing intime.
    Last edited by gawiellus; 09-18-2020 at 03:57 PM.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by gawiellus View Post
    I don't understand your answer. Varialbe i is static. It shoul'd keep the value, between function "pusty()" calls.
    static but global! It WILL keep the value between funcion calls.

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    33

    I think I know where is mistakie

    I don't have to use static variable. I have to use thread. Code for sample test I put below.
    Code:
    #include <criterion/criterion.h>
    #include <stdio.h>
    #include <stdatomic.h>
    #include <unistd.h>
    
    
    void execute(void (*)(), int);
    
    
    static atomic_int counter;
    
    
    void thread() {
      fputs(".", stdout);
      sleep(1 /* second */);
      counter += 1;
    }
    
    
    Test(the_multiply_function, should_pass_all_the_tests_provided) {
      execute(&thread, 20);
      if (counter != 20) {
        cr_assert_fail("Action was executed %d times instead of %d times", counter, 20);
      } else {
        cr_assert(1);
      }
    }
    The problem is how to wake thread while it is sleeping for 1 second.
    The server codewars allow for 12 s time running code. I tried something like that. But it still doesn't work.

    Code:
    #include <omp.h>
    #pragma omp parallel 
    void execute (void (*action)(), int nTimes) {
      #pragma omp for schedule(dynamic, 1000)
      for (int i = 0; i < nTimes; i += 1) {
        action();
      }
    }
    Can you give me some clues.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why all the omp, when you then go and slug the system by filling it with sleep() ?

    > The server codewars allow for 12 s time running code.
    Is that wall clock time (which sleep() will influence), or CPU time (which sleep() will have no effect on).

    > Can you give me some clues.
    Can you provide code which can simply be copy/paste/compile/tested ?


    FWIW, you can interrupt sleep by using a signal.
    If you can identify the actual process doing the sleeping.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to execute function
    By Satya in forum C Programming
    Replies: 4
    Last Post: 03-12-2017, 04:12 AM
  2. Replies: 3
    Last Post: 03-14-2013, 03:25 PM
  3. Making functions execute at certain times
    By bengreenwood in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2007, 11:55 AM
  4. Calling a function several times
    By JFonseka in forum C Programming
    Replies: 9
    Last Post: 08-22-2007, 12:21 AM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM

Tags for this Thread