I have created pool of 5 threads using _beginthread()
in C. All the threads from pool will run infinitely to do job received from client. For example, when client will be connected, the free thread from pool will handle this. After completing its task the thread will wait for next client. Now my problem is that, whenever I enter ctrl+c
from server, the program get terminated. But I want to close all the running and waiting threads from pool before terminating the main thread.
Asked
Active
Viewed 1,144 times
0

mehraj23
- 27
- 8
-
C or C++? Pick a language please. – πάντα ῥεῖ Jun 07 '17 at 06:58
-
If you're programming in C then please don't add the C++ tag. While the language share a common ancestor and have similar syntax, they are still very different languages. – Some programmer dude Jun 07 '17 at 06:58
-
3As for your problem, is the question about how to catch `CTRL-C`, how to nicely tell threads to end, or a combination of both (in which case you should post two questions)? If you haven't done it yet (weird, since you have been a member for over three years), then please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Jun 07 '17 at 06:59
-
For the first part of this problem, [see this question](https://stackoverflow.com/questions/18291284/handle-ctrlc-on-win32) – Jun 07 '17 at 07:13
-
What is meant by run infinitely? Had you use while(1) to do so? Or client task handle in any other API which takes control of thread? Could you please share thread pseudo code? There are 3 possible solution, 1. you can watch global variable in while(1) to trigger thread cleanup, 2. Before calling any blocking API in thread you can set thread cancel point and can send cancel from signal handler. or 3. you can have signal handler for thread (Don't know about window, for linux [refer](https://stackoverflow.com/questions/24643311/unix-pthreads-and-signals-per-thread-signal-handlers) ) – Pravin Jun 07 '17 at 07:40
-
So once you have the [Ctrl]+[C] sorted out, how are you planning to go about a user clicking the console's close button? @Pravin: I'm sorry, but Windows has a lot more options than you list. After all, multithreading was built into the system from the start. There is a rich synchronization API, that usually completely eliminates the need to poll (like your proposed solutions will). – IInspectable Jun 07 '17 at 08:27
1 Answers
1
For Windows
#include <windows.h>
#include <stdio.h>
BOOL WINAPI signal_handler(DWORD signal) {
if (signal == CTRL_C_EVENT){
printf("CTRL C caught here .\n");
}
return TRUE;
}
main()
{
....
SetConsoleCtrlHandler(signal_handler, TRUE);
.....
printf("This should appear after signal_handler\n");
}
For linux
#include<stdio.h>
#include<signal.h>
void signal_handler()
{
printf("CTRL C caught here .\n");
}
main()
{
...
// SIGINT is signal name create when Ctrl+c will pressed
signal(SIGINT,signal_handler);
printf("This should appear after signal_handler\n");
}
Once ctrl-c is captured you can close all the threads and other cleanup stuff

Deb S
- 509
- 5
- 16
-
It may be worth noting that in Windows, `signal_handler` runs in a separate thread (and the main thread continues to run in parallel) whereas in Linux the main thread is interrupted in order to run `signal_handler`. – Harry Johnston Jun 07 '17 at 23:15