簡體   English   中英

pthread_create創建的線程是否與內核線程相同?

[英]The thread created by pthread_create the same with the kernel thread?

我使用以下命令查看系統允許的最大線程數:

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

號碼是772432。

但是,我使用下面的代碼創建100萬個線程。 而且有效。

#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創建的線程與內核線程是否相同?

我的操作系統是Fedora 16,具有12核,48G RAM。

您不應該用%d長時間打印未簽名的內容。

采用

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

代替。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM