none
Can't make first example work from Joe Duffy's book 'Concurrent Programming on Windows'

    Question

  • 5 April 2009
    I have an HP Media Center PC with Vista Home Premium (x32).  It has a Core3 Quad CPU.  I'm going through Joe's book to learn about parallel processing,  threads etc.  I downloaded all the software Joe recommended, I hope.  This consisted of Visual C++ Express Edition with SP1,  MSDN Express Library,  Windows SDK, .NETFramework 3.5 SP1 and some debug programs.  I set up an empty Win32 Console project and copied in Listing 3.1 from page 91,  a two thread 'Hello World' example program.  Everything compiled except the first line:

    WIN32 - C++  CREATETHREAD.CPP

    The compiler produced the following output:

    1>------ Build started: Project: CPWMyThread3p1, Configuration: Debug Win32 ------

    1>Compiling...

    1>CPWMyThread3p1.cpp

    1>c:\users\richard\documents\visual studio 2008\projects\cpwmythread3p1\cpwmythread3p1\cpwmythread3p1.cpp(5) : error C2059: syntax error : 'constant'

    1>Build log was saved at "file://c:\Users\Richard\Documents\Visual Studio 2008\Projects\CPWMyThread3p1\CPWMyThread3p1\Debug\BuildLog.htm"

    1>CPWMyThread3p1 - 1 error(s), 0 warning(s)

    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



    Sunday, April 05, 2009 7:13 PM

Answers

  • Hi RichieV,

    The first line is actually not code.  It's just a textual header ("Win32 - C++ - CREATETHREAD.CPP").  If you leave that out, the example should compile.  And I will ensure that in subsequent printings that line is bolded in some way to make it clear that it's not code.  Apologies for the confusion.

    ---joe
    Dev Lead, Parallel Extensions, Microsoft
    Monday, April 06, 2009 4:16 AM
  • RichieV,
    Please try out this way and you should be able to compile and execute the code.


    #include

    <stdio.h>

    #include

    <windows.h>

     

     

    //DWORD WINAPI MyThreadStart(LPVOID);

    DWORD WINAPI MyThreadStart(LPVOID lpParameter)

    {

    printf("%d: Running:%s\r\n",

    GetCurrentThreadId(), reinterpret_cast<char *>(lpParameter));

     

    return 0;

    }

    int

    main(int argc, wchar_t * argv[])

    {

    HANDLE hThread;

    DWORD dwThreadId;

     

    hThread = CreateThread(NULL, 0, &MyThreadStart, "Hello World", 0, &dwThreadId);

     

    if (!hThread)

    {

    fprintf(stderr, "Thread creation failed: %d\r\n",

    GetLastError());

    return -1;

    }

    printf("%d: Created threadn%x (ID %d)\r\n",

    GetCurrentThreadId(), hThread, dwThreadId);

    // Wait for it to exit and then print the exit code.

    WaitForSingleObject(hThread, INFINITE);

    DWORD dwExitCode;

    GetExitCodeThread(hThread, &dwExitCode);

    printf("%d: Thread Exited: %d\r\n",

    GetCurrentThreadId(), dwExitCode);

    CloseHandle(hThread);

    return 0;

    }


    Jagannath
    Tuesday, April 28, 2009 11:47 AM

