473,397 Members | 2,077 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,397 software developers and data experts.

multithreading and member-functions

Can I run a thread with _beginthread() and pass as the first parameter the
pointer on a member function of a class?

#include <process.h>

class A
{
void _cdecl f(void*) {};
};

void main()
{
_beginthread(&A::f, 0, NULL);
}

I get the compilation error:
error C2664: '_beginthread' : Konvertierung des Parameters 1 von 'void
(__cdecl A::* )(void *)' in 'void (__cdecl *)(void *)' nicht moglich

PS. I work in MSVC7.0 (german version :), console application
Jul 19 '05 #1
16 5751

"Ilia Poliakov" <ip*****@allesklar.de> wrote in message
news:bj************@ID-120622.news.uni-berlin.de...
Can I run a thread with _beginthread() and pass as the first parameter the
pointer on a member function of a class?

#include <process.h>

class A
{
void _cdecl f(void*) {};
};

void main()
{
_beginthread(&A::f, 0, NULL);
}

I get the compilation error:
error C2664: '_beginthread' : Konvertierung des Parameters 1 von 'void
(__cdecl A::* )(void *)' in 'void (__cdecl *)(void *)' nicht moglich

PS. I work in MSVC7.0 (german version :), console application


1. It would be better if you ask this on a Windows NG.
2. Please translate the errors to English whenever posting.
Then may be someone here can help you.
--
With best wishes,
J.Schafer
Jul 19 '05 #2
1. It would be better if you ask this on a Windows NG.
_beginthread() is a C run-time function and has no relationship to any OS.
2. Please translate the errors to English whenever posting.
Then may be someone here can help you.


error C2664: '_beginthread' : convertion of parameter 1 from 'void (__cdecl
A::* )(void *)' in 'void (__cdecl *)(void *)' not possible

Jul 19 '05 #3
Ilia Poliakov wrote:
Can I run a thread with _beginthread() and pass as the first
parameter the pointer on a member function of a class?


Nope. AFAIK _beginthread is a C function, taking a void * argument.
Pointers and pointers to members are very different beasts. A pointer to a
member (function or variable) can have a very different implementation than
a normal pointer. The standard (let me not look it up) clearly states that
such conversion (from and to) is not supported. IMHO the best you can do is
to make a little class containing that pointer to member, create it (either
static/global, dynamicly allocated or some other way that ensures that it is
still there when the new thread tries to access it) and pass a pointer to
this very "argument list" to the new thread. Of course if you need more
arguments you can add them into this class (or struct).

--
Attila aka WW
Jul 19 '05 #4

"Ilia Poliakov" <ip*****@allesklar.de> wrote in message
news:bj************@ID-120622.news.uni-berlin.de...
1. It would be better if you ask this on a Windows NG.
_beginthread() is a C run-time function and has no relationship to any OS.


Well then it's a C++ NG.
2. Please translate the errors to English whenever posting.
Then may be someone here can help you.


error C2664: '_beginthread' : convertion of parameter 1 from 'void

(__cdecl A::* )(void *)' in 'void (__cdecl *)(void *)' not possible


Your error message gives the answer.
You cannot convert a pointer to a member function to a normal pointer
to a function.
Jul 19 '05 #5
"Ilia Poliakov" <ip*****@allesklar.de> wrote in message
news:bj************@ID-120622.news.uni-berlin.de...
1. It would be better if you ask this on a Windows NG.


_beginthread() is a C run-time function and has no relationship to any OS.


That is not true. _beginthread is non-standard and specific to Microsoft
Windows.
Jul 19 '05 #6
Josephine Schafer wrote:
[SNIP]
1. It would be better if you ask this on a Windows NG.
The problem has nothing to do with Windows, all you need to know is that
thread starting function take a void * argument to represent "arguments" for
the thread. That is clearly visible from the error message - even though it
is in German.
2. Please translate the errors to English whenever posting.
Then may be someone here can help you.


I speak no German but Konvertierung is definitely Conversion. What it wants
to convert is void (A::*)(void *) (pointer to a member function of a class A
taking a void * argument and returns nothing) to void (*)(void *): pointer
to a function bla bla. Now it is clear that *if* this conversion is
possible you get no error message. It is also clear from the rules of C++
that this conversion is not possible So even if you did not know that
"nicht möglich" is not possible you could deduce what is the problem.

IMHO if you do not take the time to answer a post... well, just do not take
the time to answer it. No offense meant.

--
Attila aka WW
Jul 19 '05 #7
Ilia Poliakov wrote:
1. It would be better if you ask this on a Windows NG.


_beginthread() is a C run-time function and has no relationship to
any OS.


_beginthread is *not* a C runtime function. Please refer to the ISO
standard of the C language! Or to this place and look for the C runtime
library documentation: http://www.dinkumware.com

--
Attila aka WW
Jul 19 '05 #8

"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bj**********@newstree.wise.edt.ericsson.se...
Josephine Schafer wrote:
[SNIP]
1. It would be better if you ask this on a Windows NG.
The problem has nothing to do with Windows, all you need to know is that
thread starting function take a void * argument to represent "arguments"

for the thread. That is clearly visible from the error message - even though it is in German.
2. Please translate the errors to English whenever posting.
Then may be someone here can help you.
I speak no German but Konvertierung is definitely Conversion. What it

wants to convert is void (A::*)(void *) (pointer to a member function of a class A taking a void * argument and returns nothing) to void (*)(void *): pointer
to a function bla bla. Now it is clear that *if* this conversion is
possible you get no error message. It is also clear from the rules of C++
that this conversion is not possible So even if you did not know that
"nicht möglich" is not possible you could deduce what is the problem.

IMHO if you do not take the time to answer a post... well, just do not take the time to answer it. No offense meant.


OK may be I just looked at _beginthread() and started taking it OT.
Neither do I understand German nor did I made make much attempts to
understand it.
But when the OP translated the error message, I answered it and yes it had
nothing to do with
Windows or anything as you said.

Hope you too understand.
J.Schafer
Jul 19 '05 #9
Josephine Schafer wrote:
"Ilia Poliakov" <ip*****@allesklar.de> wrote in message
news:bj************@ID-120622.news.uni-berlin.de...
1. It would be better if you ask this on a Windows NG.
_beginthread() is a C run-time function and has no relationship to
any OS.


Well then it's a C++ NG.


The C libray is part of C++. But of course the _beginthread function is not
part of either. :-)
Your error message gives the answer.
You cannot convert a pointer to a member function to a normal pointer
to a function.


I guess why the OP posted is to know what he _can_ do, not only to know that
he cannot do it as he tried. ;-)

--
Attila aka WW
Jul 19 '05 #10

Ilia Poliakov wrote:

Can I run a thread with _beginthread() and ...


Stay away from _beginthread() silliness ("ex"-stuff including).

As for the "rest",

http://groups.google.com/groups?selm...00AAA%40web.de
(Subject: Re: C++ and threads: Messy code?)

regards,
alexander.

P.S. Stay away from the current Boost.Thread as well.
Jul 19 '05 #11
"Ilia Poliakov" <ip*****@allesklar.de> wrote in message news:<bj************@ID-120622.news.uni-berlin.de>...
Can I run a thread with _beginthread() and pass as the first parameter the
pointer on a member function of a class?

#include <process.h>

class A
{
void _cdecl f(void*) {};
};

void main()
{
_beginthread(&A::f, 0, NULL);
}

I get the compilation error:
error C2664: '_beginthread' : Konvertierung des Parameters 1 von 'void
(__cdecl A::* )(void *)' in 'void (__cdecl *)(void *)' nicht moglich

PS. I work in MSVC7.0 (german version :), console application


As has been pointed out by others, a pointer to a member function is
not the same as a pointer to a function.

So, what you want is a function that is compatible with begin thread
and will forward your function correctly.
extern "C" void _cdecl forward_function(void *arg)
{
A *a = reinterpret_cast<A *>(arg);

arg->f();
}

