h******e 发帖数: 52 | 1 producer / consumer 问题, 要求threadsafe, high throughput
class ProducerConsumer
{
ReaderWriterLock rwLock = new ReaderWriterLock();
AutoResetEvent FullEvent = new AutoResetEvent ();
AutoResetEvent EmptyEvent = new AutoResetEvent ();
public void Producer()
{
rwLock.AcquireWriterLock();
while(queue is full)
{
FullEvent.waitOne();
}
//add
if(Queue.count == 1)
EmptyEvent.set();
rwLock.ReleaseLock();
}
public void Consumer()
{
rwLock.AcquireReaderLock();
while(Queue is empty)
{
EmptyEvent.WaitOne();
}
dequueue();
if (Count == max – 1);
FullEvent.set();
rwLock.ReleaseReaderLock();
} |
h**p 发帖数: 211 | 2 LZ这是电面?感觉不好搞啊
LZ提供下背景吧
【在 h******e 的大作中提到】 : producer / consumer 问题, 要求threadsafe, high throughput : class ProducerConsumer : { : ReaderWriterLock rwLock = new ReaderWriterLock(); : AutoResetEvent FullEvent = new AutoResetEvent (); : AutoResetEvent EmptyEvent = new AutoResetEvent (); : public void Producer() : { : rwLock.AcquireWriterLock(); : while(queue is full)
|
N*****a 发帖数: 17 | 3 对啊,没怎么复习concurrency的题,感觉挺难的。
请问楼主,consumer为什么用reader lock,consumer里面有dequeue这个操作,更改了
queue的内容,这样岂不是可以好几个reader thread一起dequeue?就不thread safe了
吧。是不是应该直接在method上加synchronized? |
h******e 发帖数: 52 | 4 简化一下
public class ProducerConsumer
{
private Semaphore fillCount;
private Semaphore emptyCount;
private ConcurrentQueue queue;
public ProducerConsumer()
{
fillCount = new Semaphore(0, 100);
emptyCount = new Semaphore(100, 100);
queue = ConcurrentQueue();
}
public void Producer()
{
while(true)
{
//produce something
int x = 1;
emptyCount.WaitOne();
queue.Enqueue(x);
fillCount.Release();
}
}
public void Consumer()
{
while(true)
{
fillCount.WaitOne();
int x = queue.Dequeue();
emptyCount.Release();
}
}
}
【在 N*****a 的大作中提到】 : 对啊,没怎么复习concurrency的题,感觉挺难的。 : 请问楼主,consumer为什么用reader lock,consumer里面有dequeue这个操作,更改了 : queue的内容,这样岂不是可以好几个reader thread一起dequeue?就不thread safe了 : 吧。是不是应该直接在method上加synchronized?
|
h**p 发帖数: 211 | 5 LZ, high through put是怎么做到的?面试官有详细说明吗?
前后提供的代码好像都有一点点小问题
详细的请看这里:https://en.wikipedia.org/wiki/Monitor_(synchronization)
下面的conditional variable, producer/consumer这块
【在 h******e 的大作中提到】 : 简化一下 : public class ProducerConsumer : { : private Semaphore fillCount; : private Semaphore emptyCount; : private ConcurrentQueue queue; : public ProducerConsumer() : { : fillCount = new Semaphore(0, 100); : emptyCount = new Semaphore(100, 100);
|
b******b 发帖数: 713 | 6 sorry cannot type in chinese.
I would start with, in practice no one is going to write producer/consumer
queue himself instead of using an existing concurrent queue implementation.
E.g. in java, there are ArrayBlockingQueue, LinkedBlockingQueue, etc, which
can be used for producer/consumer problem, and they have different
performance depend on use case. We can dive down int more details, e.g.
ArrayBlockingQueue is using 1 lock, thus producer/consumer is sharing same
lock, while LinkedBlockingQueue is doing lock splitting, thus producer/
consumer is not blocking each other, but the add/remove operation is more
expensive than ArrayQueue which just update the index,etc.
In the case of more than 1 producer/consumer, there are other tech to
improve concurrency, e.g. lock stripping, or some open source product like
Disruptor queue, etc.
【在 h**p 的大作中提到】 : LZ, high through put是怎么做到的?面试官有详细说明吗? : 前后提供的代码好像都有一点点小问题 : 详细的请看这里:https://en.wikipedia.org/wiki/Monitor_(synchronization) : 下面的conditional variable, producer/consumer这块
|
J****r 发帖数: 274 | 7 High throughput需要用到lock-free datastructure。Google一下LMAX Disruptor吧。
【在 h******e 的大作中提到】 : producer / consumer 问题, 要求threadsafe, high throughput : class ProducerConsumer : { : ReaderWriterLock rwLock = new ReaderWriterLock(); : AutoResetEvent FullEvent = new AutoResetEvent (); : AutoResetEvent EmptyEvent = new AutoResetEvent (); : public void Producer() : { : rwLock.AcquireWriterLock(); : while(queue is full)
|