-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.cc
More file actions
38 lines (34 loc) · 1.15 KB
/
Copy paththread.cc
File metadata and controls
38 lines (34 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright 2024 JOK Inc. All Rights Reserved.
// Author: easytojoin@163.com (jok)
#include <unistd.h>
#include <iostream>
#include <thread> // NOLINT
void id_shower() {
std::string thread_name;
pthread_setname_np(pthread_self(), "ceshi");
thread_name.resize(17);
pthread_getname_np(pthread_self(), const_cast<char*>(thread_name.c_str()),
16);
std::cout << thread_name << ": ";
pthread_t tid = pthread_self();
auto id = std::this_thread::get_id();
std::cout << getpid() << " " << gettid() << " " << tid << " " << id
<< std::endl;
while (true) {
}
return;
}
int main(int argc, char** argv) {
pthread_setname_np(pthread_self(), "ceshi-main");
char thread_name[100];
pthread_getname_np(pthread_self(), thread_name, 100);
std::cout << thread_name << ": ";
pthread_t tid = pthread_self();
auto id = std::this_thread::get_id();
std::cout << getpid() << " " << gettid() << " " << tid << " " << id << "\n";
std::thread task = std::thread(id_shower);
std::cout << "new thread: " << task.get_id() << std::endl;
task.join();
std::cout << "thread: " << task.get_id() << " over" << std::endl;
return 0;
}