由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一直没有很好理解thread join itself,哪位解惑一下 (转载)
相关主题
[合集] 为什么多个线程生成的随机数是一样的?如何强制thread和cpu绑定?
在main()里面创建了几个线程,如何等待所有线程都结束?有专门介绍multithreading编程的书吗
A helloworld OpenMP question?多线程 编程,process 和 thread 的一些问题。
Windows Thread APIhow to debug multi-thread program?
关于valgrind 的一个问题怎么 kill 一个 thread 啊
重复利用threads的问题关于thread的stack
pthread and C++how many ways in C++ to release a mutex?
Linux thread和NPTL thread什么关系? (转载)pthread on windows?
相关话题的讨论汇总
话题: thread话题: t1话题: join话题: threads话题: current
进入Programming版参与讨论
1 (共1页)
s********k
发帖数: 6180
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: silverhawk (silverhawk), 信区: JobHunting
标 题: 一直没有很好理解thread join itself,哪位解惑一下
发信站: BBS 未名空间站 (Wed May 2 20:30:51 2012, 美东)
thread join目的是block调用thread知道特定thread结束。那thread join itself是为
什么?难道自己block 自己?
下面是一个简单例子伪代码(几乎所有编程语言都支持)
t1=threadstart();
t2=threadstart();
t1.join()
t2.join()
这里面我首先开始thread t1,然后我调用t1.join,那意思是t1 被block?直到t1自己
运行完?不是太理解这个意思,哪位解惑一下?
t****t
发帖数: 6806
2
t1.join() means block CURRENT thread, until t1 ends. current thread is not
t1. in your example, you have 3 threads: current, t1, and t2.

【在 s********k 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: silverhawk (silverhawk), 信区: JobHunting
: 标 题: 一直没有很好理解thread join itself,哪位解惑一下
: 发信站: BBS 未名空间站 (Wed May 2 20:30:51 2012, 美东)
: thread join目的是block调用thread知道特定thread结束。那thread join itself是为
: 什么?难道自己block 自己?
: 下面是一个简单例子伪代码(几乎所有编程语言都支持)
: t1=threadstart();
: t2=threadstart();
: t1.join()

s********k
发帖数: 6180
3
谢谢,current thread可以是t1吗?这个current thread指的是正在运行的thread?

【在 t****t 的大作中提到】
: t1.join() means block CURRENT thread, until t1 ends. current thread is not
: t1. in your example, you have 3 threads: current, t1, and t2.

t****t
发帖数: 6806
4
no, when i said "current" thread, means the thread executing the code
snippet you provide. it starts t1 and t2 and waits for t1 and t2. it can
never be t1 or t2.

【在 s********k 的大作中提到】
: 谢谢,current thread可以是t1吗?这个current thread指的是正在运行的thread?
s********k
发帖数: 6180
5
如果没有join,那么会出现什么情况呢?

【在 t****t 的大作中提到】
: no, when i said "current" thread, means the thread executing the code
: snippet you provide. it starts t1 and t2 and waits for t1 and t2. it can
: never be t1 or t2.

s********k
发帖数: 6180
6
void create_test_threads()
{
int i = 0;
void* ret = NULL;
pthread_t ids[THREADS_NR] = {0};
for(i = 0; i < THREADS_NR; i++)
{
pthread_create(ids + i, NULL, start_routine, &i);
pthread_join(ids[i], &ret);
}
return ;
}
int main(int argc, char* argv[])
{
create_test_threads();
return 0;
}
这样的例子,是不是pthread_join阻塞了create_test_threads执行,实际上没有做多
线程而是单线程?

【在 t****t 的大作中提到】
: no, when i said "current" thread, means the thread executing the code
: snippet you provide. it starts t1 and t2 and waits for t1 and t2. it can
: never be t1 or t2.

a9
发帖数: 21638
7
资源没有被释放呗。

【在 s********k 的大作中提到】
: 如果没有join,那么会出现什么情况呢?
a9
发帖数: 21638
8
双线程啊。

【在 s********k 的大作中提到】
: void create_test_threads()
: {
: int i = 0;
: void* ret = NULL;
: pthread_t ids[THREADS_NR] = {0};
: for(i = 0; i < THREADS_NR; i++)
: {
: pthread_create(ids + i, NULL, start_routine, &i);
: pthread_join(ids[i], &ret);
: }

t****t
发帖数: 6806
9
well, actually the creater thread is blocked immediately. so yes,
effectively it's single-threaded.

【在 a9 的大作中提到】
: 双线程啊。
a9
发帖数: 21638
10
呃,还真是。

【在 t****t 的大作中提到】
: well, actually the creater thread is blocked immediately. so yes,
: effectively it's single-threaded.

n******t
发帖数: 4406
11
如果这个是用C写的,我觉得你不会问这个问题。

【在 s********k 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: silverhawk (silverhawk), 信区: JobHunting
: 标 题: 一直没有很好理解thread join itself,哪位解惑一下
: 发信站: BBS 未名空间站 (Wed May 2 20:30:51 2012, 美东)
: thread join目的是block调用thread知道特定thread结束。那thread join itself是为
: 什么?难道自己block 自己?
: 下面是一个简单例子伪代码(几乎所有编程语言都支持)
: t1=threadstart();
: t2=threadstart();
: t1.join()

1 (共1页)
进入Programming版参与讨论
相关主题
pthread on windows?关于valgrind 的一个问题
C++11 native thread问题重复利用threads的问题
请问,什么叫Understanding of multi-threaded development environments?pthread and C++
c++11 std thread类 怎么样,大家用过么Linux thread和NPTL thread什么关系? (转载)
[合集] 为什么多个线程生成的随机数是一样的?如何强制thread和cpu绑定?
在main()里面创建了几个线程,如何等待所有线程都结束?有专门介绍multithreading编程的书吗
A helloworld OpenMP question?多线程 编程,process 和 thread 的一些问题。
Windows Thread APIhow to debug multi-thread program?
相关话题的讨论汇总
话题: thread话题: t1话题: join话题: threads话题: current