Guys,
I want to call a pointer2function method in pthread_create... I do not know how to do that.
here is the 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 ????? !
8 2758
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.
AdrianH 1,251
Recognized Expert Top Contributor
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
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: -
.....
-
void * EventInterface::receiveEvent(void (*ptToFunc)(char *, int));
-
.....
-
EventInterface::EventInterface(void (*ptToFunc)(char *, int)){
-
-
pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *)(*ptToFunc)(char *, int));
-
-
}
-
-
AdrianH 1,251
Recognized Expert Top Contributor
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: -
.....
-
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
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? -
-
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++.
AdrianH 1,251
Recognized Expert Top Contributor
how about the function declaration:
should be like this? -
-
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
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.
AdrianH 1,251
Recognized Expert Top Contributor
Thanks for your help,
I do appreciate it.
No problem. Glad to help.
Adrian
Sign in to post your reply or Sign up for a free account.
Similar topics
by: nightowl |
last post by:
Does anybodu know how to do it precisely
|
by: Huskier |
last post by:
Hi all:
I want to pass a class-member function to pthread_create, and my
code is in the following, but I don't know how to pass
myobj.thread_function to pthread_create function.
#include...
|
by: Thorsten Kiefer |
last post by:
Hi,
my compiler tells me that the address of a static method will always
evaluate to true (which is 1). Why that ? How can i get the address of a
static method ?
I'm using gcc3.
Regards...
|
by: Gary Wessle |
last post by:
Hi
I am trying to understand how pthread is used, so I make the scenario
below, I could not understand the erros by reading the man
pthread_create.
//**************** code start...
|
by: Gary Wessle |
last post by:
I am getting
task.cpp:109: error: argument of type 'void (Spac_task::)()' does not
match 'void* (*)(void*)'
when trying to execute the line
int thr_id = pthread_create(&p_thread, NULL,...
|
by: usa777 |
last post by:
There are pthread_create symbols in libphread.so, libthread.so and libc.so.
The following symbols displayed with the nm command:
Value Size Type Bind Other Shndx Name
...
|
by: erwann |
last post by:
On a Linux platform:
Hi,
I am just trying to pass an integer as an argument to my start_routine when creating a thread with pthread_create:
In my .h file:
short portfd;
void...
|
by: dshereck |
last post by:
Hello,
I implemented a simple c++ thread wrapper; it works in Linux, but fails in
BSD.
Here is the code. The idea is that an other class will inherit and implement the virtual methods Setup...
|
by: unclefester |
last post by:
Could someone please explain to me the return type of pthread_create()'s 3rd parameter? I know it should be a function but I keep getting the error: argument of type 'void' (Test::)(void*)' does not...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |