Thread: Functions

  1. #1
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134

    Functions

    OK, I have a question. I don't know whats the difference between functions as pointer and normal function, eg:

    void function1(void)
    void *function1(void)

    What is the difference between the two?

    I'm doing parallel programming and we use pointer functions (void *function1(void)) when calling threads. I want to why it is done.

    Sorry if you cant understand the question but I've a hard time explaining things over text.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,560
    Your first example is a declaration of a function named function1 that takes no arguments and returns no value. Your second example is a declaration of a function named function1 that takes no arguments and returns a pointer to void.

    Are you thinking of a function pointer instead, e.g.
    Code:
    void (*fp)(void);
    where fp is declared to be a pointer to a function that takes no arguments and returns no value?
    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

  3. #3
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    yea, Like the pthread_create only accepts a pointer to a function and not the function itself, why is that?

    int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*func)(void*), void *arg);

    why not:

    int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void(func)(void*), void *arg);
    Last edited by BIGDENIRO; 08-30-2014 at 12:05 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,560
    Because that's the way it works: when you pass a function as an argument, it is converted to a function pointer and that is passed as the argument.
    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
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by BIGDENIRO View Post
    Like the pthread_create only accepts a pointer to a function and not the function itself, why is that?

    int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*func)(void*), void *arg);

    why not:

    int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void(func)(void*), void *arg);
    There is no such thing as "passing the function itself". A function is just a block of code that exists at some specific location (address) in memory. So in essense when you see a direct function call just think of it as an operation performed on a const (ie: unchangeable) function pointer.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    ok, so within the function call by the pointer can I have another function calling without a pointer? e,g:

    Code:
    void *function1(void *num)
    {
         int threadID;
         
         threadID = (int)num;
         Task(threadID);
    
         return NULL;
    }
    This can be done without any problems with the threads?

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by BIGDENIRO View Post
    ok, so within the function call by the pointer can I have another function calling without a pointer? e,g:

    Code:
    void *function1(void *num)
    {
         int threadID;
         
         threadID = (int)num;
         Task(threadID);
    
         return NULL;
    }
    This can be done without any problems with the threads?
    It makes no difference because in either case you're essentially manipulating a function pointer. One thing you *do* want to be careful with is static variables being used in functions that run in a threaded context, but that's another topic altogether.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-27-2013, 06:43 PM
  2. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 08:54 AM
  3. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM