C++ 如何获取指向调用我的函数的线程的指针?
使用C++ 如何获取指向调用我的函数的线程的指针?,c++,multithreading,boost,boost-thread,C++,Multithreading,Boost,Boost Thread,使用boost::thread如何从该函数中获取指向当前正在执行我的函数的boost::thread的指针 以下内容不适合我: boost::thread *currentThread = boost::this_thread; 您可以使用boost::this_线程来引用在中使用它的同一个线程 请参见您必须小心,因为boost::thread是一种可移动类型。考虑以下事项: boost::thread make_thread() { boost::thread thread([](b
boost::thread
如何从该函数中获取指向当前正在执行我的函数的boost::thread
的指针
以下内容不适合我:
boost::thread *currentThread = boost::this_thread;
您可以使用boost::this_线程来引用在中使用它的同一个线程
请参见您必须小心,因为
boost::thread
是一种可移动类型。考虑以下事项:
boost::thread
make_thread()
{
boost::thread thread([](boost::thread* p)
{
// here p points to the thread object we started from
}, &thread);
return thread;
}
// ...
boost::thread t = make_thread();
// if the thread is running by this point, p points to an non-existent object
一个
boost::thread
对象在概念上与一个线程关联,但在规范上与之不关联,即在线程过程中,可能有多个线程对象与之关联(在给定的时间不超过一个)。这就是boost::thread::id
出现在这里的部分原因。那么,您到底想要实现什么呢?如果您浏览完整的Boost线程文档(,或者如果第一个链接被破坏),您会发现没有提供任何函数来获取表示当前线程的Boost::Thread
对象的指针
然而,你可以自己解决这个问题;一种解决方案是使用映射,将boost::thread:id
s映射到boost:thread*
s,然后从线程中访问该映射以获取指针
例如:
#include <cstdio>
#include <map>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
std::map<boost::thread::id, boost::thread*> threadsMap;
boost::mutex threadsMapMutex; // to synchronize access to the map
boost::mutex stdoutMutex; // to synchronize access to stdout
void thread_function()
{
threadsMapMutex.lock();
// get a pointer to this thread
boost::thread::id id = boost::this_thread::get_id();
boost::thread* thisThread = threadsMap.find(id)->second;
threadsMapMutex.unlock();
// do whatever it is that you need to do with the pointer
if(thisThread != NULL)
{
stdoutMutex.lock();
printf("I have a pointer to my own thread!\n");
stdoutMutex.unlock();
}
}
int main()
{
threadsMapMutex.lock();
// create the threads
boost::thread thread1(&thread_function);
boost::thread thread2(&thread_function);
// insert the threads into the map
threadsMap.insert(std::pair<boost::thread::id, boost::thread*>(thread1.get_id(), &thread1));
threadsMap.insert(std::pair<boost::thread::id, boost::thread*>(thread2.get_id(), &thread2));
threadsMapMutex.unlock();
// join the threads
thread1.join();
thread2.join();
return 0;
}
#包括
#包括
#包括
#包括
std::map threadsMap;
boost::mutex threadsMapMutex;//同步对地图的访问
boost::mutex stdoutMutex;//同步对标准输出的访问
void thread_函数()
{
threadsMapMutex.lock();
//获取指向此线程的指针
boost::thread::id=boost::this_thread::get_id();
boost::thread*thisThread=threadsMap.find(id)->秒;
threadsMapMutex.unlock();
//对指针执行任何需要执行的操作
if(thisThread!=NULL)
{
stdoutMutex.lock();
printf(“我有一个指向我自己线程的指针!\n”);
stdoutMutex.unlock();
}
}
int main()
{
threadsMapMutex.lock();
//创建线程
boost::线程thread1(&thread_函数);
boost::线程thread2(&thread_函数);
//将线程插入到映射中
threadsMap.insert(std::pair(thread1.get_id(),&thread1));
threadsMap.insert(std::pair(thread2.get_id(),&thread2));
threadsMapMutex.unlock();
//连接线程
thread1.join();
螺纹2.连接();
返回0;
}
另外,我刚刚注意到,在写了这篇文章之后,你实际上正在使用这个解决方案。哦,好吧——我仍然觉得正式发布您问题的答案以及(工作)潜在解决方案的示例代码非常有用和完整。因此,在某些函数中,我将编写
boost::thread*curentThread=boost::this_thread
?不完全是。。。它实际上是一个名称空间,包含作用于“此线程”的函数。您要做的是从该名称空间调用函数。例如,您可以使用boost::this_thread::get_ID()获取“this thread”的ID,它会为“this thread”返回一个boost::thread::ID的对象。。。我最终得到了boost::thread::ids到boost::thread*的映射。顺便问一下,是否可以仅通过id从另一个线程中删除(停止\中断)线程?@Kiss No。如果您正确维护映射(即指向boost::thread
的指针始终有效,并且可能指向当前与id
关联的线程),则执行映射[the\u id]是正确的->中断()
。不过,你本可以使用适当的沟通机制。