簡體   English   中英

C - socketserver:pthread_create - 無法創建線程 errno 12

[英]C - socketserver: pthread_create - Failed to create thread errno 12

我在 C 程序中為每個傳入的客戶端連接創建線程。 在使用 400 個並行連接進行壓力測試時,我從 pthread_create 收到錯誤 12。 我知道在資源耗盡時可能會發生這種情況,但在連接數很少的情況下不應該出現這種情況。那為什么呢?

 #define MAX_CONNECTIONS 1000

pthread_t tid[MAX_CONNECTIONS];
        int i = 0;


        while(1) {

                        //Accept call creates a new socket for the incoming connection
                         addr_size = sizeof serverStorage;

                         // allocate socketfd on the heap to avoid nasty race conditions
                         int * socketfd;
                         socketfd = malloc(sizeof(int));

                         *socketfd = accept(listenfd, (struct sockaddr *) &serverStorage, &addr_size);

                        //create a thread for each client request and assign the client request to it for processing
                        //the main thread can now serve the next request
                        int err = pthread_create(&tid[i], NULL, socketThread, socketfd);
                        if(err) {
                                slog_error(0,"Failed to create thread %d",err);
                                exit(3);
                        }

                        if( i >= MAX_CONNECTIONS)
                        {
                                i = 0;
                                while( i < MAX_CONNECTIONS )
                                {
                                        pthread_join(tid[i++],NULL);
                                }
                                i = 0;
                        }


 

打開部分線程

 
                   if(injector) {
                        DEBUG slog_debug(0, "injector reached");
                        DEBUG slog_debug(0,"Starting injector helper %s",injector_command);
                        errno = 0;
                        if ( (pop = popen(injector_command, "w") ) == NULL ) {
                        slog_error(0,"popen injector FAILED continue without injection %d",errno);
                        pop = 0;
                        }
                        if (pop) {
                                DEBUG slog_debug(0, "print to injector reached");
                                if( (bytes = fwrite(nm_data,1,strlen(nm_data),pop)) != strlen(nm_data)) {
                                        slog_error(0,"Failed to write to injector %s,%s,%d",hostname,bytes,errno);
                                        pop = (FILE *)0; /* stop writing */
                                        exit(1);
                                }
                        }
                        fflush(pop);
                        pclose(pop);

                    }
                    

pthread_create(&tid[i], NULL, socketThread, &socketfd) 創建了一個討厭的競爭條件。 如果在您創建的線程取消引用地址以獲取其套接字之前對 accept() 的另一個調用返回,您將遇到真正的問題。 請參閱初始問題中的更新代碼。

暫無
暫無

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

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