2

我在一个线程中。我有一个地址。该地址是否来自我正在使用的同一堆栈上的变量?

static int *address;

void A()
{
    int x;
    atomic::CAS(address, 0, &x); // ie address = &x
    // ...
}

void B()
{
   int y;
   int * addr = atomic::read(address); // ie addr = address
   if (addr && on_same_stack(&y, addr))
   {
      // B() called from A()
   }
   else
   {
      // B() called from different thread than A()
   }
}

我需要实施on_same_stack(addr1, addr2). 我知道 Windows 上的堆栈会根据需要增长,但我也知道增长是有限制的,而且(至少在调试中)每个函数调用都有堆栈溢出检查代码。所以我认为这是可以做到的。

现在,我也知道我可以/应该使用线程 ID 等。但是我正在尝试在这里实现一些棘手的无锁编码,而且我真的没有空间来存储线程 ID,只有一个指针. (我希望避免 CMPXCH16)。请相信我,我有点知道我在做什么:-)。

这目前仅适用于 Windows。但是越便携越好。(NT/XP/7/CE?)

PS这个网站被称为“stackoverflow”所以它应该是正确的地方,不是吗?:-)

编辑:添加上下文,因为每个人都在问。我正在实现一个类似于 pthread_once 或 boost.threads call_once 的自定义 call_once。我正在尝试检查递归。我的工作非常有限。我无法添加函数参数。我无法假设程序的其余部分在做什么,比如他们已经使用了多少 TLS。等等。我只能在我的一个函数内编码,并且不能对除此之外的任何内容进行更改或假设。

感谢您的问题/回答。

4

3 回答 3

2

像(未经测试)这样疯狂的东西怎么样:

declspec(__thread) void* stackBottom;

void Thread1Routine(void* arg)
{
  volatile int bottom;
  stackBottom = ⊥

  ... (do stuff which ends up calling on_same_stack()) ...
}


bool on_same_stack(void* p)
{
  volatile int top;
  return ((LONG_PTR)p >= (LONG_PTR)&top) && ((LONG_PTR)p <= (LONG_PTR)stackBottom);
}

(编辑以消除理论上的基于寄存器的 arg 传递问题)

于 2009-11-16T17:18:52.403 回答
2

使用Win32 线程信息块

这些示例使用主线程,但实际上它必须在任何线程中运行。

示例 #1:

#include <iostream>
#include <windows.h>
#include <winnt.h>
#include <intrin.h>

inline size_t get_thread_top_stack_size()
{
    NT_TIB *s = (NT_TIB*)getTib();
    return (size_t)s->StackBase ;
}

int global_var;

int main () 
{
    size_t sp_value = 0; 
    _asm { mov [sp_value], esp } 
    size_t thread_top_stack = get_thread_top_stack_size();

    int my_local_var;
    size_t my_local_var_addr = (size_t)&my_local_var;
    if (my_local_var_addr  < thread_top_stack && my_local_var_addr > sp_value  ) {
        std::cout << "Yes, on the thread stack";
    } else {
        std::cout << "No, not on the thread stack";
    }

    size_t my_global_var_addr = (size_t)&global_var;
    if (my_global_var_addr < thread_top_stack && my_global_var_addr> sp_value  ) {
        std::cout << "Yes, on the thread stack";
    } else {
        std::cout << "No, not on the thread stack";
    }
    return 0;
}

示例 #2:

#include <windows.h>
#include <winnt.h>
#include <intrin.h>

inline NT_TIB* getTib()
{
    return (NT_TIB*)__readfsdword( 0x18 );
}

inline bool is_var_on_the_thread_stack(void* ptr)
{
    NT_TIB *nt_tib = getTib();
    return (nt_tib->StackBase >= ptr) &&  (nt_tib->StackLimit <= ptr );
}

int my_global_var;

int main () 
{
    int my_thread_var;
    if (is_var_on_the_thread_stack(&my_thread_var)) {
        std::cout << "Yes, on the thread stack" << std::endl;
    } else {
        std::cout << "No, not on the thread stack" << std::endl;
    }
    if (is_var_on_the_thread_stack(&my_global_var)) {
        std::cout << "Yes, on the thread stack" << std::endl;
    } else {
        std::cout << "No, not on the thread stack" << std::endl;
    }
    return 0;
}
于 2009-11-17T08:39:51.387 回答
1

从您的评论B() called from A()中,您不能只传递一个参数B()并将其设置为从调用时的特定值A()吗?(使用默认参数值,不需要大的改变)

你的样本不是很完整,所以这是另一个尝试对你的问题做出很多假设......怎么样:

void A()
{
    B_lockfree();
}

void B()
{
    // acquire a lock here
    B_lockfree();
    // release your lock here
}

void B_lockfree()
{
    // do whatever you want here
}

(好吧,我可以想到很多方法,但在不了解大局的情况下,它们可能都是完全错误的......)

于 2009-11-16T17:21:53.457 回答