【问题标题】:Checking pthread cancelability state检查 pthread 可取消状态
【发布时间】:2018-03-05 21:36:04
【问题描述】:

看到我可以使用 pthread_setcancelstate 来设置给定 pthread 的可取消性。 (启用或禁用它)。 但是,我找不到匹配的“pthread_getcancelstate”函数来检查给定 pthread 的可取消状态。任何建议将不胜感激。

谢谢

【问题讨论】:

  • 你认为你为什么需要这个功能?
  • 它不会做你认为它做的事情,它只是规定了 pthread_cancel() 的行为方式。
  • 我想检查是否可以在特定 pthread 上调用 pthread_cancel。根据手册页,必须启用 pthread 可取消性。我想事先检查该属性。
  • 您要求的是线程竞争错误。您检查 => 线程更改状态 => pthread_cancel() 没有做您希望的事情。避免谈论你无法获得的解决方案,编辑你的问题以专注于你真正想要完成的事情。
  • 为什么要在创建线程后更改可取消状态?据我了解,这是您在创建时定义的一种属性。类似于将线程定义为可连接或可拆卸。以 pthread_attr_getdetachstate 为例。为什么这个函数存在而“pthread_getcancelstate”不存在?

标签: c pthreads


【解决方案1】:

http://man7.org/linux/man-pages/man3/pthread_setcanceltype.3.html

根据手册页设置状态也将获得它被配置的旧状态.. 在这方面,一个简单的 get 操作可以设置一个状态,然后将状态设置回旧状态 - 我怀疑这会影响你的逻辑

您在第二次调用时设置的状态是线程所处的状态:

int curr_state;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &curr_state);
pthread_setcancelstate(curr_state, NULL);
//curr_state is the result of your required get

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 2020-01-27
    • 2010-11-24
    • 2017-11-04
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多