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.