Redian新闻
>
Home Depot 397刀或者以上大电器有10%折扣
avatar
Home Depot 397刀或者以上大电器有10%折扣# Living
n*e
1
You are given a paragraph , which contain n number of words, you are given m
threads. What you need to do is , each thread should print one word and
give the control to next thread, this way each thread will keep on printing
one word , in case last thread come, it should invoke the first thread.
Printing will repeat until all the words are printed in paragraph. Finally
all threads should exit gracefully. What kind of synchronization will use?
I have no idea how to solve this question. Any guru helps me? Thanks.
avatar
z*y
2
还是必须来美国了才能申请?
avatar
s*r
4
message queue
或自己建一个 circular buffer size of m, 每个指向一个 event,
thread 完成自己的操作后向下一个 slot post event,转完一圈后正好可以回到原来的
位置。
avatar
c*3
5
没申请过,不过理论上入境前你都没H4身份,可能没法申请吧
avatar
a*a
6
saw the same at holiday season, so it might be just constant thing
avatar
n*e
7
我觉得是不是这样的。可以建一个 circular buffer size of n(不是m)。然后这个
circular buffer 里面放的是n个word(这样才能知道什么时候把所有的word读完了)
。然后产生m 个event,除了一个thread以外,其他m-1个thread都被events block 住
。每次一个thread 读完一个数据,就设置下一个event 是signal的,这样就能保证每
次都只有一个thread 在读数据.

【在 s********r 的大作中提到】
: message queue
: 或自己建一个 circular buffer size of m, 每个指向一个 event,
: thread 完成自己的操作后向下一个 slot post event,转完一圈后正好可以回到原来的
: 位置。

avatar
G*o
8
OK. Thanks. It is better than having to wait for the 10% OFF coupon, then.

【在 a********a 的大作中提到】
: saw the same at holiday season, so it might be just constant thing
avatar
p*s
9
如何实现 “Finally all threads should exit gracefully.”?

【在 s********r 的大作中提到】
: message queue
: 或自己建一个 circular buffer size of m, 每个指向一个 event,
: thread 完成自己的操作后向下一个 slot post event,转完一圈后正好可以回到原来的
: 位置。

avatar
E*A
10
朋友家换炉子还包安装,今年还没看到不然早就换了
avatar
s*r
11
当剩余的buffer size n_left == 0 时,多转一圈即可

【在 p********s 的大作中提到】
: 如何实现 “Finally all threads should exit gracefully.”?
avatar
G*o
12
问了CSR的美眉,免费送货里面是包安装的。

【在 E*A 的大作中提到】
: 朋友家换炉子还包安装,今年还没看到不然早就换了
avatar
G*A
13
+1

【在 n****e 的大作中提到】
: 我觉得是不是这样的。可以建一个 circular buffer size of n(不是m)。然后这个
: circular buffer 里面放的是n个word(这样才能知道什么时候把所有的word读完了)
: 。然后产生m 个event,除了一个thread以外,其他m-1个thread都被events block 住
: 。每次一个thread 读完一个数据,就设置下一个event 是signal的,这样就能保证每
: 次都只有一个thread 在读数据.

avatar
b*c
14
啥炉子还要安装?俺们用电的插头自己一插就好了。

【在 E*A 的大作中提到】
: 朋友家换炉子还包安装,今年还没看到不然早就换了
avatar
n*e
15
不明白你这是怎么做到让所有thread都退出的。我觉得还需要一个退出event,当某个
thread读到最后一个word的时候,就让这个退出event处于signal状态,这样所有的
thread 就都可以退出了。

【在 s********r 的大作中提到】
: 当剩余的buffer size n_left == 0 时,多转一圈即可
avatar
y*n
16
gas range, 安装不便宜的

【在 b*******c 的大作中提到】
: 啥炉子还要安装?俺们用电的插头自己一插就好了。
avatar
s*r
17
how about m=4, n=1 billion
4 core 系统, 1 个 billion 的 words

【在 n****e 的大作中提到】
: 我觉得是不是这样的。可以建一个 circular buffer size of n(不是m)。然后这个
: circular buffer 里面放的是n个word(这样才能知道什么时候把所有的word读完了)
: 。然后产生m 个event,除了一个thread以外,其他m-1个thread都被events block 住
: 。每次一个thread 读完一个数据,就设置下一个event 是signal的,这样就能保证每
: 次都只有一个thread 在读数据.

avatar
s*r
18
At each print, should check the buffer anyway,
if the buffer is ended, post the event to next slot and return.
So the last cycle round, no threads print, only post events.

【在 n****e 的大作中提到】
: 不明白你这是怎么做到让所有thread都退出的。我觉得还需要一个退出event,当某个
: thread读到最后一个word的时候,就让这个退出event处于signal状态,这样所有的
: thread 就都可以退出了。

avatar
m*k
19
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/C

m
printing

【在 n****e 的大作中提到】
: You are given a paragraph , which contain n number of words, you are given m
: threads. What you need to do is , each thread should print one word and
: give the control to next thread, this way each thread will keep on printing
: one word , in case last thread come, it should invoke the first thread.
: Printing will repeat until all the words are printed in paragraph. Finally
: all threads should exit gracefully. What kind of synchronization will use?
: I have no idea how to solve this question. Any guru helps me? Thanks.

avatar
x*n
21
这不是上课的作业题吗?
avatar
s*r
22
这个就是弄着玩儿的,
多核线程设计这种sequential print 模式, 直接用1个 thread print 算了
avatar
f*b
23
MARK
avatar
a*Q
24
two steps: 1) read the next word, and 2) print it. 1) is in memory operation
and needs to be managed; 2) is per thread and no thread safty concern.
To implement 1), ether list or array shall work. and lockfree is the key.
avatar
r*s
25
用一个synchronous queue,再加个semaphore size(1), 一个字符 变量, 不就行了?
每个thread wait on semaphore, 而不是queue。抢到semaphore的就先查前一字符是什
么,如果是回车就release semaphore, exit; 如果不是就等在queue那里,进来一个字
符就print,然后把字符update进变量。release semaphore.
最后送一个回车符,大家一起exit就行了。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。