n****e 发帖数: 43 | 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. |
s********r 发帖数: 403 | 2 message queue
或自己建一个 circular buffer size of m, 每个指向一个 event,
thread 完成自己的操作后向下一个 slot post event,转完一圈后正好可以回到原来的
位置。 |
n****e 发帖数: 43 | 3 我觉得是不是这样的。可以建一个 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,转完一圈后正好可以回到原来的 : 位置。
|
p********s 发帖数: 3 | 4 如何实现 “Finally all threads should exit gracefully.”?
【在 s********r 的大作中提到】 : message queue : 或自己建一个 circular buffer size of m, 每个指向一个 event, : thread 完成自己的操作后向下一个 slot post event,转完一圈后正好可以回到原来的 : 位置。
|
s********r 发帖数: 403 | 5 当剩余的buffer size n_left == 0 时,多转一圈即可
【在 p********s 的大作中提到】 : 如何实现 “Finally all threads should exit gracefully.”?
|
G****A 发帖数: 4160 | 6 +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 在读数据.
|
n****e 发帖数: 43 | 7 不明白你这是怎么做到让所有thread都退出的。我觉得还需要一个退出event,当某个
thread读到最后一个word的时候,就让这个退出event处于signal状态,这样所有的
thread 就都可以退出了。
【在 s********r 的大作中提到】 : 当剩余的buffer size n_left == 0 时,多转一圈即可
|
s********r 发帖数: 403 | 8 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 在读数据.
|
s********r 发帖数: 403 | 9 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 就都可以退出了。
|
m*****k 发帖数: 731 | 10 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.
|
|
|
m*******h 发帖数: 2 | 11 Not the best quality of code, but does solve the problem.
https://gist.github.com/anonymous/6182478 |
x**n 发帖数: 461 | |
s********r 发帖数: 403 | 13 这个就是弄着玩儿的,
多核线程设计这种sequential print 模式, 直接用1个 thread print 算了 |
f*******b 发帖数: 520 | |
a****Q 发帖数: 83 | 15 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. |
r****s 发帖数: 1025 | 16 用一个synchronous queue,再加个semaphore size(1), 一个字符 变量, 不就行了?
每个thread wait on semaphore, 而不是queue。抢到semaphore的就先查前一字符是什
么,如果是回车就release semaphore, exit; 如果不是就等在queue那里,进来一个字
符就print,然后把字符update进变量。release semaphore.
最后送一个回车符,大家一起exit就行了。 |