简体   繁体   English

创建线程(POSIX 线程)

[英]Creating threads (POSIX threads)

I'm trying to know why does the Master executes the printf() first even though the Slave thread comes first in the code?我想知道为什么 Master 首先执行printf()即使 Slave 线程在代码中排在第一位?

Also, why do they have the same PIDs (Master and slave)?另外,为什么它们具有相同的 PID(主从)?

void* pthread_function(int id) {
  printf("[%s] Slave thread %d (PID=%d,TID=%ld)\n",timestamp(),id,getpid(),pthread_self());
  pthread_exit(NULL);
}
int main(int argc,char** argv) {
  // Create slave thread
  long int i = 1;
  pthread_t thread;
  pthread_create(&thread,NULL,(void* (*)(void*))pthread_function,(void*)(i));
  // Print message
  printf("[%s] Master thread (PID=%d,TID=%ld)\n",timestamp(),getpid(),pthread_self());
  // Wait for threads
  pthread_join(thread,NULL);
  // Terminate process OK
  return 0;
} 

and the output was output 是

[2019:11:20 00:25:25:853640] Master thread (PID=5338,TID=140000137201472)
[2019:11:20 00:25:25:853795] Slave thread 1 (PID=5338,TID=140000128689920)

Sorry I'm new to this, the answer might be very simple and i don't know it.对不起,我是新手,答案可能很简单,我不知道。

As this program executed, the order of events was this:当这个程序执行时,事件的顺序是这样的:

  • The main program executed.执行的主程序。
  • It launched the child thread.它启动了子线程。
  • Coincidentally(!) it managed to print its message before...巧合的是(!)它之前设法打印了它的消息......
  • ... the newly-launched child thread printed its message. ...新启动的子线程打印了它的信息。
  • Then the child thread died, and...然后子线程死了,然后......
  • ... having waited for it to die... the main thread did too. ...等待它死去...主线程也死了。

If you ran this program many times, then eventually you just might encounter a case where the child "beat its parent to the punch."如果你多次运行这个程序,那么最终你可能会遇到孩子“打败父母”的情况。 (But this also has to do with the underlying operating-system logic which ensures that outputs to the console always consist of "entire strings." Either of the two processes/threads had an equal chance to get their strings in first, but you'll never see console output consisting of the two strings intermingled. ) (但这也与底层操作系统逻辑有关,它确保控制台的输出始终由“整个字符串”组成。两个进程/线程中的任何一个都有平等的机会首先获得它们的字符串,但是你'永远不会看到控制台 output 由混合的两个字符串组成。

The ordering of statements in the source code is entirely irrelevant.源代码中语句的顺序完全不相关。

Multiple threads are scheduled independently, so the kernel may decide to schedule the new thread or the old one first, depending on its priority and what else is running on the system.多个线程是独立调度的,因此 kernel 可能会决定先调度新线程还是旧线程,具体取决于其优先级和系统上正在运行的其他线程。 Running the program multiple times may result in different results, even.多次运行程序可能会导致不同的结果,甚至。

The reason that the two threads share the same PID is because POSIX says they must.两个线程共享相同 PID 的原因是因为 POSIX 说它们必须。 There is one process, even with multiple threads.即使有多个线程,也只有一个进程。 The kernel internally tracks the two threads with separate PIDs, but glibc exposes a single PID (the PID of the main thread) as the PID of the process. kernel 在内部使用单独的 PID 跟踪两个线程,但 glibc 将单个 PID(主线程的 PID)公开为进程的 PID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM