Double hashing is a collision resolving technique in Open Addressed Hash tables
Double hashing can be done using :
(hash1(key) + i * hash2(key)) % TABLE_SIZE
Here hash1() and hash2() are hash functions and TABLE_SIZE
is size of hash table.
(We repeat by increasing i when collision occurs)
https://www.geeksforgeeks.org/double-hashing/
zhimakaiman's blog
Friday, May 18, 2018
Monday, May 14, 2018
Tuesday, May 1, 2018
c Makefile template
# c makefile template
SRC_DIR = src
OUTPUT_DIR = output
TARGETS = mytest.bin
SRCS = $(wildcard $(SRC_DIR)/*.c)
DEPS = $(addprefix $(OUTPUT_DIR)/, $(patsubst %.c, %.d, $(notdir $(SRCS))))
OBJS = $(addprefix $(OUTPUT_DIR)/, $(patsubst %.c, %.o, $(notdir $(SRCS))))
CFLAGS = -Wall -O2
RM = rm -f -v
.PHONY: all
all: $(OUTPUT_DIR)/$(TARGETS)
$(OUTPUT_DIR)/%.d: $(SRC_DIR)/%.c
$(CC) -MM $(CFLAGS) $< | sed 's,$*\.o[ :]*,$(basename $@).o $@: ,g' > $@; \
echo "compile .d file: $@"
-include $(DEPS)
$(OUTPUT_DIR)/%.o: $(SRC_DIR)/%.c
$(COMPILE.c) $(OUTPUT_OPTION) $<; \
echo "compile .o file: $@"
$(OUTPUT_DIR)/$(TARGETS): $(OBJS)
$(LINK.o) $(OUTPUT_OPTION) $^; \
echo "compile .bin file: $@ OK!"
.PHONY: clean
clean:
$(RM) output/*; \
echo "clean ok!"
https://blog.csdn.net/thisinnocence/article/details/48946645
https://blog.csdn.net/alpha_love/article/details/62953847
https://blog.csdn.net/thisinnocence/article/details/48946645
https://blog.csdn.net/alpha_love/article/details/62953847
Sunday, April 29, 2018
Implyment read write lock with mutex
Using two mutexes[edit]
Raynal demonstrates how to implement an R/W lock using two mutexes and a single integer counter. The counter, b, tracks the number of blocking readers. One mutex, r, protects b and is only used by readers; the other, g (for "global") ensures mutual exclusion of writers. This requires that a mutex acquired by one thread can be released by another. The following is pseudocode for the operations:
Begin Read
- Lock r.
- Increment b.
- If b = 1, lock g.
- Unlock r.
End Read
- Lock r.
- Decrement b.
- If b = 0, unlock g.
- Unlock r.
Begin Write
- Lock g.
End Write
- Unlock g.
This implementation is read-preferring.[
https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock#Using_two_mutexes
Saturday, April 28, 2018
Lock Free queue with CAS (compare and swap)
do {
p = ctx->tail;
if ( __sync_bool_compare_and_swap(&ctx->tail, p, tmpnode)) {
p->next=tmpnode;
break;
}
} while(1);
First a temporary variable, p
, is assigned the same value of the tail of our queue. In this case ctx
is short for “queue context”. Next we want to add our new node tmpnode
to the end of our queue (make it the tail). If two threads are trying to do this at the same time, then one of them will succeed and one of them will fail. If the compare operation fails then the enqueue action will immediately loop again, trying to do another compare and swap operation, with the updated tail value. It will do this until it is successful, or until the heat death of the universe.
When the compare and set operation is successful, it means that we were able to change the value of the tail
. The next line updates the previous tail’s next
pointer to point to our new node. We don’t need to do this via a compare and set atomic operation, because if anyone else is trying to modify the the tail, they failed (or otherwise our compare would have failed). The break
at the end exits the loop
https://schneems.com/2017/06/28/how-to-write-a-lock-free-queue/
https://www.codeproject.com/Articles/153898/Yet-another-implementation-of-a-lock-free-circular
Wednesday, April 25, 2018
tmux set mouse mode off for copy/paste
using the command below to turn mouse off for copy/paste
prefix : set -g mouse off
https://stackoverflow.com/questions/17445100/getting-back-old-copy-paste-behaviour-in-tmux-with-mouse
Subscribe to:
Posts (Atom)