samuel
}
Jul 19 '05 #12
On Wed, 3 Sep 2003 13:55:23 +0300, "Attila Feher"
<at**********@lmf.ericsson.se> wrote in comp.lang.c++:
Ilia Poliakov wrote:
Can I run a thread with _beginthread() and pass as the first
parameter the pointer on a member function of a class?


Nope. AFAIK _beginthread is a C function, taking a void * argument.


Nope, _beginthread is not a C function. It is not defined anywhere in
the C standard. Nor am I aware of any Microsoft documentation that
states that it is written in (non-standard) C.

What you could say is that _beginthread is a function that may be
called by C.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #13
"Ilia Poliakov" <ip*****@allesklar.de> wrote in message news:<bj************@ID-120622.news.uni-berlin.de>...
Can I run a thread with _beginthread() and pass as the first parameter the
pointer on a member function of a class?

#include <process.h>

class A
{
void _cdecl f(void*) {};
};

void main()
{
_beginthread(&A::f, 0, NULL);
}

I get the compilation error:
error C2664: '_beginthread' : Konvertierung des Parameters 1 von 'void
(__cdecl A::* )(void *)' in 'void (__cdecl *)(void *)' nicht moglich

PS. I work in MSVC7.0 (german version :), console application


You can use the static member function

I got it compiled with the following code with proper header included

class proc
{
public:
static void SecondThreadFunc( void* pArguments )
{
printf("In thread");
}
};

void main()
{
_beginthread(proc::SecondThreadFunc, 0, NULL );
Sleep(1000);
}

Hope that helps
Jul 19 '05 #14
Jack Klein wrote:
On Wed, 3 Sep 2003 13:55:23 +0300, "Attila Feher"
<at**********@lmf.ericsson.se> wrote in comp.lang.c++:
Ilia Poliakov wrote:
Can I run a thread with _beginthread() and pass as the first
parameter the pointer on a member function of a class?
Nope. AFAIK _beginthread is a C function, taking a void * argument.


Nope, _beginthread is not a C function. It is not defined anywhere in
the C standard. Nor am I aware of any Microsoft documentation that
states that it is written in (non-standard) C.


It is a C function. Repeat it to yourself until you understand: it *is* a C
function. And then if you still not understand say that: on the other hand
strcpy is a *standard C library* function.
What you could say is that _beginthread is a function that may be
called by C.


Which we call a C function as opposed to C++, Fortran, Fifthrun etc.
functions. If you did care to read my other posts you would have seen that
I did post (to the OP) that _beginthread is not a standard C library
function.

--
Attila aka WW
Jul 19 '05 #15
Ron Samuel Klatchko wrote:
So, what you want is a function that is compatible with begin thread
and will forward your function correctly.
And if you want that (forward correctly) you do nto want to use the posted
code.
extern "C" void _cdecl forward_function(void *arg)
{
A *a = reinterpret_cast<A *>(arg);


Reinterpret case is _implementation_ defined. In *all* cases. If you want
to get back your original A pointer here you *must* use static_cast (or a C
style cast).

--
Attila aka WW
Jul 19 '05 #16
Attila Feher wrote:
Reinterpret case is


Of course I meant reinterpret cast

--
Attila aka WW
Jul 19 '05 #17

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

Similar topics

1
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able...
3
by: TazCoder | last post by:
I have a problem with multithreading. The error I keep getting is that findFileButton_Click(...) is not a member function. But when I take the parameters out of the findFileButton_Click(...)...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
0
by: Mike | last post by:
Hello, I've done some work with dotnet multithreading and synchronizing Collections such as Queue. So I know my shared data and other messages can be protected between threads. Now I am...
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
9
by: tommy | last post by:
hi, i have found a example for multithreading and asp.net http://www.fawcette.com/vsm/2002_11/magazine/features/chester/ i want to speed up my website ... if my website is starting, they...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
5
by: sandy82 | last post by:
Whats actuallly multithreading is ... and how threading and multithreading differ . Can any1 guide how multithreading is used on the Web .. i mean a practical scenario in which u use...
4
by: Michael | last post by:
Hi, I am trying to create a multithreaded VB 2005 application which attempts to create a new thread per Domain Controller (DC) in my environment. Each thread connects to its allocated DC and...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.