加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

pthread_atfork锁定成语坏了吗?

发布时间:2020-12-16 06:56:03 所属栏目:百科 来源:网络整理
导读:pthread_atfork用法的标准习惯用法是获取pre-fork处理程序中的所有锁,并在父处理程序和子处理程序中释放它们.但据我所知,这是不可能的.如果调用线程不是互斥锁的所有者,则指定pthread_mutex_unlock具有未定义的行为(在普通或默认类型互斥锁的情况下)或失败(
pthread_atfork用法的标准习惯用法是获取pre-fork处理程序中的所有锁,并在父处理程序和子处理程序中释放它们.但据我所知,这是不可能的.如果调用线程不是互斥锁的所有者,则指定pthread_mutex_unlock具有未定义的行为(在普通或默认类型互斥锁的情况下)或失败(在递归或错误检查互斥锁的情况下).在使用pthread_atfork注册的子处理程序中,调用线程是新创建的进程的主线程,因此不能是互斥锁的所有者.

我错了还是整个pthread_atfork成语被设计破坏并且基本上不可能使用?

编辑:我也没有看到该问题的任何有效(可移植)解决方法.理想情况下,可以在子进程中销毁和重新初始化互斥锁,除了在初始化的互斥锁上调用pthread_mutex_destroy被指定为未定义的行为,以适应互斥体不是POD但涉及对某些内核级对象的引用的荒谬实现.

解决方法

我认为这是男人的相关文字:

When fork() is called,only the
calling thread is duplicated in the
child process
. Synchronization
variables
remain in the same state in the child as they were in the parent at
the time fork() was called. Thus,for
example,
mutex locks may be held by threads that no longer exist in the
child process,and any associated
states may be
inconsistent. The parent process may avoid this by explicit
code that acquires and releases locks
critical to the
child via pthread_atfork(). In addition,any critical threads
need to be recreated and reinitialized
to the
proper state in the child (also via pthread_atfork()).

在子节点中执行atfork处理程序的线程是在父节点中执行atfork prepare处理程序的线程的精确副本,因此有权解锁互斥锁.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读