VC++ Basic Threading - help
-
I am creating a vs2005 vc++ application and trying to get a secondary thread setup and working. I have read through the various postings and tried to put some code together but it won't compile. I am trying to start the new thread from a menu item via a message handler based in my application's 'CView' based class. I have created a new class of my own (GcWorkerThread) and is derived from CWinThread, and contains a member function 'GcThreadFunction' calling code:
int x;
CWinThread *pThread = AfxBeginThread (GcThreadFunction, x);UINT GcWorkerThread::GcThreadFunction (LPVOID pParam)
{
int t;
for (int y=0; y<1000000; y++
{
}}
When is comes to compiling the code I get an error: "Error C3867 'GcWorkerThread::GcThreadFunction' function call missing argument list use &GcWorkerThread::GcThreadFunction to create a pointer to member" What am I doing wrong? :(
-
I am creating a vs2005 vc++ application and trying to get a secondary thread setup and working. I have read through the various postings and tried to put some code together but it won't compile. I am trying to start the new thread from a menu item via a message handler based in my application's 'CView' based class. I have created a new class of my own (GcWorkerThread) and is derived from CWinThread, and contains a member function 'GcThreadFunction' calling code:
int x;
CWinThread *pThread = AfxBeginThread (GcThreadFunction, x);UINT GcWorkerThread::GcThreadFunction (LPVOID pParam)
{
int t;
for (int y=0; y<1000000; y++
{
}}
When is comes to compiling the code I get an error: "Error C3867 'GcWorkerThread::GcThreadFunction' function call missing argument list use &GcWorkerThread::GcThreadFunction to create a pointer to member" What am I doing wrong? :(
You cannot pass a instance method to
AfxBeginThread
. You've to pass a standard function or a class (i.e.static
) method. BTW: have a look at this [^]. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I am creating a vs2005 vc++ application and trying to get a secondary thread setup and working. I have read through the various postings and tried to put some code together but it won't compile. I am trying to start the new thread from a menu item via a message handler based in my application's 'CView' based class. I have created a new class of my own (GcWorkerThread) and is derived from CWinThread, and contains a member function 'GcThreadFunction' calling code:
int x;
CWinThread *pThread = AfxBeginThread (GcThreadFunction, x);UINT GcWorkerThread::GcThreadFunction (LPVOID pParam)
{
int t;
for (int y=0; y<1000000; y++
{
}}
When is comes to compiling the code I get an error: "Error C3867 'GcWorkerThread::GcThreadFunction' function call missing argument list use &GcWorkerThread::GcThreadFunction to create a pointer to member" What am I doing wrong? :(
try with the following change.
int x;
CWinThread *pThread = AfxBeginThread (GcThreadFunction, &x);Thanks and Regards, Selvam, http://www.wincpp.com
-
I am creating a vs2005 vc++ application and trying to get a secondary thread setup and working. I have read through the various postings and tried to put some code together but it won't compile. I am trying to start the new thread from a menu item via a message handler based in my application's 'CView' based class. I have created a new class of my own (GcWorkerThread) and is derived from CWinThread, and contains a member function 'GcThreadFunction' calling code:
int x;
CWinThread *pThread = AfxBeginThread (GcThreadFunction, x);UINT GcWorkerThread::GcThreadFunction (LPVOID pParam)
{
int t;
for (int y=0; y<1000000; y++
{
}}
When is comes to compiling the code I get an error: "Error C3867 'GcWorkerThread::GcThreadFunction' function call missing argument list use &GcWorkerThread::GcThreadFunction to create a pointer to member" What am I doing wrong? :(
GC104 wrote:
I have read through the various postings and tried to put some code together but it won't compile.
Instead of reading through the various postings I can suggest reading this[^] useful article about worker threads. [edit] Sorry I didn't see that CPallini have already gave this link :sigh: [\edit] Regards Nuri Ismail
-
I am creating a vs2005 vc++ application and trying to get a secondary thread setup and working. I have read through the various postings and tried to put some code together but it won't compile. I am trying to start the new thread from a menu item via a message handler based in my application's 'CView' based class. I have created a new class of my own (GcWorkerThread) and is derived from CWinThread, and contains a member function 'GcThreadFunction' calling code:
int x;
CWinThread *pThread = AfxBeginThread (GcThreadFunction, x);UINT GcWorkerThread::GcThreadFunction (LPVOID pParam)
{
int t;
for (int y=0; y<1000000; y++
{
}}
When is comes to compiling the code I get an error: "Error C3867 'GcWorkerThread::GcThreadFunction' function call missing argument list use &GcWorkerThread::GcThreadFunction to create a pointer to member" What am I doing wrong? :(
Info gratefully received, I have gone away and think I now have a better understanding of what a 'static member function' is all about. However I'm struggling with the example given on the link given. see below:
static UINT run(LPVOID p);
void run();
volatile BOOL running;
To start a thread, your handler doesvoid CMyView::doInvert()
{
running = TRUE;
AfxBeginThread(run, this); // why is a 'this' pointer passed as the pParam?
// run is refering to member function 'UINT CMyView::run(LPVOID p)'?
}UINT CMyView::run(LPVOID p)
{
CMyView * me = (CMyView *)p; //why is 'p' cast from something that already seems to be
//a CMyView pointer already?
me -> run();
return 0;
}void CMyView::run() //run is an overloaded function?
{
for(int x=y = 0; running && y < image.height; y++)
for(int x = 0; running && x < image.width; x++)
changePixel(x, y);
running = FALSE;
} -
Info gratefully received, I have gone away and think I now have a better understanding of what a 'static member function' is all about. However I'm struggling with the example given on the link given. see below:
static UINT run(LPVOID p);
void run();
volatile BOOL running;
To start a thread, your handler doesvoid CMyView::doInvert()
{
running = TRUE;
AfxBeginThread(run, this); // why is a 'this' pointer passed as the pParam?
// run is refering to member function 'UINT CMyView::run(LPVOID p)'?
}UINT CMyView::run(LPVOID p)
{
CMyView * me = (CMyView *)p; //why is 'p' cast from something that already seems to be
//a CMyView pointer already?
me -> run();
return 0;
}void CMyView::run() //run is an overloaded function?
{
for(int x=y = 0; running && y < image.height; y++)
for(int x = 0; running && x < image.width; x++)
changePixel(x, y);
running = FALSE;
}// why is a 'this' pointer passed as the pParam? Because of what static functions implies: a static function is shared among all instances of a class. This means that within a static function, you cannot access non-static members of the class. In order to be able to do so, you need to identify which instance you want to manipulate. This is the reason why you pass the instance as a parameter to the thread function.In fact, a static function is similar to a global function in this context. //why is 'p' cast from something that already seems to be //a CMyView pointer already? Because it is received as a LPVOID, which doesn't mean anything. So, to be able to call the Run function on the instance, the compiler needs to know that it is a CMyView object (it can't guess it).
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
Info gratefully received, I have gone away and think I now have a better understanding of what a 'static member function' is all about. However I'm struggling with the example given on the link given. see below:
static UINT run(LPVOID p);
void run();
volatile BOOL running;
To start a thread, your handler doesvoid CMyView::doInvert()
{
running = TRUE;
AfxBeginThread(run, this); // why is a 'this' pointer passed as the pParam?
// run is refering to member function 'UINT CMyView::run(LPVOID p)'?
}UINT CMyView::run(LPVOID p)
{
CMyView * me = (CMyView *)p; //why is 'p' cast from something that already seems to be
//a CMyView pointer already?
me -> run();
return 0;
}void CMyView::run() //run is an overloaded function?
{
for(int x=y = 0; running && y < image.height; y++)
for(int x = 0; running && x < image.width; x++)
changePixel(x, y);
running = FALSE;
}Just adding to the previous reply
GC104 wrote:
// why is a 'this' pointer passed as the pParam?
Because inside the worker thread, a CMyView object is being manipulated (in this case). This particular thread function needs a CMyView object be passed to it. Note: However, it would be a lot better if threads can accept any object (or data type) passed as the parameter in general. Because different threads do different things. Some threads won't require anything at all. Therefore, the best approach would be to pass a pointer to "something". And this something is known to the calling code (it sets up the parameter) and the thread also know what exactly is the pointer pointing to and so you cast it to the appropriate type.
GC104 wrote:
//why is 'p' cast from something that already seems to be
p
is a pointer tovoid
(remember thatLPVOID
is nothing butvoid *
). Therefore, you must cast it to an appropriate type (read my note above?) Hope that helps. :)It is a crappy thing, but it's life -^ Carlo Pallini
-
Just adding to the previous reply
GC104 wrote:
// why is a 'this' pointer passed as the pParam?
Because inside the worker thread, a CMyView object is being manipulated (in this case). This particular thread function needs a CMyView object be passed to it. Note: However, it would be a lot better if threads can accept any object (or data type) passed as the parameter in general. Because different threads do different things. Some threads won't require anything at all. Therefore, the best approach would be to pass a pointer to "something". And this something is known to the calling code (it sets up the parameter) and the thread also know what exactly is the pointer pointing to and so you cast it to the appropriate type.
GC104 wrote:
//why is 'p' cast from something that already seems to be
p
is a pointer tovoid
(remember thatLPVOID
is nothing butvoid *
). Therefore, you must cast it to an appropriate type (read my note above?) Hope that helps. :)It is a crappy thing, but it's life -^ Carlo Pallini