创建线程但进程出现?
create thread but process shows up?
#include <unistd.h>
#include <stdio.h>
#include <cstring>
#include <thread>
void test_cpu() {
printf("thread: test_cpu start\n");
int total = 0;
while (1) {
++total;
}
}
void test_mem() {
printf("thread: test_mem start\n");
int step = 20;
int size = 10 * 1024 * 1024; // 10Mb
for (int i = 0; i < step; ++i) {
char* tmp = new char[size];
memset(tmp, i, size);
sleep(1);
}
printf("thread: test_mem done\n");
}
int main(int argc, char** argv) {
std::thread t1(test_cpu);
std::thread t2(test_mem);
t1.join();
t2.join();
return 0;
}
用g++ -o test test.cc --std=c++11 -lpthread
编译
我运行Linux中的程序,运行置顶监控
我希望看到一个过程,但我看到了三个。
看起来std::thread正在创建线程,为什么我最终得到进程?
Linux 没有实现线程。它只有轻量级进程 (LWP),而 pthread 库将它们包装起来以提供 POSIX 兼容的线程接口。主 LWP 创建自己的地址 space,而每个后续线程 LWP 与主 LWP 共享地址 space。
许多实用程序,例如 HTOP(似乎在屏幕截图上)默认列出 LWP。为了隐藏线程 LWP,您可以打开 Setup (F2)
-> Display Options
并检查 Hide kernel threads
和 Hide userland process threads
选项。还有一个突出显示线程的选项 - Display threads in different color
.
#include <unistd.h>
#include <stdio.h>
#include <cstring>
#include <thread>
void test_cpu() {
printf("thread: test_cpu start\n");
int total = 0;
while (1) {
++total;
}
}
void test_mem() {
printf("thread: test_mem start\n");
int step = 20;
int size = 10 * 1024 * 1024; // 10Mb
for (int i = 0; i < step; ++i) {
char* tmp = new char[size];
memset(tmp, i, size);
sleep(1);
}
printf("thread: test_mem done\n");
}
int main(int argc, char** argv) {
std::thread t1(test_cpu);
std::thread t2(test_mem);
t1.join();
t2.join();
return 0;
}
用g++ -o test test.cc --std=c++11 -lpthread
我运行Linux中的程序,运行置顶监控
我希望看到一个过程,但我看到了三个。
看起来std::thread正在创建线程,为什么我最终得到进程?
Linux 没有实现线程。它只有轻量级进程 (LWP),而 pthread 库将它们包装起来以提供 POSIX 兼容的线程接口。主 LWP 创建自己的地址 space,而每个后续线程 LWP 与主 LWP 共享地址 space。
许多实用程序,例如 HTOP(似乎在屏幕截图上)默认列出 LWP。为了隐藏线程 LWP,您可以打开 Setup (F2)
-> Display Options
并检查 Hide kernel threads
和 Hide userland process threads
选项。还有一个突出显示线程的选项 - Display threads in different color
.