pointer2function method in pthread_create

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fantasticamir
    New Member
    • Apr 2007
    • 42

    pointer2function method in pthread_create

    Guys,

    I want to call a pointer2functio n method in pthread_create. .. I do not know how to do that.

    here is the code...

    Code:
     .....
    void * EventInterface::receiveEvent(int x, void (*ptToFunc)(char *, int))
    .....
    
    EventInterface::EventInterface(void (*ptToFunc)(char *, int)){
    
        int x;
        pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, ???????)
        ....
    }
    Would you please help me? I have no idea about the syntax I should follow in where I've shown with ????? !
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    You cannot pass pass any function which would take arguements other then void * as input to ptheread_create call.

    Probably you could create a void * function T1() which waits for some event to happen in message queue.
    When you want to execute any function in thread context you can pass on those values in message queue.
    Then T1 can get these messages and process them.

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by svlsr2000
      You cannot pass pass any function which would take arguements other then void * as input to ptheread_create call.

      Probably you could create a void * function T1() which waits for some event to happen in message queue.
      When you want to execute any function in thread context you can pass on those values in message queue.
      Then T1 can get these messages and process them.
      That is actually library dependent. I've seen an OS that will allow you to pass up to 10 parameters (VxWorks).

      However, if you wish to make this portable, you should only have one parameter passed. That parameter is a pointer to something which can be a struct containing as much information you are wanting to pass. You just need to document what that object is so that anyone maintaining the code will be able to see that.

      You will of course, have to cast that received value to the passed pointer type.


      Adrian

      Comment

      • fantasticamir
        New Member
        • Apr 2007
        • 42

        #4
        Originally posted by svlsr2000
        You cannot pass pass any function which would take arguements other then void * as input to ptheread_create call.

        Probably you could create a void * function T1() which waits for some event to happen in message queue.
        When you want to execute any function in thread context you can pass on those values in message queue.
        Then T1 can get these messages and process them.
        Ok if we ignore "int x" part (first argument), the second one (the *pt2Func) is a void *, isn't it?

        I mean, may I define this like:

        Code:
        .....
        void * EventInterface::receiveEvent(void (*ptToFunc)(char *, int));
        .....
        EventInterface::EventInterface(void (*ptToFunc)(char *, int)){
        
             pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *)(*ptToFunc)(char *, int));
        
        }

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #5
          Originally posted by fantasticamir
          Ok if we ignore "int x" part (first argument), the second one (the *pt2Func) is a void *, isn't it?

          I mean, may I define this like:

          Code:
          .....
          void * EventInterface::receiveEvent(void (*ptToFunc)(char *, int));
          .....
          EventInterface::EventInterface(void (*ptToFunc)(char *, int)){
          
               pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *)(*ptToFunc)(char *, int));
          
          }
          Don't bother casting the ptrToFunc, pthread_create takes a void*, so it will just autocast it back (if it can, I think that is valid in C/C++).

          I may consider typedefing your function pointer parameter, but that is debatable.


          Adrian

          Comment

          • fantasticamir
            New Member
            • Apr 2007
            • 42

            #6
            Originally posted by AdrianH
            Don't bother casting the ptrToFunc, pthread_create takes a void*, so it will just autocast it back (if it can, I think that is valid in C/C++).

            I may consider typedefing your function pointer parameter, but that is debatable.


            Adrian
            how about the function declaration:
            should be like this?

            Code:
            void * EventInterface::receiveEvent(void (*ptToFunc)(char *, int));
            
            or 
            
            void * EventInterface::receiveEvent(void * (*ptToFunc)(char *, int));
            By the way, I guess I should play with that to see which one would be accepted by g++.

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by fantasticamir
              how about the function declaration:
              should be like this?

              Code:
              void * EventInterface::receiveEvent(void (*ptToFunc)(char *, int));
              
              or 
              
              void * EventInterface::receiveEvent(void * (*ptToFunc)(char *, int));
              By the way, I guess I should play with that to see which one would be accepted by g++.
              They both should be accepted. All you did was change the signature of the function pointer from returning nothing to returning a void pointer.


              Adrian

              Comment

              • fantasticamir
                New Member
                • Apr 2007
                • 42

                #8
                Originally posted by AdrianH
                They both should be accepted. All you did was change the signature of the function pointer from returning nothing to returning a void pointer.


                Adrian

                Thanks for your help,

                I do appreciate it.

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  Originally posted by fantasticamir
                  Thanks for your help,

                  I do appreciate it.
                  No problem. Glad to help.


                  Adrian

                  Comment

                  Working...