473,466 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pointer2function method in pthread_create

42 New Member
Guys,

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

here is the code...

Expand|Select|Wrap|Line Numbers
  1.  .....
  2. void * EventInterface::receiveEvent(int x, void (*ptToFunc)(char *, int))
  3. .....
  4.  
  5. EventInterface::EventInterface(void (*ptToFunc)(char *, int)){
  6.  
  7.     int x;
  8.     pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, ???????)
  9.     ....
  10. }
  11.  
Would you please help me? I have no idea about the syntax I should follow in where I've shown with ????? !
May 16 '07 #1
8 2758
svlsr2000
181 Recognized Expert New Member
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.
May 16 '07 #2
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
May 16 '07 #3
fantasticamir
42 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. .....
  2. void * EventInterface::receiveEvent(void (*ptToFunc)(char *, int));
  3. .....
  4. EventInterface::EventInterface(void (*ptToFunc)(char *, int)){
  5.  
  6.      pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *)(*ptToFunc)(char *, int));
  7.  
  8. }
  9.  
  10.  
May 16 '07 #4
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:

Expand|Select|Wrap|Line Numbers
  1. .....
  2. void * EventInterface::receiveEvent(void (*ptToFunc)(char *, int));
  3. .....
  4. EventInterface::EventInterface(void (*ptToFunc)(char *, int)){
  5.  
  6.      pthread_create( &receiveThread, NULL, EventInterface::receiveEvent, (void *)(*ptToFunc)(char *, int));
  7.  
  8. }
  9.  
  10.  
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
May 16 '07 #5
fantasticamir
42 New Member
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?

Expand|Select|Wrap|Line Numbers
  1.  
  2. void * EventInterface::receiveEvent(void (*ptToFunc)(char *, int));
  3.  
  4. or 
  5.  
  6. void * EventInterface::receiveEvent(void * (*ptToFunc)(char *, int));
  7.  
  8.  
By the way, I guess I should play with that to see which one would be accepted by g++.
May 16 '07 #6
AdrianH
1,251 Recognized Expert Top Contributor
how about the function declaration:
should be like this?

Expand|Select|Wrap|Line Numbers
  1.  
  2. void * EventInterface::receiveEvent(void (*ptToFunc)(char *, int));
  3.  
  4. or 
  5.  
  6. void * EventInterface::receiveEvent(void * (*ptToFunc)(char *, int));
  7.  
  8.  
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
May 16 '07 #7
fantasticamir
42 New Member
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.
May 16 '07 #8
AdrianH
1,251 Recognized Expert Top Contributor
Thanks for your help,

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


Adrian
May 16 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: nightowl | last post by:
Does anybodu know how to do it precisely
12
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...
8
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...
9
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...
5
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,...
1
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 ...
1
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...
1
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...
1
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...
1
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...
0
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...
0
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,...
1
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...
0
agi2029
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,...
0
isladogs
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.