1

I have an existing c++ embedded application (ARM) with a non-threaded web server. It is used like this:

main(){
   while(1){
     doIO();
     doProcess1();
     doProcess2();
     etc...
     webServer.poll(); 
   }
}

The webServer.poll() serves pages from a base directory and calls one of my functions if the url contains a specific pattern so it can respond to an rpc call that will often read or write to the SD card.

I have to replace the existing web server code and I can't find a non-threaded, embedded web server that meets my requirements (no Boost being the biggest.)

Civetweb seems to meet my requirements and is working fine, but it is threaded (1 per connection.)

My question is, since re-writing all of the existing code to be thread safe is not an option, would it be ok to do something like this with a global "threadLock"?

main(){
   startThreadedWebServer();
   while(1){
     pthread_mutex_lock(&threadLock);
     doIO();
     doProcess1();
     doProcess2();
     etc...
     pthread_mutex_unlock(&threadLock);
     sleep(0.1); //give webServer some time to get the lock
   }
}

And then in the rpc function:

pthread_mutex_lock(&threadLock);
doRpcCode() 
pthread_mutex_unlock(&threadLock);

The server can serve normal pages in a thread, but any rpc calls would be locked.

I am doing this now and it is working, but I don't know if there are any potential issues. This application typically has 1 user, maybe 3 at most, with rpc calls every few seconds. Speed is not really a concern.

k_hampton
  • 23
  • 3
  • There's nothing fundamentally wrong with that approach other than terrible scalability. If the latter is no concern, I don't see why it couldn't be used. – Special Sauce Dec 18 '15 at 17:31
  • [GNU Libmicrohttpd](https://www.gnu.org/software/libmicrohttpd/) might meed your needs--it supports a number of different threading models. – Mark Waterman Dec 18 '15 at 19:34
  • Libmicrohttpd is the first one I tried, out of 10+ different libraries. I don't recall why I passed on that one, but I actually prefer the threaded serving of other files if it doesn't cause any problems. – k_hampton Dec 19 '15 at 15:03
  • Have a look at [Bitty HTTP](http://bittyhttp.com) I wrote it because I had similar needs to what you have. It doesn't use threads, and is meant to be called in the same way as webServer.poll() is called in your main loop. – Paul Hutchinson Oct 18 '19 at 22:17

0 Answers0