开发者

Execute 2 different functions concurrently, is pthread my answer?

I have a fixed size array (example: struct bucket[DATASIZE]) where at the very beginning I load information from a file. Since I am concerned about scalability and execution time, no dynamic array was used.

Each time I process half of the array I am free to replace those spots with more data from the file. I don't have a clear idea on ho开发者_如何学Pythonw I would do that but I thought about pthreads to start 2 parallel tasks: one would be the actual data processing and the other one would make sure to fill out the array.

However, all the examples that I've seen on pthreads show that they are all working on the same task but concurrently. Is there a way to have them do separate things? Any ideas, thoughts?


You can definitely have threads doing different tasks. The pattern you're after is very common - it's called a Producer-Consumer arrangement.


What you are trying to do seems very similar to standard concurrent program called producer-consumer (look it up, you surely find an example in pthreads). This program has one fixed size buffer which is processed by consumer and filled by producer.


Yes, that's an excellent use for pthreads: it's one of the very things that pthreads was made for.

You might think about fork( )ing twice, once to create the process to do the data manipulation; and then a second fork( ) to create the process that fills in the blanks. Use a mutex to let each process protect the array from the other process and it will work fine.

Why would your array need a mutex? How would you set it up? When would each process need to acquire the mutex and when would it need to release the mutex?

-- pete

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