c++ 来自不同进程的线程可以有相同的id吗?

ebdffaop  于 2023-03-05  发布在  其他
关注(0)|答案(3)|浏览(331)

我正在编写一个程序,我想在其中创建几个进程,并为每个进程创建几个线程。简而言之,我的程序创建几个进程,并为每个进程创建几个线程。
下面是代码的一个简短片段:

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

fjaof16o

fjaof16o1#

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

bfhwhh0e

bfhwhh0e2#

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

wribegjk

wribegjk3#

linux man page for fork特别提到了forked进程中的线程将与forkee的进程线程具有相同的线程id:

  • 子进程是用一个线程创建的,即调用fork()的线程。

所以是的,多个进程的线程可以(并且经常)具有相同的线程id。

相关问题