Link to home
Start Free TrialLog in
Avatar of gopikrish
gopikrish

asked on

Problem with Struct object for parallel process checking in 2 diff. functions..

I am trying to access a variable of an object which is declared inside a struct. But my logic is failing. Let me explain my logic below.

My struct is as follows in a header file "defs.h",

typedef      struct      tagCON {
      BOOL      fAbort;  // This is set to TRUE if the engine should immediately end its search.

               BOOL      fTimeout;

             SEARCH_STATUS      ss;

}      CON, * PCON;


#include "defs.h"
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")) {
computer = side;
continue;
}

if (side == computer)
            {
think(pcon, tmMyTime, tmTheirTime);
// Some ouputs i display.....
continue;
}

if (!strcmp(command, "quit")) {
pcon->fAbort = TRUE;
return;
}
} //end for loop

}

So think() function executes for very long time. And inside think() function there is a condition if (pcon-> fAbort) return;
So while think is processing, here in main(), I make pcon->fAbort = TRUE when "quit" command is inputed by the user. But the problem is think() is not getting breaked. Actually this program is hooked to a GUI called s Winboard. When I close Winboard, my program is not getting killed. In task manager its running and each and every time I have to "end process" it.

  So whats wrong with this? Thanks.
ASKER CERTIFIED SOLUTION
Avatar of manish_regmi
manish_regmi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gopikrish
gopikrish

ASKER

Yep side and computer are integers.
I did computer = EMPTY;
where EMPTY is defined as #define EMPTY            6

But still its not quitting. Any other ideas please ?
oops it seems that your code never reached there
 //// remove that continue in strcmp(command , "go").
continue will again take you to fgets.

for(;;)
     {
if (!fgets(line, 256, stdin))
return;
sscanf(line, "%s", command);

if (!strcmp(command, "go")) {
computer = side;
//continue;  //// remove that continue
}

if (side == computer)
          {
think(pcon, tmMyTime, tmTheirTime);
// Some ouputs i display.....
computer = <somethingelse>;   //add this
continue;
}

if (!strcmp(command, "quit")) {
pcon->fAbort = TRUE;
return;
}
} //end for loop


regards
Manish regmi
a better code will be

for(;;)
{
    if (!fgets(line, 256, stdin))
        return;
     sscanf(line, "%s", command);

    if (!strcmp(command, "go")) {
     think(pcon, tmMyTime, tmTheirTime);
   }
   else if(!strcmp(command, "quit"))
   {
       break;
   }
   else
   {
     ?? whatever error you want to show
    }
}


in your code there is no use of
pcon->fAbort = TRUE;

because just after that you are returning from main.
also there is no use of computer and side.


regards
Manish Regmi

No think runs for long time (more than 10 mins !)
So while think is running, user inputs "quit"
I hope you got it. So as soon as "quit" is inputted, my program has to stop.
ok, i understood. It is not possible unless you write a multithreaded program.

main thread process input
thread1 process think.

which platform are you in?

for posix threads. this is a start
http://yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
http://sourceware.org/pthreads-win32/ (pthreads for windows)

for windows threads
http://www.oreilly.com/catalog/multithread/excerpt/ch01.html

you will also need a method to notify another thread about an event ie quit pressed by user. Threads library has a lot of things to handle that.

regards
Manish Regmi

hey I made it to work using another method !

From my think() function after certain time interval, I call checkup() regularly and see if user has entered "quit". If so it will set pcon->fAbort to TRUE. And so I after I return back to think() I am checking if (pcon->fAbort == TRUE) and hence I return; (ie quit from think() ) :)

void checkup(PCON pcon)
{
      char line[256], command[256];

      if (!fgets(line, 256, stdin))
            return;
      sscanf(line, "%s", command);
      if (!strcmp(command, "quit")) {
      pcon->fAbort = TRUE;
      return;
      }
}

Anyway thanks for the help !


i was surprised to know it work. possibly it was waiting in fgets and you typed "exit" and thought it worked.

the problem with that code the checkup function blocks. I.e your other parts of think function will NOT work until the user types some characters followed by return key.

suppose your think function shuld run for 10 mins and you are checking for quit every 1 second. you cave to type return key 6000 times before that function runs whole and you type "exit". If you do not type return key it will wait indefinately and think() will never complete its work.

regards
Manish Regmi
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If it is multithreaded, the communication structure should be declared as volatile.