473,671 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble with pointer to a function

Hi,
I dont understand the third argument of this function. This function
expect the address of a function, but what does "void *(*funktion)(vo id
*)," mean? I am confused by so many "wildcards" :-)

int pthread_create( pthread_t *thread,
const pthread_attr_t *attribute,
void *(*funktion)(vo id *),
void *argumente );
When invoking the pthread_create function I get an error at compile time.

[...]
void BlockOp::start( ) {
if(pthread_crea te(&pth, NULL, &BlockOp::proce ss, cBuffer) != 0) {
...
}
}

void BlockOp::proces s() {
int i;
for (i = 0; i < iLimit; ++i) {
...
}
pthread_exit((v oid *) val++);
}
[...]

error: cannot convert `void (BlockOp::*)()' to `void*(*)(void* )' for
argument `3' to `int pthread_create( pthread_t*, const pthread_attr_t* ,
void*(*)(void*) , void*)' BlockOp.cpp blockop line 17

Any idea how to clear it up?

Regards,
T h o m a s B
Oct 9 '05 #1
3 2320
ben
Thomas Barth wrote:
Hi,
I dont understand the third argument of this function. This function
expect the address of a function, but what does "void *(*funktion)(vo id
*)," mean? I am confused by so many "wildcards" :-)

int pthread_create( pthread_t *thread,
const pthread_attr_t *attribute,
void *(*funktion)(vo id *),
void *argumente );
A pointer to function can be declared as ReturnT (*PtrToFuncT)(A rgT1,
ArgT2...). So void* (*function)(voi d*) means "function" is a
pointer-to-a function which takes a pointer-to-void as an parameter and
returns a pointer-to-void.

So say you have a function called hello:

void* hello(void*);

Then you can assign function with hello:

void* (*function)(voi d*);
function = &hello;


When invoking the pthread_create function I get an error at compile time.

[...]
void BlockOp::start( ) {
if(pthread_crea te(&pth, NULL, &BlockOp::proce ss, cBuffer) != 0) {
...
}
}

void BlockOp::proces s() {
int i;
for (i = 0; i < iLimit; ++i) {
...
}
pthread_exit((v oid *) val++);
}
[...]

error: cannot convert `void (BlockOp::*)()' to `void*(*)(void* )' for
argument `3' to `int pthread_create( pthread_t*, const pthread_attr_t* ,
void*(*)(void*) , void*)' BlockOp.cpp blockop line 17
From the compiler error we know that BlockOp::proces s takes no argument
and returns nothing at all, assuming it is a static member function.

In otherwords, Block::process, assumed static, is of type void(void).
Note that pthread_create requires a pointer to void*(void*) and you
can't therefore pass Block::process.

Any idea how to clear it up?

Regards,
T h o m a s B


If you really don't care about the void* argument you can wrap up
Block::process like so:

void* thread_entry(vo id* trivial)
{
Block::process( );
}

Ben

Oct 9 '05 #2
ben
Thomas Barth wrote:
Hi,
I dont understand the third argument of this function. This function expect the address of a function, but what does "void *(*funktion)(vo id
*)," mean? I am confused by so many "wildcards" :-)
int pthread_create( pthread_t *thread,
const pthread_attr_t *attribute,
void *(*funktion)(vo id *),
void *argumente );

A pointer to function can be declared as ReturnT (*PtrToFuncT)(A rgT1,
ArgT2...). So void* (*function)(voi d*) means "function" is a
pointer-to-a function which takes a pointer-to-void as an parameter and
returns a pointer-to-void.

So say you have a function called hello:

void* hello(void*);

Then you can assign function with hello:

void* (*function)(voi d*);
function = &hello;


When invoking the pthread_create function I get an error at compile time.

[...]
void BlockOp::start( ) {
if(pthread_crea te(&pth, NULL, &BlockOp::proce ss, cBuffer) != 0) {
...
}
}

void BlockOp::proces s() {
int i;
for (i = 0; i < iLimit; ++i) {
...
}
pthread_exit((v oid *) val++);
}
[...]

