I want to exit main loop by calling cancel_main_loop inside of the loop.

571 views
Skip to first unread message

sigflup synasloble

unread,
Aug 24, 2018, 8:36:48 PM8/24/18
to emscripten-discuss
I want to exit main loop by calling cancel_main_loop inside of the loop.

When I do this my program hangs :(

Alon Zakai

unread,
Aug 26, 2018, 9:49:00 AM8/26/18
to emscripten-discuss
This should work, and looks like we have a passing test for it, browser.test_emscripten_main_loop, so I think there might be something more going on in your application. Can you provide a testcase showing the issue?

On Fri, Aug 24, 2018 at 5:36 PM sigflup synasloble <pant...@gmail.com> wrote:
I want to exit main loop by calling cancel_main_loop inside of the loop.

When I do this my program hangs :(

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sigflup synasloble

unread,
Aug 26, 2018, 11:15:47 AM8/26/18
to emscripten-discuss
Maybe it's because I have the simulate infinite loop argument on


On Sunday, August 26, 2018 at 8:49:00 AM UTC-5, Alon Zakai wrote:
This should work, and looks like we have a passing test for it, browser.test_emscripten_main_loop, so I think there might be something more going on in your application. Can you provide a testcase showing the issue?

On Fri, Aug 24, 2018 at 5:36 PM sigflup synasloble <pant...@gmail.com> wrote:
I want to exit main loop by calling cancel_main_loop inside of the loop.

When I do this my program hangs :(

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.

Alon Zakai

unread,
Aug 26, 2018, 1:59:49 PM8/26/18
to emscripten-discuss
I don't think that should matter (it just affects what happens in the initial call to set the main loop). But let us know if you find a testcase showing that, perhaps there's a bug.

On Sun, Aug 26, 2018 at 8:15 AM sigflup synasloble <pant...@gmail.com> wrote:
Maybe it's because I have the simulate infinite loop argument on


On Sunday, August 26, 2018 at 8:49:00 AM UTC-5, Alon Zakai wrote:
This should work, and looks like we have a passing test for it, browser.test_emscripten_main_loop, so I think there might be something more going on in your application. Can you provide a testcase showing the issue?

On Fri, Aug 24, 2018 at 5:36 PM sigflup synasloble <pant...@gmail.com> wrote:
I want to exit main loop by calling cancel_main_loop inside of the loop.

When I do this my program hangs :(

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

sigflup synasloble

unread,
Aug 26, 2018, 2:23:10 PM8/26/18
to emscripten-discuss

#include <stdio.h>
#include <stdlib.h>

#include <string.h>
#include <SDL/SDL.h>
#include <emscripten.h>
 
 
SDL_Surface *screen;

 
void renderloop(void) {
 SDL_Event event;
 SDL_Rect dst;
  SDL_LockSurface(screen);

  for(int n=0;n<1000;n++) {
    int x=random()%800;
    int y=random()%600;
    int pixel=random()*100000;
    int bpp = screen->format->BytesPerPixel;
    Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
    if((x>screen->w)||(y>screen->h)||(x<0)||(y<0)) return;
    *(Uint32 *)p = pixel;
  }
 
  SDL_UnlockSurface(screen);


  SDL_UpdateRect(screen, 0,0,0,0);

  while(SDL_PollEvent(&event))
   if(event.type==SDL_KEYDOWN) emscripten_cancel_main_loop();

}



 
int main(int argc, char *argv[]) {
 

  SDL_Init(SDL_INIT_VIDEO);
 
  screen=SDL_SetVideoMode(800,600,32,SDL_ANYFORMAT);
 
  printf("in loop\n");
  emscripten_set_main_loop(renderloop,0,1);
  printf("out loop\n");
  return 0;
}

ok, you run it and press any key to exit the loop. When the loop stops it doesn't call "printf("out loop\n");" :(

On Sunday, August 26, 2018 at 12:59:49 PM UTC-5, Alon Zakai wrote:
I don't think that should matter (it just affects what happens in the initial call to set the main loop). But let us know if you find a testcase showing that, perhaps there's a bug.

On Sun, Aug 26, 2018 at 8:15 AM sigflup synasloble <pant...@gmail.com> wrote:
Maybe it's because I have the simulate infinite loop argument on


On Sunday, August 26, 2018 at 8:49:00 AM UTC-5, Alon Zakai wrote:
This should work, and looks like we have a passing test for it, browser.test_emscripten_main_loop, so I think there might be something more going on in your application. Can you provide a testcase showing the issue?

On Fri, Aug 24, 2018 at 5:36 PM sigflup synasloble <pant...@gmail.com> wrote:
I want to exit main loop by calling cancel_main_loop inside of the loop.

When I do this my program hangs :(

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.

Alon Zakai

unread,
Aug 27, 2018, 5:25:02 PM8/27/18
to emscripten-discuss
I think I see the issue - when you tell it to implement an infinite loop,  you can never get to the rest of main() - we actually stop main at that point (since as an infinite loop, the rest is not reachable). However, we do still let you cancel the main loop (which is just a JS event that keeps happening), which stops running without continuing in main. Perhaps that could be a little clearer in the docs.

In any case, if you want code to run after the main loop ends, you can perhaps put it in a function and call it right after canceling the main loop.


To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages