Re: help: why dosent this code work?

From: Peter T. Breuer (ptb_at_lab.it.uc3m.es)
Date: 02/20/05


Date: Sun, 20 Feb 2005 09:42:40 +0100

vesper_carmen@yahoo.com wrote:
> This code is supposed to print "hello world" character by character
> with an inteval of 40 ms between it. But it goes into some kind of
> infinite loop :(

I suggest you debug it, then. Get it down to ten or max twenty lines of
code and somebody may be inclined to help you. And indenting reasonably
may help.

  char *h = "Hello World\n";
  int main ()
  {
      printf ("\n In main \n");
      //initialize mutex here?

Somewhere at least. Shume mutex shumwhere?
  
      init_timer (40000);

Does that launch the thread? Yes it does. And runs it (pthread_create).
Does it really run? Have you checked with printf? As far as I could see
it is stuck waiting on a semaphore that I never saw anyone signal.

  
      while (*h++) {

Well, isn't that all wrong for a start? You will count through the
characters in h. Shrug.

          while (!stopped) ;

And that appears to be an infinite busy loop. I suppose you are hoping that
a thread will change it? But when? What does the scheduler think is the
time to let the other thread in? Is this where you are stuck?

          stopped = 1;

But then why do you set stopped to 1, when it is already that, since it
just exited the busy loop?
  
          init_timer (40000);

Well, you don't seem to trust do while.

      }
  
      printf ("\n %d \n", &delaycounter);
      //sleep();
  
  }

Go and debug your code - first find where you are stuck. Then find WHY.
I would imagine that you are stuck in the busy while(!stopped) loop.

If you don't feel like debugging it, explain it. There are at least two
threads, and one of those threads seems to be prepared to create more
threads, so you cannot expect people to know what you have in mind for
the code to do.

If you don't feel like explaining it, reduce it to smething that need
no explanation.

I would guess that you are all mixed up about who gets signals. But
then I'm not going to do your debugging for you!

Peter



Relevant Pages

  • Re: Batch script for maintaing remote services
    ... I want to kill this service before sc command go on stopping ... Does the loop breaks when it finds "stopped" status for a particular ... But how will I kill this service if it gets stuck in ... command parameter, makes sure that next service is only ...
    (microsoft.public.windowsxp.general)
  • Re: locked thread - how to investigate?
    ... With truss is there any way to focus on a ... the threads are being consumed which causes more consumption of the ... Perhaps you mean a thread stuck in a CPU loop? ...
    (comp.unix.solaris)
  • Calculating an average [was Re: Summary of loops]
    ... Been stuck doing this program and ... > cant seem to debug the errors! ... > How do I calculate the totals of the computation in a loop ...
    (comp.lang.java.help)
  • Re: Error Handling not Working ASP.NET
    ... Your code could easily get stuck in a loop in the error handler... ... any unhandled exception goes to Application_Error, ...
    (microsoft.public.dotnet.framework.aspnet)
  • problem with xp-tablet dotnet application
    ... compact-part). ... When I put a messagebox to see where ge gets stuck the dialog appears ... I looks like he gets into a wait loop in the onPaint ... but I don't have a onPaint handler in that Form. ...
    (microsoft.public.dotnet.languages.csharp)