作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
线程创建绝对奇怪的情况,这里是测试代码:
#include <stdint.h> /* C99 types */
#include <stdbool.h> /* bool type */
#include <stdio.h> /* printf, fprintf, snprintf, fopen, fputs */
#include <string.h> /* memset */
#include <signal.h> /* sigaction */
#include <time.h> /* time, clock_gettime, strftime, gmtime */
#include <sys/time.h> /* timeval */
#include <unistd.h> /* getopt, access */
#include <stdlib.h> /* atoi, exit */
#include <errno.h> /* error messages */
#include <math.h> /* modf */
#include <assert.h>
#include <sys/socket.h> /* socket specific definitions */
#include <netinet/in.h> /* INET constants and stuff */
#include <arpa/inet.h> /* IP address conversion stuff */
#include <netdb.h> /* gai_strerror */
#include <pthread.h>
void thread_up(void) {
printf("thread ....\n");
int loop=0;
while(loop<=7)
{
printf("loop...\n");
sleep(10);
loop++;
}
printf("Exit loop\n");
}
int main()
{
pthread_t thrid_up=0;
int i = pthread_create( &thrid_up, NULL, (void * (*)(void *))thread_up, NULL);
if (i != 0) {
printf("ERROR: [main] impossible to create upstream thread\n");
exit(1);
}
while(1)
{
sleep(10);
}
}
该代码在 ps 命令上产生以下输出(在 ./test& 启动之后):
30214 root 704 S ./test
30215 root 704 S ./test
30216 root 704 S ./test
有什么提示吗?
最佳答案
只是一个注释。 man 7 pthreads
LinuxThreads
The notable features of this implementation are the following:
- In addition to the main (initial) thread, and the threads that the program creates using
pthread_create(3)
, the implementation creates a "manager" thread. This thread handles thread creation and termination. (Problems can result if this thread is inadvertently killed.)- Threads do not share process IDs. (In effect, LinuxThreads threads are implemented as processes which share more information than usual, but which do not share a common process ID.) LinuxThreads threads (including the manager thread) are visible as separate processes using
ps(1)
.
不管这个文档是关于 Glibc 的,你的 uClibc 版本可能使用 LinuxThreads 作为 pthread“后端”。因此从技术上讲,您的代码可能会创建三个调度实体。
但首先您确实必须确保 ps
打印线程,而不仅仅是进程,并仔细检查在探测期间是否只有一个已启动的程序实例。
附言您还应该检查您的 uClibc 是否真的使用 LinuxThreads 而不是 NPTL。因此 IMO 观察到三个线程的机会是存在的,但它非常小。
关于c - Linux 2.6.30 uClibc 0.9.29 pthread_create 两个线程只安装一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38720979/
我是一名优秀的程序员,十分优秀!