pthread_create(&thread, NULL, AcceptLoop, (void *)this);
I have declared like this and inside of the AcceptLoop function I have infinity while loop. I’d like to close this thread when the server is closed. I have read pthread_cancel and pthread_join but I am not sure which one is better and safer. I would like to hear some detailed instructions or tutorials. Thanks in advance.
I believe you would like to exit the worker thread by signalling from the main thread.
Inside
AcceptLoopinstead of looping infinitiely you loop on a condition, you can set the condition through your main thread, You will have to use some synchronization for this variable. Once the variable is set from main thread the worker threadAcceptLoopwould break out and you can then callpthread_exit.if you would like your main thread to wait for child thread to exit you can use
pthread_jointo do so.In general, A child thread can exit in three conditions:
pthread_exit.pthread_cancel.