简体   繁体   English

是否可以在没有pthread_join()的情况下使用pthreads?

[英]Is it possible to use pthreads without pthread_join()?

What I've noticed recently in attempting to add some multithreaded functionality to some code of mine for a project at work is that pthreads are a huge pain in the ass to deal with logistically... 最近我注意到在尝试为工作项目的某些代码添加一些多线程功能时,pthreads在后勤处理方面是一个巨大的痛苦......

Here's the scenario... 这是场景......

I have an infinite loop in my main method (a server) spawning threads to deal with data whenever it receives a packet from whatever client. 我的main方法(服务器)中有一个无限循环,它产生线程来处理来自任何客户端的数据包的数据。 The problem is that I can't get the threads to execute concurrently at all. 问题是我无法让线程同时执行。 They refuse to begin execution until a call to pthread_join() from the main method, which completely kills the whole purpose of using threads in the first place (server needs to STOP execution flow, and wait for the thread to finish processing its data before receiving anymore packets! ridiculous.) 他们拒绝开始执行,直到从main方法调用pthread_join(),这完全杀死了首先使用线程的整个目的(服务器需要停止执行流程,并等待线程在接收之前完成处理数据)再包了!太荒谬了。)

So is there a way to use pthreads and have them actually be multithreaded? 那么有没有办法使用pthreads并让它们实际上是多线程的? Or am I better off not using threads at all, and saving the extra resources by stopping execution in my server to call a function to process data? 或者我最好不要使用线程,并通过停止在我的服务器中执行来调用函数来处理数据来节省额外的资源?

I'm thinking I may have to resort to forking everytime... 我想我可能每次都要诉诸...

This is frustrating.... 这令人沮丧......

some sample code i did is below: 我做的一些示例代码如下:

// gcc threads.c -lpthread
#include <stdio.h>
#include <pthread.h>

struct point{
    int x, y;
};

static void *print_point(void *point_p);

int main() {
    pthread_t tid;
    struct point pt = {3, 5};
    printf("enter main\n");
    pthread_create(&tid, NULL, print_point, &pt);
    while(1){continue;}
    return 0;
}

static void *print_point(void *point_p) {
    struct point arg = * (struct point *) point_p;
    printf("Point: (%d, %d)\n", arg.x, arg.y);
    return NULL;
}

when I run and compile that (yes i compile with the -lpthread switch), it prints "enter main" and doesn't execute the thread... I even let it run for a while (got up, went to the bathroom, ate some food), and still nothing. 当我运行并编译时(是的,我用-lpthread开关编译),它打印“输入主”并且不执行线程...我甚至让它运行了一段时间(起床,去了浴室,吃了一些食物),但仍然没有。

So since the main method spawns a thread then loops infinitely, the thread should eventually execute... right? 因此,由于main方法产生一个线程然后无限循环,所以线程最终应该执行......对吗? From what I can tell from my tests the main method never gives up execution to the thread it spawned. 从我从测试中可以看出,main方法永远不会放弃执行它产生的线程。 The only way I can get it to give it up it by calling join (but that defeats the purpose of having threads since main will wait around until the thread is done). 唯一的方法是我可以让它通过调用join来放弃它(但是因为main会等到线程完成后才会失败,因为它会有一些线程。)

You're never giving the thread a chance to execute with that while(1){continue;} . while(1){continue;}你永远不会让线程有机会执行它。 One of two things will happen here. 这里将发生两件事之一。

  1. You've compiled with high enough optimization that the compiler makes that entire loop vanish. 你已经编译了足够高的优化,编译器使整个循环消失。 The thread never gets a chance to execute because main starts the thread and then immediately returns zero. 线程永远不会有机会执行,因为main启动线程然后立即返回零。
  2. The compiler doesn't optimize the loop away. 编译器不会优化循环。 With this busy loop you once again are not giving the thread mechanism a chance to slip in. 有了这个繁忙的循环,你再次没有给线程机制一个机会。

Add a sleep (0); 加一个sleep (0); call to the body of that busy loop. 呼叫那个繁忙循环的主体。

Actually your code works fine for me, but I think your problem is that the main thread is sitting in that while() loop, hogging all the CPU usage, so the second thread never gets a chance. 实际上你的代码对我来说运行正常,但我认为你的问题是主线程坐在while()循环中,占用了所有的CPU使用率,所以第二个线程永远不会有机会。 The fact that pthread_join makes it work is a bit of a red herring: it's just stopping the main thread so the other threads get a chance. pthread_join使其工作的事实是一个红色的鲱鱼:它只是停止主线程,所以其他线程有机会。

Obviously the right fix for this is to make the main thread sleep properly when it has nothing to do. 显然,正确的解决方法是让主线程在没有任何关系时正常睡眠。 For your test code, try putting sleep(1) in your while loop. 对于测试代码,请尝试将sleep(1)放入while循环中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM