来自不同进程的线程可以具有相同的ID吗?

时间:2018-10-28 08:56:10

标签: c++ multithreading process

我正在编写一个程序,我想在其中创建多个进程以及每个进程的多个线程。简而言之,我的程序创建了多个进程,每个进程创建了多个线程。

这是代码的简短片段:

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吗?奇怪,每个线程的标识符应该唯一,对吧?

2 个答案:

答案 0 :(得分:0)

这些线程ID看起来更像地址。 this_thread::get_id()返回的值不必与系统“线程ID”相同。它主要用于提供字符串比较/作为关联容器中线程的键的目的。您可能需要尝试致电gettidGetCurrentThreadId。请注意,在Windows “直到线程终止之前,线程标识符将唯一地标识整个系统中的线程。” ,因此来自不同进程的线程不能具有相同的id。在其他平台上,行为可能有所不同。

答案 1 :(得分:0)

  

您可能想尝试调用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 ++文档,将会看到函数说明如何指示它返回线程标识符。