[英]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.