• 4485阅读
  • 7回复

在A线程里开了一个B线程,但是两个线程在new时parent==0,那么,线程A销毁时,线程B会被销毁吗? [复制链接]

上一主题 下一主题
离线iiiyyyhhhsss
 

只看楼主 倒序阅读 楼主  发表于: 2010-05-05
两个线程是否相互独立,互不影响的?
离线xtfllbl

只看该作者 1楼 发表于: 2010-05-05
不独立,b会被销毁。
[ 此帖被xtfllbl在2010-05-06 16:17重新编辑 ]
上海欢迎您
离线iiiyyyhhhsss

只看该作者 2楼 发表于: 2010-05-05
但是,我的parent元素已经设为零了,它们之间还有关系?
有什么办法让,它们独立?
离线xtfllbl

只看该作者 3楼 发表于: 2010-05-05
主进程中分别开a,b,就独立了。
[ 此帖被xtfllbl在2010-05-06 16:17重新编辑 ]
上海欢迎您
离线iiiyyyhhhsss

只看该作者 4楼 发表于: 2010-05-05
下面是我用标准C库里的pthread实现的。
发现:
线程A退出销毁时,并没有销毁线程B。

这个又该如何解释?

编译指令:  g++ -o mthread mthread.cpp -l pthread

mthread.cpp如下:
using namespace std;
#include <iostream>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

int run_A=0;
int run_B=100;

void *thread_function_B(void *arg){
while(run_B<1000)
{
    cout<<"run_B=="<<run_B++<<endl;
    sleep(2);
}
pthread_exit(NULL);
}

void *thread_function_A(void *arg)
{
pthread_t b_thread;
pthread_create(&b_thread,NULL,thread_function_B,NULL);
//pthread_join(b_thread,NULL);
while(run_A<5)
{
    cout<<"run_A=="<<run_A++<<endl;;
    sleep(2);
}
pthread_exit(NULL);
}

int main()
{
pthread_t a_thread;
pthread_create(&a_thread,NULL,thread_function_A,NULL);   /*创建一个进程*/
pthread_join(a_thread,NULL);  //*等待子线程结束*/

while(run_A<1000)
{
   cout<<"run_A=============="<<run_A++<<endl;
   sleep(2);
}
exit(0);
}
离线xtfllbl

只看该作者 5楼 发表于: 2010-05-05
也许我想当然了,你可以把上面的代码用qthread实现下看看。
[ 此帖被xtfllbl在2010-05-06 16:16重新编辑 ]
上海欢迎您
离线paranoid
只看该作者 6楼 发表于: 2010-05-05
QThread::~QThread ()
Destroys the thread.
Note that deleting a QThread object will not stop the execution of the thread it represents.
离线beajisong

只看该作者 7楼 发表于: 2010-05-06
独立,互不干扰
快速回复
限100 字节
 
上一个 下一个