Creating threads from a class

This is a discussion on Creating threads from a class within the C++ Programming forums, part of the General Programming Boards category; Hello, I have that problem: I try to create a new thread (on windows) with the api function 'CreateThread' from ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287

    Creating threads from a class

    Hello, I have that problem: I try to create a new thread (on windows) with the api function 'CreateThread' from a function in a class; the thread procedure is a function private of the class, also the hanlde return, and the parameters passed. that's how I'm trying to do that (I have created a sample code to help my poor explanations ), that's it: first the main *.cpp file

    Code:
    #include <conio.h>
    #include <stdio.h>
    
    #include "asample.h"
    CSample *sample;
    
    int main()
    {
    sample=new CSample();
    sample->start();
    delete sample;
    getch();
    return 0;
    }
    now the 'asample.h' class header:

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    class CSample
    {
    public:
        CSample();
        virtual~CSample();
        void start();
    private:
        DWORD WINAPI crThr(LPVOID );
        
        HANDLE ht;
        DWORD ti;
    };
    and finally the 'asample.cpp' code:

    Code:
    #include "asample.h"
    
    CSample::CSample()
    {
    ht=NULL;
    }
    CSample::~CSample()
    {
    }
    void CSample::start()
    {
    th=CreateThread(NULL,0,crThr,0,0,&ti);
    CloseHandle(ht);
    }
    
    DWORD WINAPI CSample::crThr(LPVOID lpParam)
    {
    printf("OK!\n");
    }
    the compiler says
    Code:
    In method `void CSample::start()':
    12 no matches converting function `crThr' to type `DWORD (*)(void *)'
    11 candidates are: DWORD CSample::crThr(void *)
     [Build Error]  [asample.o] Error 1
    I have tryed to rename the function to:
    'DWORD CSample::WINAPI crThr(LPVOID lpParam)'
    but not solves the prob; also have done a call like:
    'ht=(HANDLE)CreateThread(NULL,0,crThr,0,0,&ti);'
    or
    'ht=(DWORD WINAPI)CreateThread(NULL,0,crThr,0,0,&ti);'
    and finally I have no idea on what more to do.

    How can I salve that problem?

    Thank's in advance
    niara

    PS I've decided to post here instead in windows board because I think that's a class problem, no a winapi prob.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Code:
    #include <windows.h>
    #include <cstdio> 
    
    class CSample
    {
    public:
        CSample();
        virtual~CSample();
        void start();
    private:
        static DWORD WINAPI crThr(LPVOID );
        
        HANDLE ht;
        DWORD ti;
    };
    Ought to do it. They're the wrong types, the class function has this the api function doesn't(check its type definition in whatever windows header it's in, windef.h, maybe). If you search the windows board you'll probably find many examples of this.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    hello Ken Fitlike, thanks for your help and time. now works great. I'll take a look on the windows board for more info.

    niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  5. Creating a string class
    By incognito in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2002, 04:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21