pthread_create创建的线程与内核线程相同吗? [英] The thread created by pthread_create the same with the kernel thread?

查看:490
本文介绍了pthread_create创建的线程与内核线程相同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下命令查看系统允许的最大线程数:

I use the command below to view the maximum number of threads my system allows:

# cat /proc/sys/kernel/threads-max

电话号码是772432.

And the number is 772432.

但是,我使用下面的代码创建了100万个线程.而且有效.

However, I use the code below to create 1 million threads. And it works.

#include <pthread.h>
#include <stdio.h>

static unsigned long long thread_nr = 0;

pthread_mutex_t mutex_;

void* inc_thread_nr(void* arg) {
    /* int arr[1024][1024]; */
    (void*)arg;
    pthread_mutex_lock(&mutex_);
    thread_nr ++;
    pthread_mutex_unlock(&mutex_);
}

int main(int argc, char *argv[])
{
    int err;
    int cnt = 0;

    pthread_mutex_init(&mutex_, NULL);

    while (cnt < 1000000) {
        pthread_t pid;
        err = pthread_create(&pid, NULL, (void*)inc_thread_nr, NULL);
        if (err != 0) {
            break;
        }
        pthread_join(pid, NULL);
        cnt++;
    }

    pthread_mutex_destroy(&mutex_);
    printf("Maximum number of threads per process is = %d\n", thread_nr);
}

输出为:

Maximum number of threads per process is = 1000000

,它大于最大线程数.这是什么原因呢?并且pthread_create创建的线程与内核线程是否相同?

which is larger than the maximum number of threads. What is the reason for this? And is the thread create by pthread_create the same with the kernel thread?

我的操作系统是Fedora 16,具有12个内核,48G RAM.

My OS is Fedora 16, with 12 cores, 48G RAM.

推荐答案

您不应使用%d长时间打印未签名的内容.

You should not print your unsigned long long with %d.

使用

printf("... %llu\n", thread_nr);

相反.

这篇关于pthread_create创建的线程与内核线程相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