在 pthread_create 中指定的函数中释放参数 [英] freeing argument in function specified in pthread_create

查看:52
本文介绍了在 pthread_create 中指定的函数中释放参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小型服务器,它创建一个新线程来处理每个新连接.我需要使用 pthread_create 的第四个参数将套接字传递给函数.尝试释放用于套接字的内存时,出现段错误.通讯工作正常.我试过传递一个 void* 和一个 void**(转换为 void*,有点丑)

I am writing a small server which creates a new thread to handle each new connection. I need to pass the socket to the function using the fourth argument of pthread_create. When trying to free the memory used for the socket i get a segfault. The communication works fine. I have tried passing a void* and also a void** (casted to void*, kind of ugly)

这是我在尝试解决此问题时使用的最新 cludge,稍后将在响应函数中进行实际工作.

This is the latest cludge i'm using while trying to figure this out, later if will be doing actual work in the respond function.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <pthread.h>

void *respond(void *thread_arg)
{
        void *arg = *(void**)thread_arg;
        printf("responding...\n");
        int client_sock;
        client_sock = (int)arg;

        char *message = "Write smthng to echo!\n\t";
        send(client_sock,message,strlen(message),0);
        char *buf = (char*)malloc(100);
        int ptr = 0;
        char last = ' ';
        while (last != '\n') {
                recv(client_sock,&last,1,0);
                buf[ptr++] = last;
        }
        buf[ptr++] = '\n';
        send(client_sock, buf, ptr, 0);
        ptr = 0;
        free(buf);
        close(client_sock);
        //free(arg); // why segfault?
        //free(*(void**)thread_arg); // the same
        pthread_exit(NULL);
}

int main(int argc, char **argv)
{
        int socket_desc, client_sock, addrlen, tmp;
        struct sockaddr_in address;
        pthread_t *responder_thread;
        void *cs;

        socket_desc = socket(AF_INET, SOCK_STREAM, 0);
        if (socket_desc < 0)
                printf("could not create socket");
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = INADDR_ANY;
        address.sin_port = htons(8080);
        tmp = bind(socket_desc, (struct sockaddr *)&address, sizeof(address));
        if (tmp < 0)
                printf("could nod bind to port!");
        listen(socket_desc, 5);

        addrlen = sizeof(struct sockaddr_in);
        while (true) {
                client_sock = accept(socket_desc, (struct sockaddr *)&address, (socklen_t*)&addrlen);
                if (client_sock < 0) {
                        printf("could not create client socket");
                } else {
                        printf("Accepted connection!\n");
                        cs = malloc(sizeof(int));
                        cs = (void*)client_sock;
                        responder_thread = (pthread_t*)malloc(sizeof(pthread_t*));
                        tmp = pthread_create(responder_thread, NULL, respond, (void*)&cs);
                        //cs = NULL;
                        if (tmp) {
                                printf("pthread_create returned '%d', exiting", tmp);
                                exit(-1);
                        }

                }
        }
        pthread_exit(NULL);
}

最后,澄清一下;我对c非常缺乏经验.:)

Lastly, to clarify; I am very inexperienced when it comes to c. :)

推荐答案

尝试更多类似的方法.

int* cs;
...
cs = (int*)malloc(sizeof(int));
*cs = client_sock;
...
tmp = pthread_create(responder_thread, NULL, respond, (void*)cs);

那你就不需要这个演员表了.

Then you don't need this casting.

void *arg = *(void**)thread_arg;

你可以释放 thread_arg.

and you can just free the thread_arg.

free(thread_arg);

这篇关于在 pthread_create 中指定的函数中释放参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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