0

I need to trasmit with a (Boost) tcp server information collected in real time by the ARToolKit video tracking library.

Which is the right way of doing it?

I'm actually doing it with Boost threads and asio, but I think that what I do is done in a bad way (even if it works)

Here is what I do to run the server (the source of the Server class is from Boost tutorial):

 boost::asio::io_service io_service;
 Server s(io_service, 2345);
 boost::thread bt(boost::bind(&boost::asio::io_service::run, &io_service)); //server in background in a second thread

Then I start the video tracking

 startTracking(); //blocking call in the main thread

defined in this way

 void startTracking(){
 glutInit(&argc, argv); //global and reachable
 if ((gArglSettings = arglSetupForCurrentContext()) == NULL) {
    fprintf(stderr, "main(): arglSetupForCurrentContext() returned error.\n");
    exit(-1);}

 ... //init a lot of artoolkit parameters
 arVideoCapStart();
 argMainLoop( NULL, keyEvent, mainLoop );
 }

In this (horrible) way everything works. But I would like to avoid spawning a second thread for the asio server (it is not supposed to be thrown there, as I read from the Boost doc).

Otherwise trying to put the video traking out of the main thread crashes the ARToolKit library ie:

 boost::thread workerThread(startTracking);  
 workerThread.join();

When the join() is run the program segfaults at glutInit call

UnableToLoad
  • 315
  • 6
  • 18
  • 1
    There is not enough information to help you here... What does `startTracking()` do? Is it thread-safe? – Chad Feb 09 '13 at 20:11
  • what does *everything crashes* mean? Segfault? Erase your hard drive? – Sam Miller Feb 09 '13 at 21:14
  • It segfaults at glutInit() call, or trying to comment it segfault to the next call, arglSetupForCurrentContext() I don't know if it is thread safe – UnableToLoad Feb 10 '13 at 12:14
  • @UnableToLoad perhaps you may not call these functions in in a non-main thread. Take a look at theier documentation. – Igor R. Feb 10 '13 at 14:24
  • Yes, arglSetupForCurrentContext needs it, but so do I have any advantages of using an asynchronous server? (as I'm actually doing) – UnableToLoad Feb 10 '13 at 15:44

1 Answers1

0

What do you think the workerThread.join() method does? Take a look at the answer to this question. So, calling the join method will cause the thread it is called from (main thread) to block and wait until the worker thread has completed. Is that what you want? If you have set up ASIO to run on that main thread, then none of the ASIO I/O socket handlers will be able to execute and thus it will appear to hang because the thread it is on is frozen from the join method. Likewise for the ARToolKit library, if the calls to it have been initiated on this main thread, then it too will appear to freeze because that thread is frozen when the join method is called.

If this is not your problem, then please provide more code.

Community
  • 1
  • 1
Bob Bryan
  • 3,687
  • 1
  • 32
  • 45
  • Sorry for the bad explanation, it doesn't "freeze", it segfaults. What I want is to know which is the right way of doing what I'm doing, I mean running a server and video tracking at the same time, because how I use it now I don't think I'm using in the right way the asynchronous server – UnableToLoad Feb 10 '13 at 12:17