All replies

  • Hi RichieV,

    The first line is actually not code.  It's just a textual header ("Win32 - C++ - CREATETHREAD.CPP").  If you leave that out, the example should compile.  And I will ensure that in subsequent printings that line is bolded in some way to make it clear that it's not code.  Apologies for the confusion.

    ---joe
    Dev Lead, Parallel Extensions, Microsoft
    Monday, April 06, 2009 4:16 AM
  •  

     

    Thanks Joe
        You are a treasure to take the time to answer these newbie questions.  I commented out the offending line and got the following link errors.  So the program compiles but doesn't link.  I looked at various forums on the two error codes (LNK2019 and LNK1120) and the subject is confusing to me and a lot of other people.  I guess I'm missing one or more libraries but I don't know which ones nor how to link them if I did know what they are.  Maybe I'm starting the project wrong.  I'll go back and start over again with various configurations.  If you or anyone have some ideas on how to get going I would appreciate them. Maybe a book on understanding linking in the Visual Studios environment.

    1. 1>------ Build started: Project: CPWMyThread3p1, Configuration: Debug Win32 ------
    2. 1>Linking...
    3. 1>CPWMyThread3p1.obj : error LNK2019: unresolved external symbol "unsigned long __stdcall MyThreadStart(void *)" (?MyThreadStart@@YGKPAX@Z) referenced in function _main
    4. 1>C:\Users\Richard\Documents\Visual Studio 2008\Projects\CPWMyThread3p1\Debug\CPWMyThread3p1.exe : fatal error LNK1120: 1 unresolved externals
    5. 1>Build log was saved at "file://c:\Users\Richard\Documents\Visual Studio 2008\Projects\CPWMyThread3p1\CPWMyThread3p1\Debug\BuildLog.htm"
    6. 1>CPWMyThread3p1 - 2 error(s), 0 warning(s)
    7. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========
    Here is the code from Joe's book:<br/>
    
    
    #include <stdio.h>
    
    #include <windows.h>
    
    
    
    DWORD WINAPI MyThreadStart(LPVOID);
    
    
    
    int main(int argc, wchar_t * argv[])
    
    {
    
    	HANDLE hThread;
    
    	DWORD dwThreadId;
    
    
    
    	// Create the new thread.
    
    	hThread = CreateThread(NULL,           // lpThreadAttributes
    
    		              0,              // dwStachSize
    
    			     &MyThreadStart, // lpStartAddress
    
    			     "Hello World",  // lpParameter
    
    			     0,              // dwCreationFlags
    
    			     &dwThreadId);   // lpThreadd
    
    	if (!hThread)
    
    	{
    
    		fprintf(stderr, "Thread creation failed: %d\r\n",
    
    			GetLastError());
    
    		return -1;
    
    	}
    
    
    
    	printf("%d: Created threadn%x (ID %d)\r\n",
    
    		GetCurrentThreadId(), hThread, dwThreadId);
    
    
    
    	// Wait for it to exit and then print the exit code.
    
    	WaitForSingleObject(hThread, INFINITE);
    
    
    
    	DWORD dwExitCode;
    
    	GetExitCodeThread(hThread, &dwExitCode);
    
    	printf("%d: Thread Exited: %d\r\n",
    
    		GetCurrentThreadId(), dwExitCode);
    
    	CloseHandle(hThread);
    
    
    
    	return 0;
    
    }
    
    
    
    DWORD WINAPI MtThreadStart(LPVOID lpParameter)
    
    {
    
    	printf("%d: Running:%s\r\n",
    
    		GetCurrentThreadId(), reinterpret_cast<char *>(lpParameter));
    
    	return 0;
    
    }
    
    
    
    


    RichieV


    From RichieV

    Well I tried 7 different ways to set up a project for the above program and all but 3 produced the same link errors.  The other 3 produced files with _tmain code so I didn't know where I would put Joe's program.
    • Proposed as answer by Sai Jagannath Tuesday, April 28, 2009 11:46 AM
    Saturday, April 11, 2009 2:10 PM
  • RichieV,
    Please try out this way and you should be able to compile and execute the code.


    #include

    <stdio.h>

    #include

    <windows.h>

     

     

    //DWORD WINAPI MyThreadStart(LPVOID);

    DWORD WINAPI MyThreadStart(LPVOID lpParameter)

    {

    printf("%d: Running:%s\r\n",

    GetCurrentThreadId(), reinterpret_cast<char *>(lpParameter));

     

    return 0;

    }

    int

    main(int argc, wchar_t * argv[])

    {

    HANDLE hThread;

    DWORD dwThreadId;

     

    hThread = CreateThread(NULL, 0, &MyThreadStart, "Hello World", 0, &dwThreadId);

     

    if (!hThread)

    {

    fprintf(stderr, "Thread creation failed: %d\r\n",

    GetLastError());

    return -1;

    }

    printf("%d: Created threadn%x (ID %d)\r\n",

    GetCurrentThreadId(), hThread, dwThreadId);

    // Wait for it to exit and then print the exit code.

    WaitForSingleObject(hThread, INFINITE);

    DWORD dwExitCode;

    GetExitCodeThread(hThread, &dwExitCode);

    printf("%d: Thread Exited: %d\r\n",

    GetCurrentThreadId(), dwExitCode);

    CloseHandle(hThread);

    return 0;

    }


    Jagannath
    Tuesday, April 28, 2009 11:47 AM