473,322 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

This pointer question!

Guys,

As you know static member functions do not have a this pointer. so
instead, what shall i do?

for example ... consider I want to call

Code: ( text )

1.
pthread_create(&thID,NULL,classA::B, (void *) this);

and it is called in an static method so instead of this what can I
put??

Thanks,

Amir.

May 28 '07 #1
8 2128
am***@parsonline.net wrote:
Guys,

As you know static member functions do not have a this pointer. so
instead, what shall i do?

for example ... consider I want to call

Code: ( text )

1.
pthread_create(&thID,NULL,classA::B, (void *) this);
An off topic but common question. You must pass an extern "C" linkage
function to C library functions that expect a function pointer as one of
their parameters.

You *must* not pass a class member function and *should* not pass a
static member function.

--
Ian Collins.
May 28 '07 #2
Ian Collins a écrit :
am***@parsonline.net wrote:
>Guys,

As you know static member functions do not have a this pointer. so
instead, what shall i do?

for example ... consider I want to call

Code: ( text )

1.
pthread_create(&thID,NULL,classA::B, (void *) this);
An off topic but common question. You must pass an extern "C" linkage
function to C library functions that expect a function pointer as one of
their parameters.

You *must* not pass a class member function and *should* not pass a
static member function.
I didn't know about the static member function. Why is that (some
portability issue on their representation I presume)?

You can use template along the following pattern (using intermediary
class or not, using richer templatization ...):

//! execute member function
template<class T>
void* pthread_class_threadifier(void* param)
{
//better to add try/catch otherwise, stack unwinding doesn't happen
in some cases
std::auto_ptr<std::pair<T*,void(T::*)() p(
reinterpret_cast<std::pair<T*,void(T::*)()>*>(para m));

((p->first)->*(p->second))();

return NULL;
}

//! start thread on given method of given class
template<class T>
int pthread_create_class(pthread_t& thID, T& object, void (T::*method)())
{
std::pair<T*,void (T::*)()>* param=
new std::pair<T*,void (T::*)()>(&object,method);

int ret= pthread_create(&thID,NULL,
pthread_class_threadifier<T>,
reinterpret_cast<void*>(param));

if(ret)delete param;

return ret;
}

And then:
//test class
class A
{
public:
//this method will be threadified
void a()
{
std::cout<<"OK"<<std::endl;
}
};

int main()
{

A a1;
pthread_t pid;

//create thread
int ret=pthread_create_class(pid,a1,&A::a);
if (ret)
{
std::cerr<<"Error: "<<ret<<std::endl;
}

//wait a bit
::sleep(1);

return 0;
}

Michael
May 28 '07 #3
Michael DOUBEZ wrote:
Ian Collins a écrit :
>am***@parsonline.net wrote:
>>Guys,

As you know static member functions do not have a this pointer. so
instead, what shall i do?

for example ... consider I want to call

Code: ( text )

1.
pthread_create(&thID,NULL,classA::B, (void *) this);
An off topic but common question. You must pass an extern "C" linkage
function to C library functions that expect a function pointer as one of
their parameters.

You *must* not pass a class member function and *should* not pass a
static member function.

I didn't know about the static member function. Why is that (some
portability issue on their representation I presume)?
There is no guarantee that any C++ function will have the same linkage
as a C function. In practice most non-member functions do, but the
compiler is free to issue a diagnostic when one is passed to a function
expecting an extern "C" function pointer.
You can use template along the following pattern (using intermediary
class or not, using richer templatization ...):
This isn't strictly correct (but it will probably work) as function
templates can not have extern "C" linkage.

--
Ian Collins.
May 28 '07 #4
On May 28, 9:42 pm, Ian Collins <ian-n...@hotmail.comwrote:
Michael DOUBEZ wrote:
Ian Collins a écrit :
a...@parsonline.net wrote:
>As you know static member functions do not have a this pointer. so
instead, what shall i do?
>for example ... consider I want to call
>Code: ( text )
> 1.
pthread_create(&thID,NULL,classA::B, (void *) this);
An off topic but common question. You must pass an extern "C" linkage
function to C library functions that expect a function pointer as one of
their parameters.
You *must* not pass a class member function and *should* not pass a
static member function.
I didn't know about the static member function. Why is that (some
portability issue on their representation I presume)?
There is no guarantee that any C++ function will have the same linkage
as a C function. In practice most non-member functions do, but the
compiler is free to issue a diagnostic when one is passed to a function
expecting an extern "C" function pointer.
Not "free to", required to. Linkage is part of type, and
passing a function with "C++" linkage to pthread_create is just
as much an error as passing one which takes an int instead of a
pointer, or which returns void, instead of void*.

