首页 \ 问答 \ 从线程ID获取pthread_t(get pthread_t from thread id)

从线程ID获取pthread_t(get pthread_t from thread id)

我无法找到将线程ID( pid_t )转换为pthread_t的函数,这将允许我调用pthread_cancel()pthread_kill()

即使pthreads没有提供一个是否有Linux特定的功能? 我不认为这样的功能存在,但我很乐意纠正。

背景

我很清楚通常最好让线程通过条件变量等来管理自己的生命周期。

此用途用于测试目的。 我试图找到一种方法来测试应用程序在其中一个线程“死亡”时的行为方式。 所以我真的在寻找一种杀死线程的方法。 使用syscall(tgkill())杀死进程,因此我为测试人员提供了一种方法,让进程获得要杀死的线程的id。 我现在需要将该id转换为pthread_t,以便我可以:

  • 使用pthread_kill(tid,0)检查它的存在性
  • 根据需要调用pthread_kill()pthread_cancel()

这可能会使测试达到一个不必要的极端。 如果我真的想这样做,某种模拟pthread实现可能会更好。

实际上,如果你真的想要强大的隔离,你通常最好使用进程而不是线程。


I am unable to find a function to convert a thread id (pid_t) into a pthread_t which would allow me to call pthread_cancel() or pthread_kill().

Even if pthreads doesn't provide one is there a Linux specific function? I don't think such a function exists but I would be happy to be corrected.

Background

I am well aware that it is usually preferable to have threads manage their own lifetimes via condition variables and the like.

This use is for testing purposes. I am trying to find a way to test how an application behaves when one of its threads 'dies'. So I'm really looking for a way to kill a thread. Using syscall(tgkill()) kills the process, so instead I provided a means for a tester to give the process the id of the thread to kill. I now need to turn that id into a pthread_t so that I can then:

  • use pthread_kill(tid,0) to check for its existence followed by
  • calling pthread_kill() or pthread_cancel() as appropriate.

This is probably taking testing to an unnecessary extreme. If I really want to do that some kind of mock pthreads implementation might be better.

Indeed if you really want robust isolation you are typically better off using processes rather than threads.


原文:https://stackoverflow.com/questions/46323502
更新时间:2023-01-05 21:01

最满意答案

而不是这样做,更好的方法可能是简单地将两个项目统计为一行。 getView()方法返回一个View,它表示getItem()返回的每个项目。 在您的情况下,每个项目包含两个元素。 所以只需将逻辑放入其中就可以一次检索两个元素。 可能更容易将它们封装在类中,例如:

ArrayList<Row> mItems = new ArrayList<Row>();

private class Row {
  Object obj1;
  Object obj2;
}

public void addItem(Object obj) {
  Row useRow;
  if(mItems.isEmpty()) {
     useRow = new Row();
  } else {
     useRow = mItems.get(mItems.size() - 1);
     if(useRow.obj2 != null) {
        useRow = new Row();
     }
  }

  if(useRow.obj1 == null) {
     useRow.obj1 = obj;
  } else {
     useRow.obj2 = obj;
  } 

  mItems.add(useRow);
}

在这种情况下,BaseAdapter由Row对象列表支持。 每个Row对象包含两个元素。 每次添加元素时,都会将其添加到第一个元素,然后添加到第二个元素,否则您将创建一个新的Row对象并添加它。

编辑:

为了具有可点击性,您必须为每个项目的View实现OnClickListener。 这样的事情可能有用:

public interface ItemClickListener {
   public void onItemClick(Object obj);
}

private ItemClickListener mClickListener;

public void setItemClickListener(ItemClickListener listener) {
   mClickListener = listener;
}

@Override
public boolean areAllItemsEnabled() {
  return false;
}

@Override
public boolean isEnabled() {
   return false;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   ViewGroup root;
   if(convertView == null) {
      root = buildRootView();
      View item1View = buildFirstView();
      View item2View = buildSecondView();
      ...
      item1View.setOnClickListener(mItemListener);
      item2View.setOnClickListener(mItemListener);
      ...
      // Put both Views in your top level root view if they are not there already
   }

   Row row = getItem(position);
   item1View.setTag(row.obj1);
   item2View.setTag(row.obj2);
}

private View.OnClickListener mItemListener = new View.OnClickListener {
   @Override
   public void onClick(View v) {
      Object obj = (Object) v.getTag();
      if(mClickListener != null) {
          mClickListener.onItemClick(obj);
      }
   }
}

所以基本上,你通过覆盖“areAllItemsEnabled()”和“isEnabled()”来禁用单击以返回“false”。 然后,每次用户单击一行时,适配器中的单击侦听器都将激活。 由于您将行的Object放在View的标记中,因此您可以在单击时检索它。 即使ListView循环使用,它也会被交换到一个新的Object ,因为它每次调用getView() 。 然后创建一个从click界面继承的对象,以检索对象并执行您需要的任何操作。


Rather than do it this way, a better method may be to simply count two items as one row. The getView() method returns a View that is a representation of each item returned by getItem(). In your case, each item contains two elements. So just put the logic in that would retrieve two elements at a time. May be easier to encapsulate them in a class like for example:

ArrayList<Row> mItems = new ArrayList<Row>();

private class Row {
  Object obj1;
  Object obj2;
}

public void addItem(Object obj) {
  Row useRow;
  if(mItems.isEmpty()) {
     useRow = new Row();
  } else {
     useRow = mItems.get(mItems.size() - 1);
     if(useRow.obj2 != null) {
        useRow = new Row();
     }
  }

  if(useRow.obj1 == null) {
     useRow.obj1 = obj;
  } else {
     useRow.obj2 = obj;
  } 

  mItems.add(useRow);
}

In this case, your BaseAdapter is backed by a List of Row objects. Each Row object contains two of your elements. Every time you add an element, you add it to the first, then to the second, else you create a new Row object and add it.

EDIT:

In order to have clickability, you'll have to implement an OnClickListener to each item's View. Something like this may work:

public interface ItemClickListener {
   public void onItemClick(Object obj);
}

private ItemClickListener mClickListener;

public void setItemClickListener(ItemClickListener listener) {
   mClickListener = listener;
}

@Override
public boolean areAllItemsEnabled() {
  return false;
}

@Override
public boolean isEnabled() {
   return false;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   ViewGroup root;
   if(convertView == null) {
      root = buildRootView();
      View item1View = buildFirstView();
      View item2View = buildSecondView();
      ...
      item1View.setOnClickListener(mItemListener);
      item2View.setOnClickListener(mItemListener);
      ...
      // Put both Views in your top level root view if they are not there already
   }

   Row row = getItem(position);
   item1View.setTag(row.obj1);
   item2View.setTag(row.obj2);
}

private View.OnClickListener mItemListener = new View.OnClickListener {
   @Override
   public void onClick(View v) {
      Object obj = (Object) v.getTag();
      if(mClickListener != null) {
          mClickListener.onItemClick(obj);
      }
   }
}

So basically, you disable the clicking by overriding "areAllItemsEnabled()" and "isEnabled()" to return "false". Then, the click listener in the adapter will activate each time the user clicks on a row. Since you put the Object of the row in the View's tag, you can retrieve it on click. It will be swapped to a new Object even when the ListView recycles because it calls getView() each time. Then create an object that inherits from the click interface to retrieve the object and do whatever you need.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。