English 中文(简体)
更好地锁定共享资源,或者愿意满足要求?
原标题:Better to lock on a shared resource, or have a thread to fulfill requests?

我有一个共享的记忆库,许多不同的透镜可以要求分配。 要求从中拨款将发生在每一条路面,但read的面积可能很小,往往只有一只read。 我不敢肯定,以下处理方式中哪些是更好的。

最后,我可能需要执行和看到两者产生更有利的结果...... 我也担心,即便是第2号的思考,此时此刻也可能是不成熟的,因为我实际上没有使用这一共同资源的法律。 但是,这个问题令人 interesting切,继续使我偏离其他工作。

1) Create a mutex and have a thread attempt to lock it before obtaining the allocation, then unlocking it.

(2) 每当需要分配申请时,每个校正都登记申请时间,然后在申请时间段(同时(结果=NUL)栏(斜体=NUL)等待申请时间才能取得结果。 单一校正不断使申请时间固定,使拨款额得以分配,从而导致申请时间。

Number 1 is the simple solution, but a single thread could potentially hog the lock if the timing is right. The second is more complex, but ensures fairness among threads when pulling from the resource. However it still blocks the requesting threads, and if there are many threads the iteration could burn cycles without doing any actual allocations until it finds a request to fulfill.

NOTE: C on Linux using pthreads

最佳回答

Solution 2 is bogus. It s an ugly hack and it does not ensure memory synchronization.

I would say go with solution 1, but I m a little bit skeptical of the fact that you mentioned "memory pool" to begin with. Are you just trying to allocate memory, or is there some other resource you re managing (e.g. slots in some special kind of memory, memory-mapped file, textures in video memory, etc.)?

If you are just allocating memory, then you re completely right to be worried about premature optimization. The whole problem is premature optimization, and the system malloc will do as well as or better than your memory pool will do. (Or if your code will be running on one of the few systems with a pathologically broken malloc like some video game consoles, just drop in a replacement only on those known-broken systems.)

If you really do have a special resource you need to manage, start with solution 1 and see how it works. If you have problems, you might find you can improve it with a condition variable where the resource manager notifies you when a slot can be allocated, but I really doubt this will be necessary.

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签