error: cannot convert `void (BlockOp::*)()' to `void*(*)(void* )' for argument `3' to `int pthread_create( pthread_t*, const pthread_attr_t* ,
void*(*)(void*) , void*)' BlockOp.cpp blockop line 17

From the compiler error we know that BlockOp::proces s takes no argument
and returns nothing at all, assuming it is a static member function.

In otherwords, Block::process, assumed static, is of type void(void).
Note that pthread_create requires a pointer to void*(void*) and you
can't therefore pass Block::process.

Any idea how to clear it up?

Regards,
T h o m a s B

If you really don't care about the void* argument you can wrap up
Block::process like so:

void* thread_entry(vo id* trivial)
{
Block::process( );
}

Ben
Oct 9 '05 #3
Thomas Barth wrote:
Hi,
I dont understand the third argument of this function. This function
expect the address of a function, but what does "void *(*funktion)(vo id
*)," mean? I am confused by so many "wildcards" :-)

int pthread_create( pthread_t *thread,
const pthread_attr_t *attribute,
void *(*funktion)(vo id *),
void *argumente );
When invoking the pthread_create function I get an error at compile time.

[...]
void BlockOp::start( ) {
if(pthread_crea te(&pth, NULL, &BlockOp::proce ss, cBuffer) != 0) {
...
}
}

void BlockOp::proces s() {
int i;
for (i = 0; i < iLimit; ++i) {
...
}
pthread_exit((v oid *) val++);
}
[...]

error: cannot convert `void (BlockOp::*)()' to `void*(*)(void* )' for
argument `3' to `int pthread_create( pthread_t*, const pthread_attr_t* ,
void*(*)(void*) , void*)' BlockOp.cpp blockop line 17

Any idea how to clear it up?

Regards,
T h o m a s B


The function required by pthread_create( ) is of the form

void *thread_fnc(voi d *Data)

You tried to pass a pointer to a non static member function
Create a static member function and pass a pointer to an array
of pointers for the object and required data that can then be
used to call the required member function. Hopefully the sample
code below will point you in the right direction.

class Example
{
public:
static void *thread_fnc(voi d *pData);

private:
void My_Fnc(char *pBuffer);
};

void *Example::threa d_fnc(void *pData)
{
void **p = reinterpret_cas t<void **>(pData);
Example *pObj = reinterpret_cas t<Example *>(p[0])
char *pBuffer = reinterpret_cas t<char *>(p[1])
pObj->My_Fnc(pBuffer );
}
void Some_fnc(void)
{
pthread_t Thr;
Oct 9 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2277
by: Don Bruder | last post by:
Got a stumper here. I imagine that for someone experienced in C++, this is too pathetic for words. For a rookie, using this project as a sort of "midterm exam" in his self-taught "how to program in C++" course, it's seeming to be an insurmountable problem. Can anyone assist? I'm at wit's end here. Everything *LOOKS* (to this rookie, anyway) correct, but the compiler is barfing on it. Here's the class declaration for Bar class Bar {
8
1583
by: Olaf Martens | last post by:
Greetings! Here's another problem that appeared recently: I have the following function call: l_test->Compress(&"filename.dat",l_buf); l_test is a pointer-to-packer that I want to check here. The file name thing should be plain enough, whereas l_buf is declared as a
4
3343
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and if so, why not? Shouldn the compiler be able to tell which of he overloaded functions to use? The second A::comp() is the one I accidently added and gives the error message (in Borland C++Builder 6) Unit1.cpp E2285 Could not find a match for
8
2255
by: Pegboy | last post by:
I am having trouble with malloc() again for a PC app I am developing. The method of the suspicious line of code seems to be Ok on a embedded platform, but not with the PC platform. The embedded platform uses a different compiler. I feel like I'm overlooking a very simple problem, but I can't see it. I would appreciate any help. Thank you. I am trying to allocate memory for a structure of type NAT_S which contains a pointer to a...
2
2232
by: Thomas G. Marshall | last post by:
Arthur J. O'Dwyer <ajo@nospam.andrew.cmu.edu> coughed up the following: > On Thu, 1 Jul 2004, Thomas G. Marshall wrote: >> >> Aside: I've looked repeatedly in google and for some reason cannot >> find what is considered to be the latest ansi/iso C spec. I cannot >> even find C99 in its final draft. Where in ansi.org or the like do >> I find it? > > The official C99 specification is copyright ISO and distributed by > various national...
2
2540
by: spidey12345 | last post by:
what i need this program to do is to read paragraphs like "st blah blh test ere se sit blha eere w" and then it will reformat to "st blah blh test ere se sit blha eere w" ....
1
2136
by: yucikala | last post by:
Hello, I'm a "expert of beginner" in C#. I have a dll - in C. And in this dll is this struct: typedef struct msg_s { /* please make duplicates of strings before next call to emi_read() ! */ int op_type; /* of "op_t" type: operation type; submit (>0), response (<0) */
9
7328
by: Matt | last post by:
Hi friends, Okay so well, I have quite a problem right now with a file stream. What I am doing is to use the Cannon SDK dlls to get control over my old Cannon A60 Camera for some surveillance useage. By using ctypes it all worked well until now. I am able to load the dlls, use a lot of functions, am able to connect to the camera, read out some params, send commands etc... but now I am stuck with reading the picture data.
9
2052
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded the + operator by means of a friend function. Everything worked fine until I decided to use a variable array size (by using new/delete), now I get an error when a temporary object is deleted (e.g. after addition), the error occurs at the delete...
0
8483
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8603
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2818
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2060
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.