With regards to Michael's code, I don't think a template
instantiation can have C linkage, so the code is illegal.
You can use template along the following pattern (using intermediary
class or not, using richer templatization ...):
This isn't strictly correct (but it will probably work) as function
templates can not have extern "C" linkage.
In which case, a good compiler, in strictly conforming mode,
should refuse to compile the code. (Note that both g++ and VC++
get this wrong.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 29 '07 #5
James Kanze wrote:
On May 28, 9:42 pm, Ian Collins <ian-n...@hotmail.comwrote:
>Michael DOUBEZ wrote:
>>I didn't know about the static member function. Why is that (some
portability issue on their representation I presume)?
>There is no guarantee that any C++ function will have the same linkage
as a C function. In practice most non-member functions do, but the
compiler is free to issue a diagnostic when one is passed to a function
expecting an extern "C" function pointer.

Not "free to", required to. Linkage is part of type, and
passing a function with "C++" linkage to pthread_create is just
as much an error as passing one which takes an int instead of a
pointer, or which returns void, instead of void*.
Agreed, but many (most?) don't. Sun CC is the only compiler I know that
does object.

--
Ian Collins.
May 29 '07 #6
On Tue, 29 May 2007 20:22:31 +1200, Ian Collins wrote:
>James Kanze wrote:
>Not "free to", required to. Linkage is part of type, and
passing a function with "C++" linkage to pthread_create is just
as much an error as passing one which takes an int instead of a
pointer, or which returns void, instead of void*.
Agreed, but many (most?) don't. Sun CC is the only compiler I know that
does object.
Of course the EDG front-end in strict mode rejects it as well, though
not all EDG-based compilers do. The front-end offers the
--implicit_extern_c_type_conversion and
--no_implicit_extern_c_type_conversion options to control this, but it
is up to the compiler writer whether they are made available to the
user (of course, the conversion cannot work if the compiler uses
incompatible calling conventions for C and C++ functions).

--
Gennaro Prota -- C++ Developer, For Hire
https://sourceforge.net/projects/breeze/
(replace 'address' with 'name.surname' to mail)
May 30 '07 #7
On May 28, 4:07 pm, a...@parsonline.net wrote:
Guys,

As you know static member functions do not have a this pointer. so
instead, what shall i do?

for example ... consider I want to call

Code: ( text )

1.
pthread_create(&thID,NULL,classA::B, (void *) this);

and it is called in an static method so instead of this what can I
put??
If you can you're much better off using Boost::threads for this. The
following is off the top of my head:

#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>

class A {
public:
void a() { std::cout << "The A" << std::endl; }
} anA;

int main() {
boost::thread theThread( boost::bind( &A::a, anA ) );
theThread.join();
return 0;
}

Your example would look like more like this:

boost::thread t( boost::bind( &classA::B, this ) );
// Do something else
t.join(); // Wait for this->B() to finish
K

May 30 '07 #8
On May 30, 4:41 pm, Kirit Sælensminde <kirit.saelensmi...@gmail.com>
wrote:
On May 28, 4:07 pm, a...@parsonline.net wrote:
As you know static member functions do not have a this pointer. so
instead, what shall i do?
for example ... consider I want to call
Code: ( text )
1.
pthread_create(&thID,NULL,classA::B, (void *) this);
and it is called in an static method so instead of this what can I
put??
If you can you're much better off using Boost::threads for this.
It depends. The conception of Boost::threads is far from
perfect, and it's all to easy to accidentally get a detached
thread due to an unexpected exception. The advantages of
Boost::threads limit themselves to: 1) they've already wrapped
the system API, so you don't have to learn both Windows and
Posix, and worry about system dependant code yourself, and 2) in
cases where you want to view the new thread as a functional
object, with its own totally independant lifetime,
Boost::threads has already handled the copying and the
synchronization issues at thread start up for you. (2 is almost
always the case for detached threads, rarely however for
joinable threads.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 31 '07 #9

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

Similar topics

4
by: trustron | last post by:
Hi all, I have got a pointer question. I was told that a pointer is a variable holding a memory address. SO:
8
by: BigMan | last post by:
I wonder if the C++ standard says what should happen if I increment a pointer to a one-past-the-end value. I think it says that it is perfectly legal to increment a pointer to the last element...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
6
by: m sergei | last post by:
main(int argc, char *argv) the second parameter to function main takes a pointer to an array. can we also write it in terms of a reference rather than a pointer ? an example of using (usage)...
24
by: s.subbarayan | last post by:
Dear all, According to standards is this valid: char TmpPtrWriteBuffer; void* PtrWriteBuffer =(void*) TmpPtrWriteBuffer; I had a debate with my colleagues that anything cant be typecasted to...
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
2
by: Chad | last post by:
The following question stems from the following thread on comp.lang.c: http://groups.google.com/group/comp.lang.c/browse_thread/thread/0ad03c96df57381a/5f20260b30952fe7?hl=en#5f20260b30952fe7 I...
14
by: sheroork | last post by:
Can anyone help me in getting to understand pointer to pointer with examples? Appritiate in advance. Sagar
4
by: Jeffrey Spoon | last post by:
Hello, I am trying to make a simple function that returns a pointer to another function. In my header file I've declared my function pointer: void (*pStateFunction) (void); //assume the function...
15
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.