Pthread冻结标准输出
Pthread freezes stdout?
假设c++程序中有两个线程(pthread):
-
main thread
-
child thread
程序的功能很简单:
- 将两个线程绑定到两个不同的内核。
- 将两个线程的优先级设置为一些非常高的值(
child thread
为-99,main thread
为-98)。 -
child thread
正在执行一些使用100% CPU的繁重任务。 -
child thread
创建后,main thread
正在尝试调用printf()
问题是,一旦创建了子线程,它就会冻结标准输出,并且不再在控制台上打印任何内容。但是,当程序退出时,所有消息突然显示在控制台中。下面是一个.cpp文件来演示这个效果:
main.cpp:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/mman.h>
bool EXIT = false;
void signal_handler(int signal){
EXIT = true;
}
void *child_thread(void *x_args){
printf("Setting child thread CPU affinity (Core #1)...n");
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(1, &cpuset);
if (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset)){
perror("Cannot set child thread CPU affinity");
printf("Exitn");
exit(1);
}
printf("Locking memory of child thread...n");
mlockall(MCL_CURRENT | MCL_FUTURE);
printf("Setting child thread priority (-99)...n");
struct sched_param sched_param;
sched_param.sched_priority = sched_get_priority_max(SCHED_FIFO)-1;
if (sched_setscheduler(0, SCHED_FIFO, &sched_param)){
perror("Cannot set child thread priority");
printf("Exitn");
exit(1);
}
printf("Entering while loop inside child thread...n");
while(!EXIT){}
return NULL;
}
int main(){
signal(SIGINT, signal_handler);
pthread_t thread;
printf("Setting main thread CPU affinity (Core #0)...n");
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(0, &cpuset);
if (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset)){
perror("Cannot set main thread CPU affinity");
printf("Exit.n");
exit(1);
}
printf("Locking memory of main thread...n");
mlockall(MCL_CURRENT | MCL_FUTURE);
printf("Setting main thread priority (-98)...n");
struct sched_param sched_param;
sched_param.sched_priority = sched_get_priority_max(SCHED_FIFO)-2;
if (sched_setscheduler(0, SCHED_FIFO, &sched_param)){
perror("Cannot set main thread priority");
printf("Exit.n");
exit(1);
}
printf("Creating child thread...n");
if (pthread_create(&thread, NULL, child_thread, NULL)){
perror("Cannot create child thread");
printf("Exit.n");
exit(1);
}
printf("Entering while loop in main thread...n");
while(!EXIT){
sleep(1);
printf("I can't see this until I press Ctrl+C!n");
}
pthread_join(thread, NULL);
printf("Exit.n");
return 0;
}
你可以编译它:
g++ main.cpp -pthread -o main
然后运行:
sudo ./main
那么您应该看到stdout在输出以下内容后冻结:
Setting main thread CPU affinity (Core #0)...
Locking memory of main thread...
Setting main thread priority (-98)...
Creating child thread...
Entering while loop in main thread...
Setting child thread CPU affinity (Core #1)...
即使过了一个小时,你也不会看到任何输出。但是当按下Ctrl+C
时。您将看到所有消息输出:
I can't see this until I press Ctrl+C!
I can't see this until I press Ctrl+C!
I can't see this until I press Ctrl+C!
I can't see this until I press Ctrl+C!
I can't see this until I press Ctrl+C!
I can't see this until I press Ctrl+C!
I can't see this until I press Ctrl+C!
I can't see this until I press Ctrl+C!
main thread
实际上是在后台运行的,因为如果你注释掉while循环中的两行(sleep和printf),你可以看到它也使用了100%的CPU。但ht
我在这里错过了什么?
我不会声称自己是专家,但您似乎有一个单一的资源,标准输出和两个线程试图使用它。我相信printf是线程安全的,但不是可重用的。我的第一反应是在对printf和stdout的访问中使用某种线程安全锁,以确保一次只有一个线程调用它。
相关文章:
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 使用提升过程获取 shell 命令的标准输出
- 将标准输出重定向到ostream
- 如何在C++中执行命令并获取命令的返回代码标准输出和标准
- 在程序执行期间从标准输出重定向到自定义流
- 标准输出C++意外行为
- 防止 qDebug() 写入标准输出
- C++ - <<运算符重载,链表 - 地址而不是标准输出
- 有没有办法知道C++中将打印到标准输出的大小?
- 最后推迟标准输出
- 得到~对标准输出没有回应~在黑客排名问题上
- Win32 GUI C(++) 应用将标准输出和标准输出重定向到磁盘上的同一文件
- 在标准输出中执行打印会导致数组中随机分配值
- 捕获远程进程子进程的标准输出
- 使用 QProcess 读取标准输出
- 如何将值发送到标准输出流并在另一个程序中访问这些值
- 无法使用创建进程重定向标准输出
- HackerRank说~对标准输出没有回应~.C++
- 记录标准输入和标准输出
- Pthread冻结标准输出