[英]Can threads from different processes have the same id?
我正在编写一个程序,其中我想为每个进程创建多个进程和多个线程。 简而言之,我的程序创建了多个进程,每个进程创建了多个线程。
这是代码的简短片段:
void NormalityComponent::newThread(int thread_id){
std::cout << "New Thread [" << thread_id +1 << "] created in Component [" << name_component << "] with id " << std::this_thread::get_id() << std::endl;
timeSimulation();
std::cout << "Thread [" << thread_id +1 << "] in [" << name_component << "] with id " << std::this_thread::get_id() << " ends its simulation" << std::endl;
}
void NormalityComponent::timeSimulation(){
for (int i = 0; i < time_factor * TEMPORAL_PARAMETER; i++);
}
void NormalityComponent::startAnalysisMode3(){
//creation of a new process to simulate the Normality Component Analysis and a thread for each Instance.
pid_t pid = fork();
if (pid == 0){
//Child Process
std::cout << "New Normality Component ["<< name_component <<"] created with id " << getpid() << std::endl;
//Creation of instances
for(int i = 0; i < number_instances;i++){
v_threads.push_back(std::thread(&NormalityComponent::newThread,this, i));
std::this_thread::sleep_for (std::chrono::milliseconds(100));
}
std::for_each(v_threads.begin(), v_threads.end(), std::mem_fn(&std::thread::join));
kill(getpid(), SIGTERM); //it ends the process when the threads finish.
}
}
这是输出的一部分:
New Thread [1] created in Component [Velocity] with id 140236340983552
New Thread [1] created in Component [FaceRecognition] with id 140236340983552
New Thread [1] created in Component [Trajectories] with id 140236340983552
New Thread [2] created in Component [Velocity] with id 140236332590848
New Thread [2] created in Component [Trajectories] with id 140236332590848
New Thread [2] created in Component [FaceRecognition] with id 140236332590848
来自不同进程的线程可以具有相同的ID吗? 很奇怪,每个线程的标识符应该唯一,对吧?
这些线程ID看起来更像地址。 由this_thread::get_id()
返回的值不必与系统“线程ID”相同。 它主要用于提供字符串比较/作为关联容器中线程的键的目的。 您可能想尝试调用gettid
或GetCurrentThreadId
。 请注意,在窗口“直到线程终止之前,线程标识符唯一地标识整个系统中的线程。” 因此来自不同进程的线程不能具有相同的ID。 在其他平台上,行为可能有所不同。
您可能想尝试调用gettid或GetCurrentThreadId
@ VTT,gettid()对我有用。
void NormalityComponent::newThread(int thread_id){
pid_t tid = (pid_t) syscall (SYS_gettid);
std::cout << "New Thread [" << thread_id +1 << "] created in Component [" << name_component << "] with id " << tid << std::endl;
timeSimulation();
std::cout << "Thread [" << thread_id +1 << "] in [" << name_component << "] with id " << tid << " ends its simulation" << std::endl;
}
该解决方案为每个线程提供唯一的标识符。 输出示例如下:
New Normality Component [Trajectories] created with id 5118
New Normality Component [Velocity] created with id 5119
New Thread [1] created in Component [Trajectories] with id 5121
New Normality Component [FaceRecognition] created with id 5120
New Thread [1] created in Component [Velocity] with id 5122
New Thread [1] created in Component [FaceRecognition] with id 5123
New Thread [2] created in Component [Trajectories] with id 5124
New Thread [2] created in Component [Velocity] with id 5125
New Thread [2] created in Component [FaceRecognition] with id 5126
Thread [1] in [Velocity] with id 5122 ends its simulation
New Thread [3] created in Component [Trajectories] with id 5127
New Thread [3] created in Component [FaceRecognition] with id 5128
New Thread [4] created in Component [FaceRecognition] with id 5129
无论如何,我想了解为什么this_thread :: get_id()不返回线程ID。 如果查看c ++文档,您将看到函数说明如何指示其返回线程标识符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.