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

Starting a thread which runs a member function

Hello ,

I have the following code :

//...additional includes cut off
class MyThread{

public:
DWORD WINAPI ThreadProc( LPVOID lpParameter );
void run();
};

DWORD WINAPI MyThread::ThreadProc(LPVOID lpParameter){

cout<<"Thread called\n";
return 0;
}

void MyThread::run(){

CreateThread(NULL, 0, MyThread::ThreadProc, NULL, 0, NULL); //<---
LINE
}

void main(void){
MyThread p;
p.run();

}

When i compile this ( with MSVS 2005 ) i get the following compile
error:

LINE: error C3867: 'MyThread::ThreadProc': function call missing
argument list; use '&MyThread::ThreadProc' to create a pointer to
member.

Then I add &MyThread:ThreadProc instead of MyThread::ThreadProc and i
get another compile error:

LINE: error C2664: 'CreateThread' : cannot convert parameter 3 from
'DWORD (__stdcall MyThread::* )(LPVOID)' to 'LPTHREAD_START_ROUTINE'

Any logic explanation?

Thank-you ,

Daniel
Aug 15 '08 #1
2 4815
On Aug 15, 10:56*am, daniel <daniel.bal...@gmail.comwrote:
Hello ,

I have the following code :

//...additional includes cut off
class MyThread{

public:
* * * * DWORD WINAPI ThreadProc( *LPVOID lpParameter );
* * * * void run();

};

DWORD WINAPI MyThread::ThreadProc(LPVOID lpParameter){

* * * * cout<<"Thread called\n";
* * * * return 0;

}

void MyThread::run(){

* * * * CreateThread(NULL, 0, MyThread::ThreadProc, NULL, 0, NULL); //<---
LINE

}

void main(void){

* * * * MyThread p;
* * * * p.run();

}

When i compile this ( with MSVS 2005 ) i get the following compile
error:

LINE: error C3867: 'MyThread::ThreadProc': function call missing
argument list; use '&MyThread::ThreadProc' to create a pointer to
member.

Then I add &MyThread:ThreadProc instead of MyThread::ThreadProc and i
get another compile error:

LINE: error C2664: 'CreateThread' : cannot convert parameter 3 from
'DWORD (__stdcall MyThread::* )(LPVOID)' to 'LPTHREAD_START_ROUTINE'

Any logic explanation?

Thank-you ,

Daniel
It expects a free standing function not a member function. The bad
news is that you will have to provide that function. The good news is
that you can pass anything you want to that function so it's pretty
easy to pass an address of some object that you can use.

HTH
Aug 15 '08 #2
daniel <da***********@gmail.comkirjutas:
Hello ,

I have the following code :

//...additional includes cut off
class MyThread{

public:
DWORD WINAPI ThreadProc( LPVOID lpParameter );
void run();
};

DWORD WINAPI MyThread::ThreadProc(LPVOID lpParameter){

cout<<"Thread called\n";
return 0;
}

void MyThread::run(){

CreateThread(NULL, 0, MyThread::ThreadProc, NULL, 0, NULL); //<---
LINE
}

void main(void){
MyThread p;
p.run();

}

When i compile this ( with MSVS 2005 ) i get the following compile
error:

LINE: error C3867: 'MyThread::ThreadProc': function call missing
argument list; use '&MyThread::ThreadProc' to create a pointer to
member.

Then I add &MyThread:ThreadProc instead of MyThread::ThreadProc and i
get another compile error:

LINE: error C2664: 'CreateThread' : cannot convert parameter 3 from
'DWORD (__stdcall MyThread::* )(LPVOID)' to 'LPTHREAD_START_ROUTINE'
See FAQ:

http://parashift.com/c++-faq-lite/po....html#faq-33.2

And consider to use boost::thread instead.

hth
Paavo
Aug 15 '08 #3

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

Similar topics

1
by: stub | last post by:
Several objects of the same class, which contains a static member function, are created in several threads. If the said member function is called in the threads concurrently, it must be made to...
2
by: Rajesh Abraham | last post by:
I have a Windows Service and in the OnStart, I am initializing the BusinessLayer Object and calling a method of the object, which normally takes about 10 mts to finish execution. Now when I...
2
by: karl | last post by:
I have a windows service that kicks off a 'monitor' thread which in turn kicks off 4 additional threads. These 4 threads basically are listen on a designated socket and report back any errors...
11
by: PJ | last post by:
I'd like to create a subsystem in my asp.net application that is responsible for emails that need to be send out based upon certain events so that the main request/response threads aren't...
7
by: MariusI | last post by:
Are objects implicitly stored in the TLS of the currently running thread? When creating multithreaded applications i get errors when accessing data from a different thread than the thread used to...
7
by: Joseph Geretz | last post by:
I have a Service which runs OK, but I'm abviously not starting it properly. In my OnStart event I commence a long running process which polls a database table and performs various processing. Since...
23
by: Boltar | last post by:
Hi I'm writing a threading class using posix threads on unix with each thread being run by an object instance. One thing I'm not sure about is , if I do the following: myclass::~myclass() {...
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
18
by: J.K. Baltzersen | last post by:
To whomever it may concern: I am using MS Visual C++ 6.0. I have a process A which instantiates an object C. At a later point the process A creates the thread B. The thread B has access...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.