由于许多进程,pthread_create 的错误返回代码是 35 错误,我使用了 pthread_exit ,它应该杀死线程,不是吗?

发布于 2024-12-07 15:42:55 字数 1335 浏览 0 评论 0原文

我在 pthread_create 中创建了一个 pthread_create,我使用了套接字编程,在其中接收一个数据包,然后创建一个线程来写入文件。当我发送一个非常大的文件时,我收到此错误......?

代码如下...

void *writePack(void *sock)
{

    size_t nbyte;
    ssize_t writeSize;
    nbyte = 1466;
    off_t offset;
    offset = (((struct writePacket *)sock)->seq * 1466);
    char* buffer = new char();
    buffer = ((struct writePacket *)sock)->datamsg;
    writeSize = pwrite(((struct writePacket *)sock)->pp,(const void *)buffer, nbyte, offset );
    free(buffer);
    pthread_exit(NULL);
}

这是父接收代码的代码...

recvfrom(sockA->sockid, (void *)&recvdata, sizeof(struct data), 0, (struct sockaddr *) &cli_addr, &clilen);
        if (n<0)
            error("Error on reading");

        pthread_mutex_lock(&qlock);
                struct writePacket* a;
        a=new writePacket;
        a->sockID = sockA->sockid;
        a->pp = sockA->pp;
        a->seq = recvdata.seq;
        memcpy(a->datamsg,recvdata.datamsg,1466);

        pc = pthread_create(&write[counter], NULL, writePack,(void *) a);
                     if (pc)
                        {
                           printf("ERROR; return code from pthread_create() is %d\n", pc);
                             exit(-1);
                        }

I have create a pthread_create inside a pthread_create, I have used socket programming, where I receive a packet and then create a thread which does the writing to the file. When I send a very large file, i get this error...??

The code is as follows...

void *writePack(void *sock)
{

    size_t nbyte;
    ssize_t writeSize;
    nbyte = 1466;
    off_t offset;
    offset = (((struct writePacket *)sock)->seq * 1466);
    char* buffer = new char();
    buffer = ((struct writePacket *)sock)->datamsg;
    writeSize = pwrite(((struct writePacket *)sock)->pp,(const void *)buffer, nbyte, offset );
    free(buffer);
    pthread_exit(NULL);
}

this is the code of the parent receive code...

recvfrom(sockA->sockid, (void *)&recvdata, sizeof(struct data), 0, (struct sockaddr *) &cli_addr, &clilen);
        if (n<0)
            error("Error on reading");

        pthread_mutex_lock(&qlock);
                struct writePacket* a;
        a=new writePacket;
        a->sockID = sockA->sockid;
        a->pp = sockA->pp;
        a->seq = recvdata.seq;
        memcpy(a->datamsg,recvdata.datamsg,1466);

        pc = pthread_create(&write[counter], NULL, writePack,(void *) a);
                     if (pc)
                        {
                           printf("ERROR; return code from pthread_create() is %d\n", pc);
                             exit(-1);
                        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独守阴晴ぅ圆缺 2024-12-14 15:42:55

可能是您创建的线程没有机会执行,而您一直在创建越来越多的线程从套接字读取数据。

相反,创建一个从套接字读取数据的线程,另一个执行写入操作的线程,并在两个线程之间传递消息中的数据。

It could be that the threads you create didn't get a chance to execute, while you've been creating more and more threads reading from the socket.

Instead, create a thread that would do the reading from the socket, another thread that would do the writing, and pass the data in messages between the two threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文