I'm trying to create a simple server that starts a new thread for each new client that connects. I know I can use fork(), but I've heard that threads are more efficient. Also I have some global queues, so if I used fork(), each process would get their own version of the queues. (I could fix this with shared memory).
But I'm not sure where to implement this in my code.
Heres the pseudocode of my server and main accept() loop:
Global queue1;
Global queue2;
<create socket and request socket, fill address structure, bind(), listen()>
while(1) {
<socket = accept()>
<close request socket>
<Processing the request>
<Closing socket>
}
So I need to know where/how, in my code, to implement threads, and which type of threads to use. It is supposed to run on Linux.
Also I need to know how to synchronize Global queue1 and Global queue2 so that the threads can access and write/delete from them without problems.