-
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 :)
-
Sorry about that, here are the 2 attached files: 1-1.c and posixtest.h.
Thanks for any help!!
-
//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).
-
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
-
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!!
-
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.
-
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
-
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.