问题
I am currently developing a web app in C using the G-Wan web server and I want to use the KV Store included with G-Wan in order to store the generated web page and a counter on the number of times the page has been displayed.
The KV store is a hash map you can use to store complex data, like structures.
That's what I have used to store the HTML data and an integer which is the counter:
typedef struct
{
char* HTML;
int nbDisplays;
} my_data;
The web server is using several threads and the KV store is shared among the threads.
For updating the field nbDisplays I am using an atomic operation __sync_fetch_and_add (...) But I don't know what is the best way to update the field HTML which is a pointer.
Is it better:
- to build a new structure using the existing one stored in KV to get the current values and add it to the KV Store with the add() function provided by G-WAN (this function replace the old structure by the new one)
- Or to update directly the values of the structure stored in the KV store?
I am not used to program in a multi thread environment and I am scared about some "strange" behaviors...
回答1:
kv_add()
is documented as being an atomic operation. You could use it to update your counter but this is slower than just using an atomic operation to increment the nbDisplays
counter without touching the HTML pointer (the name of the page does not change).
But G-Wan KV Store's features does not make this look like a hash map
. You can't do those range queries with a hash map
.
来源:https://stackoverflow.com/questions/12006364/managing-the-caching-in-g-wan-with-kv-store