Thread: Want a function that accepts either a bool value or a function ptr that returns bool

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    21

    Want a function that accepts either a bool value or a function ptr that returns bool

    Basically, I have a function called "monitor" and basically, this function works by waiting until some expression is false, and then it returns.

    Right now, for this specific instance, it compares two uint16_t's. If they are equal, it will "sleep" with a condition variable. When one of the uint16_t's is changed, the changing thread will signal on the condition variable, and the function will wake up, do the comparison, and finally return.

    How can I generalize this "monitor function" so that it could accept a generic function that returns a bool value?

    For example, right now, I have

    Code:
    void Monitor(uint16_t start_val, uint16_t &tail)
    {
         mutex.lock()
         while (start_val == tail) {
              condition.wait(mutex);
         }
    }
    but I want to generalize the input parameters to accept a boolean function. The programmer can specify any number of parameters to this boolean function, so basically, the loop would be

    Code:
    void Monitor(bool function)
    {
         mutex.lock()
         while (function(param p1, param p2, ..., param pn) {
              condition.wait(mutex);
         }
    }
    Is there any way to do this?
    Last edited by joshuastuden; 09-21-2018 at 02:10 PM.

  2. #2
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    149
    Easy:

    Code:
    template <typename Function>
    void Monitor(Function function)
    {
        // ...
    }

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    21
    But how do I know what parameters to pass into function() when I call it from monitor?

    Quote Originally Posted by Sir Galahad View Post
    Easy:

    Code:
    template <typename Function>
    void Monitor(Function function)
    {
        // ...
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,857
    Considering that you want to pass "any number of parameters", you would probably need a variadic template:
    Code:
    template<typename Function, typename... Arguments>
    void Monitor(Function function, Arguments... arguments)
    {
        mutex.lock();
        if (function(arguments...))
        {
            condition.wait(mutex);
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    21
    Thank you. This is very helpful. I can't seem to call Monitor, though. I get a lot of compilation errors.

    I have

    Code:
    bool compare(int a, int b, int c)
    {
         return a == b == c;
    }
    but how do I invoke Monitor, which is a member function of a class?

    obj->Monitor(compare(val1, val2, val3));

    ?

  6. #6
    Registered User
    Join Date
    Oct 2017
    Posts
    21
    I no longer think this is correct. How do I ensure that the function returns a boolean?

    Quote Originally Posted by Sir Galahad View Post
    Easy:

    Code:
    template <typename Function>
    void Monitor(Function function)
    {
        // ...
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,857
    Quote Originally Posted by joshuastuden
    but how do I invoke Monitor, which is a member function of a class?

    obj->Monitor(compare(val1, val2, val3));

    ?
    Notice that it invokes the function for you with the supplied arguments, so of course you cannot just call the function: you would end up with what the function returns, i.e., a bool, which is of course not what the function template expects. Hence:
    Code:
    obj->Monitor(compare, val1, val2, val3);
    Quote Originally Posted by joshuastuden
    How do I ensure that the function returns a boolean?
    On one hand, that's complicated. The solutions typically lie in something called concepts, but I'm afraid I have never used them, and as it stands it is on the path to standardisation but was rejected from the most recent version of the C++ standard (C++17), though a version of it may eventually be published in C++20. So generally, I'd say you're out of luck unless you want to go with a non-standard implementation, e.g., Boost Concept Check Library.

    On the other hand, that's simple: you don't. If the function doesn't return something that can be implicitly converted to bool, a compile error will result.

    By the way, this function is probably wrong:
    Code:
    bool compare(int a, int b, int c)
    {
         return a == b == c;
    }
    If your intention was to compare to see if a and b and c are all equal to each other, then you should have written:
    Code:
    return a == b && b == c;
    Otherwise, suppose a = 0, b = 0, and c = 0. Then a == b evaluates to true, and true == c evaluates to false since 0 is converted to false, and true == false evaluates to false.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. control reaches end of non void function in bool function
    By Giwrgows x in forum C Programming
    Replies: 26
    Last Post: 12-19-2015, 02:58 PM
  2. Replies: 11
    Last Post: 12-14-2015, 02:43 PM
  3. Help with Bool function
    By jaimeribg in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2014, 01:38 PM
  4. A function that returns a bool if true.
    By MCDD in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2012, 03:58 PM
  5. bool function
    By MB1 in forum C++ Programming
    Replies: 10
    Last Post: 04-22-2005, 05:31 PM

Tags for this Thread