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()
|