Thread: reference with pointers?

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    reference with pointers?

    Hello,

    I am still unfamiliar with some aspects with C++ programming and have a question:

    I was looking at some code that is part of a test suite where there was a function:


    void *a_thread_func()
    {

    pthread_exit(0);
    return NULL;
    }

    what exactly does this ( "void *a_thread_func()" ) mean??

    It is being used for invoking threads in the following line:

    pthread_create(&new_th, &new_attr, a_thread_func, NULL);


    I have included/attached the code and the header (only 2 files), but when I compile this code I get an error of :

    "Passing 'void * (*) () ' as argument 3 of 'pthread_create(pthread_t *, const pthread_attr_t *, void (*)(void*), void *)'

    I was wondering if I need to cast something, although I am not sure what to do??

    I thank in advance for any help that can be provided. It is appreciated

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    43
    Sorry about that, here are the 2 attached files: 1-1.c and posixtest.h.

    Thanks for any help!!

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    //edit:
    You need a function a_thread_create which looks like this:
    void* a_thread_create(void*);

    you have

    void* a_thread_create();

    The void* part in parentheses doesn't need to be touched if you have no arguments.

    And in the future, posix stuff should go in the linux forum.

    //edit2:

    void* at the beginning means the function returns a void pointer (ie: a pointer without a specific type).
    Last edited by ygfperson; 07-21-2003 at 07:13 PM.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    43
    HI, thanks for your reply, unfortunately it is still giving me the same error.
    I guess the attachments didn't stick, but here they are:

    THanks for any help, it is greatly appreciated :


    1-1.c: ( and posixtest.h is pasted below

    #include <pthread.h>
    #include <stdio.h>
    #include <errno.h>
    #include "posixtest.h"


    void *a_thread_func()
    {

    pthread_exit(0);
    return NULL;
    }

    int main()
    {
    pthread_t new_th;
    pthread_attr_t new_attr;
    int ret;

    /* Initialize attribute */
    if(pthread_attr_init(&new_attr) != 0)
    {
    perror("Cannot initialize attribute object\n");
    return PTS_UNRESOLVED;
    }

    /* Destroy attribute */
    if(pthread_attr_destroy(&new_attr) != 0)
    {
    perror("Cannot destroy the attribute object\n");
    return PTS_UNRESOLVED;
    }

    /* Creating a thread, passing to it the destroyed attribute, should
    * result in an error value of EINVAL (invalid 'attr' value). */
    ret=pthread_create(&new_th, &new_attr, a_thread_func, NULL);

    if(ret==EINVAL)
    {
    printf("Test PASSED\n");
    return PTS_PASS;
    }
    else if((ret != 0) && ((ret == EPERM) || (ret == EAGAIN)))
    {
    perror("Error created a new thread\n");
    return PTS_UNRESOLVED;
    }
    else if(ret==0)
    {
    printf("Test PASSED: NOTE*: Though returned 0 when creating a thread with a destroyed attribute, this behavior is compliant with garbage-in-garbage-out. \n");
    return PTS_PASS;
    } else
    {
    printf("Test FAILED: (1) Incorrect return code from pthread_create(); %d not EINVAL or (2) Error in pthread_create()'s behavior in returning error codes \n", ret);
    return PTS_FAIL;
    }

    }

    ------------------------------
    posixtest.h:


    #define PTS_PASS 0
    #define PTS_FAIL 1
    #define PTS_UNRESOLVED 2
    #define PTS_UNSUPPORTED 4
    #define PTS_UNTESTED 5

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    THANK YOU!!

    THANK YOU!!, you were right, I'll make sure to post any future POSIX ?s in the LINUX forum.

    THANKS AGAIN!!!

    //another question:

    void * a_thread_func (void *),

    I understand that as you said it return a pointer without a type, but what does the (void *) mean?? , there are no arguments, but I am unclear about the (void *). THanks for your explanation!!
    Last edited by smd; 07-21-2003 at 07:28 PM.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The function that pthread_create expects would have the form: void *a_func(void *). That void* is a pointer to essentially anything. In this case, your function would take it, and try to cast it to whatever argument type it expects (if any), and then uses it.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Take a look at malloc

    Code:
    void* malloc(int byte_count);
    or something.

    Well, when you use malloc don't you type cast?
    like
    Code:
    char* blah;
    blah = (char) malloc(4);
    void pointers are like pointers without names, and you can rename them. Although it can become dangerous later, but for functions like malloc, it is really useful. HOWEVER this is the C++ board, and malloc is very uncommon in C++ (from my experience), but I noticed u were posting POSIX code, I could have sworn that POSIX was in C. Well anyway, C uses malloc() and C++ uses new.

    http://www.rt.com/man/malloc.3.html

    for more info on malloc() and the alloc family.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The proper (C) conversion would be:
    Code:
    char* x = (char*)malloc(10); // For example.
    In C++, you generally want to stay away from C style conversions, and instead prefer dynamic_cast, static_cast, and reinterpret_cast (though this is the least safe of the three). In addition, their is a const_cast.

    >> I understand that as you said it return a pointer without a type, but what does the (void *) mean?? , there are no arguments, but I am unclear about the (void *). THanks for your explanation!!

    The void* is an argument, actually. Its a pointer to a type which could be anything. Its like passing an int* (or char* or my_struct* etc...) except more versatile and not as safe. In generally, polymorphism or templates should be prefered (though Pthreads is a C library, so of course it doesn't have these).

    If your interested in threads, you might want to look at the Boost library (www.boost.org). They have a really C++ thread library.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Reference and Pointers
    By Shal in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2006, 04:48 PM
  4. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  5. GCC - Strange networking functions error.
    By maththeorylvr in forum Windows Programming
    Replies: 3
    Last Post: 04-05-2005, 12:00 AM