Thread: Compile Errors for CreateThread..

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    9

    Compile Errors for CreateThread..

    I have the following but getting compile errors...

    Code:
    void xboard(pcon)
    {
    for (;;) {
    if (side == computer)
    {
    DWORD Id;
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)think, pcon, 0, &Id);
    ...
    }
    ...
    ...
    }
    
    void think(PVOID pArg)
    {
    PCON pcon = (PCON)pArg;
    .....
    .....
    }
    Inside my header file where I define all my functions, I have,
    Code:
    void think(PVOID pArg);
    The errors I am getting are...

    c:\documents and settings\gopi\desktop\chesscalc\protos.h(14) : error C2146: syntax error : missing ')' before identifier 'pArg'
    c:\documents and settings\gopi\desktop\chesscalc\protos.h(14) : error C2061: syntax error : identifier 'pArg'
    c:\documents and settings\gopi\desktop\chesscalc\protos.h(14) : error C2059: syntax error : ';'
    c:\documents and settings\gopi\desktop\chesscalc\protos.h(14) : error C2059: syntax error : ')'
    c:\documents and settings\gopi\desktop\chesscalc\chesscalc.c(53) : error C2065: 'think' : undeclared identifier
    c:\documents and settings\gopi\desktop\chesscalc\search.c(133) : error C2198: 'ExitThread' : too few actual parameters

    Ofcourse I am having #include <Windows.h> in files having xboard() and think() functions..
    So please help me. Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    If I get those errors I would try to do that

    Code:
    void xboard(PCON pcon)
    Niara

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    9
    I am really sorry its my typo mistake. Its already, void xboard(PCON pcon)

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Cant help when you exclude all the code, you are basically missing a ) and perhaps a ; or two.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    9
    Hmm.. when compiling each and every file which is having #include "protos.h" its repeating those first 4 lines of errors I have posted above. Inside protos.h, I am having void think(PVOID pArg);

    But I dont seem to have missed any ) or ; in it.
    Anything wrong in it ?

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    lpStartAddress

    The starting address of the new thread. This is typically the address of a function declared with the WINAPI calling convention that accepts a single 32-bit pointer as an argument and returns a 32-bit exit code. Its prototype is:

    DWORD WINAPI ThreadFunc( LPVOID );
    Notice the prototype?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    9
    I changed it to as follows,
    DWORD WINAPI think( LPVOID pArg )

    But still getting an error as follows and other error I have posted above are also still there,
    c:\documents and settings\gopi\desktop\chesscalc\protos.h(14) : error C2061: syntax error : identifier 'WINAPI'

    Any suggestions please?

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    It would be easier to tell if you would post your complete code, or if that is fairly long, a short example that demonstrates the problem.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code would be helpful.
    Try without WINAPI.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    9
    Well, I got this answer for my explanation....

    I asked:
    Could anyone briefly tell how to create a multithreading in windows C for the below problem...

    Code:
    typedef struct tagCON {
    BOOL fTimeout;
    SEARCH_STATUS ss;
    } CON, *PCON
    
    int main()
    {
    char line[256], command[256];
    static CON s_con;
    PCON pcon = &s_con;
    
    for(;;)
    {
    if(!fgets(line, 256, stdin))
    return;
    sscanf(line, "%s", command);
    
    if(!strcmp(command, "go") {
    think(pcon);
    continue;
    }
    if(!strcmp(command, "quit") {
    pcon->fTimeout = TRUE;
    continue;
    }
    So I am calling think() from main and also passing a struct object. think() is basically executing for more than 10 mins...
    What I want is, whenever user enters "quit", fTimeout is set to true, and inside think() for every millisec I am checking if fTimeout is true and if so, I will return to main.

    But the problem is, since control is passed to think() from main, fgets() is not getting user input until control from think() is returned back to main. So how this can be achieved please?

    Thanks in advance.

    Someone replied
    I don't think that you need threading for this. This is simple case when user waits for execution and optionally stops it - for such case, it is enough to test pressed key periodically. You can print something like this:
    printf("Press Esc to stop\n");
    think(...);

    Inside think function, add the following code:

    Code:
    while (...)    // some loop - I guess you have it
    {
        // calculations
        // ...
    
        if ( _getch() == 27 )          // Esc ?
            return;
    }
    I asked:
    Well but I am not inputting them through keyboard.. There is a GUI interface through which commands like "quit", "go" etc are sent...
    So I doubt, if I use _getch() inside think() will fetch the commands sent by GUI ?
    Only in main() I can get right ?

    Someone replied:
    Is this console application or Winnows application? Looking at your main function I guess that this is console application. Do you have message loop?

    I asked:
    I am really sorry... I made a small mistake. Just replace that main() with xboard().
    main() calls the above xboard(pcon).
    ok?

    Someone replied:
    There are better ways to design the flow but.. you can just create a thread on yout think() function as follows:

    Code:
    void
    think(PVOID pArg){
        while (1) {
            PCON pCon = (PCON)pArg;
            // keep think...ing... 
    
            if (timeoutSet){
                ExitThread()
            }
        }
    }
    
    
    main(){
        
        for (;;;){
            // populate a new  struct arg from command fgets()..
            PCON pCon = new CON;
    
           If (command != "quit"){
               DWORD Id;
               CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE )think, pCon, 0, &Id);
            } else {
                 // set timeout flag
                // cleanup
                 // break;
            }
        }
    
        // what else to do ?
    }
    Since the above is in another forum, I am not refering that link because I dont know if its legal as per the rules of this forum. Thanks.

  11. #11
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    void
    think(PVOID pArg)
    
    //...
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE )think, pCon, 0, &Id);
    This isn't good. You shouldn't have to cast to LPTHREAD_START_ROUTINE. Declare your function with a DWORD return type.
    Do not declare this callback function with a void return type and cast the function pointer to LPTHREAD_START_ROUTINE when creating the thread. Code that does this is common, but it can crash on 64-bit Windows. (msdn)
    If I've got this straight, you have a function ('think') that does some processing, and you want the user to be able to cancel the processing? Would something like this work?
    Code:
    #include <windows.h>
    #include <iostream>
    
    
    
    DWORD WINAPI Think(LPVOID lpParam)
    {
      
      while (*(bool*)(lpParam))
      {
        std::cout<<"Thinking...";
      }
      return 0;
    }
    
    
    
    int main()
    {
    
      bool start = true;
      
      HANDLE hThread = CreateThread(0,0,Think,&start,0,0);
      while (1)
      {
        if (GetAsyncKeyState(VK_ESCAPE))
        {
          start = false;
          break;
        }
      }
    
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    9
    Well, but I need to pass pcon struct object to think() since I am doing some processing inside think().

    Why isnt creating a thread as,
    Code:
     CreateThread(NULL, 0, think, (LPVOID)pcon, 0, &Id);
    
    And think as,
    DWORD WINAPI think( LPVOID pArg )
    {
    ....
    }
    Why is the above throwing error ?
    For your information,
    static CON s_con;
    PCON pcon = &s_con;

  13. #13
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    It should still work:
    Code:
    DWORD WINAPI Think(LPVOID lpParam)
    {
      PCON pCon = (PCON)lpParam;
      while (!pCon->fTimeout)
      {
        //...
      }
      return 0;
    }
    
    
    CON s_con = {false,/*...*/};
      
    CreateThread(0,0,Think,&s_con,0,&Id);
    while (1)
      {
        if (GetAsyncKeyState(VK_ESCAPE))
        {
          s_con.fTimeout = true;
          break;
        }
      }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    9
    Ok, when I moved think() from seperate file to the file where main() and xboard() is there, its working.

    But small problem with the logic...

    Code:
    void xboard(PCON pcon)
    {
    DWORD Id;
    HANDLE hThread = CreateThread(0, 0, think, pcon, 0, &Id);
    while(1)
    {
      if (hThread == 0)
      break;
     if (!fgets(line, 256, stdin))
         continue;
     if (line[0] == '\n')
         continue;
     sscanf(line, "%s", command);
     if (!strcmp(command, "quit")) {
        pcon->fTimeout = TRUE;
    return;
        }
    }
    ... logic
    ... logic
    }
    Only after think thread is returned I need to perform the {... logic} part. Till then I need to loop and keep on checking if user inputs "quit". Suppose user inputs "quit" then fTimeout is set to "True" which will exit the thread. And also xboard() is returned to main().
    But since I am entering the infinite while(1) loop before the thread is returned, I am not able to compare hThread to 0. So how to accomplish this please ?

  15. #15
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    For your previous errors, are you including windows.h somewhere before your prototype of think()? (Since things like WINAPI are needed...)

    Also, what are you trying to do with that most recent while() loop? "since I am entering the infinite while(1) loop before the thread is returned" - then what is the point of the thread...? If you don't want the code to run concurrently, then why multithread? If you want to know if the thread is still running, there's a plethora of ways to do that... you can wait on a thread with the WaitFor*Object*() functions, or use GetExitCodeThread().
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange compile errors
    By csonx_p in forum C++ Programming
    Replies: 10
    Last Post: 07-28-2008, 11:41 AM
  2. compile once, compile twice ...error
    By Benzakhar in forum Windows Programming
    Replies: 6
    Last Post: 12-28-2003, 06:00 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM