return()对pthread_exit()在pthread启动函数 [英] return() versus pthread_exit() in pthread start functions

查看:144
本文介绍了return()对pthread_exit()在pthread启动函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序显示,我们可以使用return()或pthread_exit()返回一个可用于pthread_join()状态变量的void *变量。



(1)是否应该优先使用另一个?



(2)为什么使用return()工作?通常我们认为返回一个值在堆栈,但由于线程完成堆栈应该消失。或者堆栈不会被销毁,直到pthread_join()?



(3)在你的工作,你看到很多使用的状态变量?看来90%的代码我看到刚刚NULL状态参数。因为通过void * ptr改变的任何东西已经反映在调用线程中,似乎没有多少点返回它。任何新的void * ptr返回的都必须指向由开始线程malloced的东西,这使得接收线程负责处理它。我认为状态变量是半无意义的,我错了吗?

  #include< iostream> 
#include< pthread.h>

using namespace std;

struct taskdata
{
int x;
float y;
string z;
};


void * task1(void * data)
{
taskdata * t =(taskdata *)data;

t-> x + = 25;
t-> y - = 4.5;
t-> z =再见;

return(data);
}

void * task2(void * data)
{
taskdata * t =(taskdata *)data

t-> x - = 25;
t-> y + = 4.5;
t-> z =World;

pthread_exit(data);
}


int main(int argc,char * argv [])
{
pthread_t threadID;

taskdata t = {10,10.0,Hello};

void * status;

cout<< before<< t.x<< < t<< < t.z < endl;

//通过return()

pthread_create(& threadID,NULL,task1,(void *)& t);

pthread_join(threadID,& status);

taskdata * ts =(taskdata *)status;

cout<< after task1<< ts - > x< < ts - > y< < ts - > z< endl;

//由pthread_exit()

pthread_create(& threadID,NULL,task2,(void *)& t);

pthread_join(threadID,& status);

ts =(taskdata *)status;

cout<< after task2<< ts - > x< < ts - > y< < ts - > z< endl;

}

输出:



before 10 10 Hello
after task1 35 5.5 Goodbye
after task2 10 10 World


解决方案

(1)在C ++代码中,使用 return 那么 pthread_exit 只能保证调用注册了 pthread_cancel_push()的取消处理程序 。在某些系统上,这种机制也会导致C ++局部变量的析构函数被调用,但这不能保证用于可移植代码---检查你的平台文档。



main() return 将隐式调用 exit(),因此终止程序,而 pthread_exit()只会终止线程,程序将保持运行,直到所有线程都终止或一些线程调用 exit() abort()或另一个终止程序的函数。



(2) return 的使用起作用,因为POSIX规范说。返回的值存储在 pthread_join()可以检索它的位置。在调用 pthread_join()之前,线程使用的资源不会被回收。



在原始POSIX线程中的线程的返回值。然而,我倾向于使用更高级的设施,如Boost线程库,最近的C ++ 0x线程库,它提供了在线程之间传输值的替代方法,如futures,避免与内存管理相关的问题,你引用


The following program shows that we can use return() or pthread_exit() to return a void* variable that is available to pthread_join()'s status variable.

(1) Should there be a preference for using one over the other?

(2) Why does using return() work? Normally we think of return putting a value on the stack but since the thread is completed the stack should vanish. Or does the stack not get destroyed until after pthread_join()?

(3) In your work, do you see much use of the status variable? It seems 90% of the code I see just NULLs out the status parameter. Since anything changed via the void* ptr is already reflected in the calling thread there doesn't seem much point to returning it. Any new void* ptr returned would have to point to something malloc-ed by the start thread, which leaves the receiving thread with the responsibility to dispose of it. Am I wrong in thinking the status variable is semi-pointless?

#include <iostream>
#include <pthread.h>

using namespace std;

struct taskdata
{
       int  x;
     float  y;
    string  z;
};


void* task1(void *data)
{
    taskdata *t = (taskdata *) data;

    t->x += 25;
    t->y -= 4.5;
    t->z = "Goodbye";

    return(data);
}

void* task2(void *data)
{
    taskdata *t = (taskdata *) data;

    t->x -= 25;
    t->y += 4.5;
    t->z = "World";

    pthread_exit(data);
}


int main(int argc, char *argv[])
{
    pthread_t threadID;

    taskdata t = {10, 10.0, "Hello"};

    void *status;

    cout << "before " << t.x << " " << t.y << " " << t.z << endl;

    //by return()

    pthread_create(&threadID, NULL, task1, (void *) &t);

    pthread_join(threadID, &status);

    taskdata *ts = (taskdata *) status;

    cout << "after task1 " << ts->x << " " << ts->y << " " << ts->z << endl;

    //by pthread_exit()

    pthread_create(&threadID, NULL, task2, (void *) &t);

    pthread_join(threadID, &status);

    ts = (taskdata *) status;

    cout << "after task2 " << ts->x << " " << ts->y << " " << ts->z << endl;

}

With output of:

before 10 10 Hello
after task1 35 5.5 Goodbye
after task2 10 10 World

解决方案

(1) In C++ code, using return causes the stack to be unwound and local variables destroyed, whereas pthread_exit is only guaranteed to invoke cancellation handlers registered with pthread_cancel_push(). On some systems this mechanism will also cause the destructors for C++ local variables to be called, but this is not guaranteed for portable code --- check your platform documentation.

Also, in main(), return will implicitly call exit(), and thus terminate the program, whereas pthread_exit() will merely terminate the thread, and the program will remain running until all threads have terminated or some thread calls exit(), abort() or another function that terminates the program.

(2) The use of return works because the POSIX specification says so. The returned value is stored in a place where pthread_join() can retrieve it. The resources used by the thread are not reclaimed until pthread_join() is called.

(3) I never use the return value of a thread in raw POSIX threads. However, I tend to use higher level facilities such as the Boost thread library, and more recently the C++0x thread library, which provide alternative means for transferring values between threads such as futures, which avoid the problems associated with memory management that you allude to.

这篇关于return()对pthread_exit()在pthread启动函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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