Я написал программу, которая порождает поток, который читает в цикле из stdin блокирующим способом. Я хочу, чтобы поток возвращался из заблокированного чтения немедленно. Я зарегистрировал обработчик сигнала (с сигментом и без знака SA_RESTART) в потоке чтения, отправил ему сигнал и ожидал, что прочитанное будет завершено с ошибкой EINTR. Но этого не происходит. Это проблема или ограничение Cygwin или я делаю что-то неправильно? Вот код:Cygwin: прерывание блокировки читать
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
pthread_t thread;
volatile int run = 0;
void root_handler(int signum)
{
printf("%s ENTER (thread is %x)\n", __func__, pthread_self());
run = 0;
}
void* thr_func(void*arg)
{ int res;
char buffer[256];
printf("%s ENTER (thread is %x)\n", __func__, pthread_self());
struct sigaction act;
memset (&act, 0, sizeof(act));
act.sa_sigaction = &root_handler;
//act.sa_flags = SA_RESTART;
if (sigaction(SIGUSR1, &act, NULL) < 0) {
perror ("sigaction error");
return 1;
}
while(run)
{
res = read(0,buffer, sizeof(buffer));
if(res == -1)
{
if(errno == EINTR)
{
puts("read was interrupted by signal");
}
}
else
{
printf("got: %s", buffer);
}
}
printf("%s LEAVE (thread is %x)\n", __func__, pthread_self());
}
int main() {
run = 1;
printf("root thread: %x\n", pthread_self());
pthread_create(&thread, NULL, &thr_func, NULL);
printf("thread %x started\n", thread);
sleep(4);
pthread_kill(thread, SIGUSR1);
//raise(SIGUSR1);
pthread_join(thread, NULL);
return 0;
}
Я использую Cygwin (1.7.32 (0,274/5/3)).