gpt4 book ai didi

c - 为什么要先屏蔽信号再解除屏蔽?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:25:47 25 4
gpt4 key购买 nike

在阅读某些涉及 v4l2 API 的源代码时,我偶然发现了这些片段:

首先:

sigemptyset(&set);
sigaddset(&set, SIGCHLD);
sigaddset(&set, SIGALRM);
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGTERM);
sigaddset(&set, SIGHUP);
pthread_sigmask(SIG_BLOCK, &set, &old);

然后:

pthread_sigmask(SIG_UNBLOCK, &old, NULL);

介于两者之间:

if (s->pframe >= 0) {
if (xioctl(s->fd, VIDIOC_QBUF, &s->buf) == -1) {
motion_log(LOG_ERR, 1, "%s: VIDIOC_QBUF", __FUNCTION__);
return -1;
}
}

memset(&s->buf, 0, sizeof(struct v4l2_buffer));

s->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
s->buf.memory = V4L2_MEMORY_MMAP;

if (xioctl(s->fd, VIDIOC_DQBUF, &s->buf) == -1) {

/* some drivers return EIO when there is no signal,
driver might dequeue an (empty) buffer despite
returning an error, or even stop capturing.
*/
if (errno == EIO) {
s->pframe++;
if ((u32)s->pframe >= s->req.count) s->pframe = 0;
s->buf.index = s->pframe;

motion_log(LOG_ERR, 1, "%s: VIDIOC_DQBUF: EIO (s->pframe %d)", __FUNCTION__, s->pframe);

return 1;
}

motion_log(LOG_ERR, 1, "%s: VIDIOC_DQBUF", __FUNCTION__);

return -1;
}

s->pframe = s->buf.index;
s->buffers[s->buf.index].used = s->buf.bytesused;
s->buffers[s->buf.index].content_length = s->buf.bytesused;

我的诚实猜测是,在这种特殊情况下,阻塞信号可以防止设置过程被中断。但我完全不确定。请帮忙?

最佳答案

https://support.sas.com/documentation/onlinedoc/sasc/doc750/html/lr1/zlocking.htm

你猜对了,应该是为了防止线程中断。上面的链接不是特定于 pthreads,但概念是相同的,我认为解释得很好

编辑:pthreads 关于阻塞信号的解释 http://maxim.int.ru/bookshelf/PthreadsProgram/htm/r_40.html

关于c - 为什么要先屏蔽信号再解除屏蔽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35374807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com