thread.join?怎样使用C#Thread.Join函数
你是否对于thread.join和怎样使用C#Thread.Join函数感到困惑?别担心,今天小编将为您揭开这个谜团,让我们一同探索吧!
关于Linux 线程pthread_join的用法
Linux系统pthread_join用于挂起当前线程(调用pthread_join的线程),直到thread指定的线程终止运行为止,当前线程才继续执行。
案例代码:
/*******************************************
**Name:pthread_join.c
**用于Linux下多线程学习
**案例解释线程的暂停和结束
**Author:admin
**Date:2015/8/11
**Copyright(c)2015,AllRightsReserved!
**********************************************
#include<pthread.h>
#include<unistd.h>
#include<stdio.h>
void*thread(void*str)
{
inti;
//不调用pthread_join线程函数
for(i=0;i<10;++i)
{
sleep(2);
printf("Thisinthethread:%d\n",i);
}
returnNULL;
}
intmain()
{
pthread_tpth;
inti;
intret=pthread_create(&pth,NULL,thread,(void*)(i));
//调用pthread_join线程函数
pthread_join(pth,NULL);
for(i=0;i<10;++i)
{
sleep(1);
printf("Thisinthemain:%d\n",i);
}
return0;
}
通过Linux下shell命令执行上面的案例代码:
[root@localhostsrc]#gccpthread_join.c-lpthread
[root@localhostsrc]#./a.out
Thisinthemain:0
Thisinthethread:0
Thisinthemain:1
Thisinthemain:2
Thisinthethread:1
Thisinthemain:3
Thisinthemain:4
Thisinthethread:2
Thisinthemain:5
Thisinthemain:6
Thisinthethread:3
Thisinthemain:7
Thisinthemain:8
Thisinthethread:4
Thisinthemain:9
子线程还没有执行完毕,main函数已经退出,那么子线程也就退出了,“pthread_join(pth,NULL);”函数起作用。
[root@localhostsrc]#gccpthread_join.c-lpthread
[root@localhostsrc]#./a.out
Thisinthethread:0
Thisinthethread:1
Thisinthethread:2
Thisinthethread:3
Thisinthethread:4
Thisinthethread:5
Thisinthethread:6
Thisinthethread:7
Thisinthethread:8
Thisinthethread:9
Thisinthemain:0
Thisinthemain:1
Thisinthemain:2
Thisinthemain:3
Thisinthemain:4
Thisinthemain:5
Thisinthemain:6
Thisinthemain:7
Thisinthemain:8
Thisinthemain:9
这说明pthread_join函数的调用者在等待子线程退出后才继续执行。
C++中的std:thread的join函数与detach函数有什么区别
C++中的thread对象通常来说表达了执行的线程(thread of execution),这是一个OS或者平台的概念。
当thread::join()函数被调用后,调用它的线程会被block,直到线程的执行被完成。基本上,这是一种可以用来知道一个线程已结束的机制。当thread::join()返回时,OS的执行的线程已经完成,C++线程对象可以被销毁。
当thread::detach()函数被调用后,执行的线程从线程对象中被分离,已不再被一个线程对象所表达--这是两个独立的事情。C++线程对象可以被销毁,同时OS执行的线程可以继续。如果程序想要知道执行的线程何时结束,就需要一些其它的机制。join()函数在那个thread对象上不能再被调用,因为它已经不再和一个执行的线程相关联。
去销毁一个仍然可以“joinable”的C++线程对象会被认为是一种错误。为了销毁一个C++线程对象,约么join()函数需要被调用(并结束),要么detach()函数被调用。如果一个C++线程对象当销毁时仍然可以被join,异常会被抛出。
C++线程对象不被表达为执行的线程的其它的情况(也就是unjoinable):
默认构造的线程对象不表达为执行的线程,所以是unjoinable。
被移开的线程将不表达为执行的线程,所以是unjoinable。
在std::thread的析构函数中,std::terminate会被调用如果:
线程没有被Joined(用t.join())
线程也没有被detached(用t.detach())
因此,你应该在执行流程到析构函数前总是要么join,要么detach一个线程。
当一个程序终止时(比如main返回),剩下的在后台的detached线程执行不会再等待;相反它们的执行会被挂起并且它们的本地线程对象会被销毁。
关键地,这意味着这些线程的栈不是完好无损的,因此一些析构函数不会被执行。依赖于这些行为,一些析构函数假象会被承担,这可能是一种坏情形,好像程序已经Crash或者已经被kill。希望OS会释放加在这些文件上的锁。Depending
on the actions those destructors were supposed to undertake, this might
be as bad a situation as
if the program had crashed or had been killed. Hopefully the OS will
release the locks on files, etc... but you could have corrupted shared
memory, half-written files, and the like.
所以,你应该使用join还是detached?
使用join
除非你需要更灵活并且想要独立地提供一种同步机制来等待线程完成,在这种情况下你应该使用detach
怎样使用C#Thread.Join函数
MSDN的解释:阻塞调用线程,直到某个线程终止时为止。首先明确几个问题:
1、一个进程由一个或者多个线程组成,线程之间有可能会存在一定的先后关系和互斥关系。多线程编程,首先就是要想办法划分线程,减少线程之间的先后关系和互斥关系,这样才能保证线程之间的独立性,各自工作,不受影响。Google的MapReduce核心思想就是尽量减少线程之间的先后关系和互斥关系。
2、无论如何地想办法,线程之间还是会存在一定的先后关系和互斥关系,这时候可以使用Thread.Join方法。
3、一个线程在执行的过程中,可能调用另一个线程,前者可以称为调用线程,后者成为被调用线程。
4、Thread.Join方法的使用场景:调用线程挂起,等待被调用线程执行完毕后,继续执行。
5、被调用线程执行Join方法,告诉调用线程,你先暂停,我执行完了,你再执行。从而保证了先后关系。
6、考虑一种有意思的情况:在当前线程内调用Thread.CurrentThread.Join()会出现什么情况?分析:假设当前线程为A,此时调用线程为A,被调用线程也为A,由于调用线程A暂停,被调用线程A(也就是调用线程A)永远不会执行完毕,造成死锁。
好了,前面分析完了,现在来看测试用例吧:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading;
namespaceTest
{
classTestThread
{
privatestaticvoidThreadFuncOne()
{
for(inti=0;i<10;i++)
{
Console.WriteLine(Thread.CurrentThread.Name+"i="+i);
}
Console.WriteLine(Thread.CurrentThread.Name+"hasfinished");
}
staticvoidMain(string[]args)
{
Thread.CurrentThread.Name="MainThread";
ThreadnewThread=newThread(newThreadStart(TestThread.ThreadFuncOne));
newThread.Name="NewThread";
for(intj=0;j<20;j++)
{
if(j==10)
{
newThread.Start();
newThread.Join();
}
else
{
Console.WriteLine(Thread.CurrentThread.Name+"j="+j);
}
}
Console.Read();
}
}
}
下面是测试的结果:
结论:从测试中我们可以很清楚的看到MainThread在NewThread.Join被调用后被阻塞,直到NewThread
执行完毕才继续执行。
感谢您的阅读!希望本文对解决您关于thread.join的问题有所帮助。如果您还有其他疑问,欢迎随时向我们提问。