Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Thread in C++ (http://www.programmingforums.org/showthread.php?t=8854)

myName Mar 14th, 2006 2:59 AM

Thread in C++
 
:

void Processfile()
{
        char FileName[] = "C:\\abc.txt";

        while( true)
        {
                if (fexists(FileName))
                {
                        printf("File Exist\n\n");

                        ifstream infile("C:\\abc.txt");
                        string sLine = "";
                        while( getline(infile, sLine) )
                        {
                                if( strlen(sLine.c_str()))
                                {
                                        printf( "%s\n", sLine.c_str());
                                }
                        }

                        DeleteFile();
                }
                else
                {
                        printf( "Not Exist\n\n" );
                }

        }
}


This function, i need to create it as a thread...

I do this
:

            DWORD  d_threadID = 0;
        CreateThread(NULL,
                0,
                Processfile,
                NULL,
                0,
                (LPDWORD)&d_threadID);


Error
Quote:

error C2664: 'CreateThread' : cannot convert parameter 3 from 'void (void)' to 'unsigned long (__stdcall *)(void *)'
None of the functions with this name in scope match the target type
:confused: :( :o

grumpy Mar 14th, 2006 3:26 AM

The error message is fairly self explanatory. The third argument of CreateThread is a pointer to a function that accepts one argument of type void * and returns an unsigned long. The compiler is complaining because you have attempted to pass it a function that accepts no arguments and returns void.

myName Mar 14th, 2006 4:03 AM

still can not understand..... please guide...

Cache Mar 14th, 2006 4:13 AM

I guess this is Win32 API?

You need to use a specific prototype/calling convention for your thread procedure.

:

DWORD WINAPI ThreadProc( LPVOID pVoid )

myName Mar 14th, 2006 5:02 AM

ok.. i add (LPTHREAD_START_ROUTINE) for the 3th parameter...

:

              DWORD  d_threadID = 0;
        CreateThread(NULL,
                0,
                (LPTHREAD_START_ROUTINE)Processfile,
                NULL,
                0,
                (LPDWORD)&d_threadID);


Now there is no error..
BUTTTT... :o :confused:
Why the process file did not run?
What's wrong????
Anybody see what wrong? please inform.. your help will be very much appreciate...

:

void Processfile()
{
        char FileName[] = "C:\\abc.txt";

        while( true)
        {
                if (fexists(FileName))
                {
                        printf("File Exist\n\n");

                        ifstream infile("C:\\abc.txt");
                        string sLine = "";
                        while( getline(infile, sLine) )
                        {
                                if( strlen(sLine.c_str()))
                                {
                                        printf( "%s\n", sLine.c_str());
                                }
                        }

                        DeleteFile();
                }
                else
                {
                        printf( "Not Exist\n\n" );
                }
                Sleep(200);

          }
}


grumpy Mar 14th, 2006 6:32 AM

Sigh. As I said earlier, your function ProcessFile() takes no arguments and returns void. It needs to accept a void pointer and return an unsigned long.
:

unsigned long ProcessFile(void *)
{
    // the body of your function
}


Using a cast, as in
:

CreateThread(NULL,
                0,
                (LPTHREAD_START_ROUTINE)Processfile,
                NULL,
                0,
                (LPDWORD)&d_threadID);

tells the compiler not to complain when you pass your version of ProcessFile() as the third argument to CreateThread(). It does not magically allow CreateThread() to work correctly when it (or the thread it creates) attempts to execute your function by passing an argument to it and checking the return value. In fact, using the cast in this instance (passing a function to CreateThread() that is of a different from from what CreateThread() expects) is a good way to ensure your program does not work correctly.

In this case it would also be a good idea to eliminate the (LPDWORD) cast as well from your call. Telling the compiler that &d_threadID is a pointer to a DWORD does not magically turn d_threadID into a DWORD.


All times are GMT -5. The time now is 5:12 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC