-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
65 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
cmake_minimum_required (VERSION 3.8.2) | ||
project(KeyValueStorage) | ||
|
||
add_compile_options(-std=c11) | ||
include_directories(include) | ||
|
||
file(GLOB SOURCES "src/*.c") | ||
file(GLOB sources "src/*.c") | ||
list(REMOVE_ITEM sources "src/node.c" "src/hashtable.c" "src/thpool.c") | ||
|
||
add_library(node "src/node.c") | ||
add_library(hashtable "src/hashtable.c") | ||
|
||
add_library(thpool "src/thpool.c") | ||
|
||
add_executable(main ${sources}) | ||
add_executable(node_test "test/node_test.c") | ||
add_executable(hash_test "test/hash_test.c") | ||
add_executable(thpool_rwlock_test "test/thpool_rwlock_test.c") | ||
|
||
add_executable(main ${SOURCES}) | ||
add_executable(node_test "test/node_test.c" "src/node.c") | ||
add_executable(hash_test "test/hash_test.c" "src/node.c" "src/hashtable.c") | ||
target_link_libraries(main pthread) | ||
target_link_libraries(hashtable node) | ||
target_link_libraries(thpool pthread) | ||
target_link_libraries(main hashtable thpool) | ||
target_link_libraries(node_test node) | ||
target_link_libraries(hash_test hashtable) | ||
target_link_libraries(thpool_rwlock_test thpool) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <pthread.h> | ||
#include "thpool.h" | ||
|
||
typedef threadpool threadpool_t; | ||
|
||
static pthread_rwlock_t rwlock; | ||
static threadpool_t thpool; | ||
|
||
void read_task() { | ||
pthread_rwlock_rdlock(&rwlock); | ||
int i; | ||
for (i = 0; i < 20; i++) { | ||
printf("read_task_%d in %u\n", i, (int) pthread_self()); | ||
} | ||
pthread_rwlock_unlock(&rwlock); | ||
} | ||
|
||
void write_task() { | ||
pthread_rwlock_wrlock(&rwlock); | ||
int i; | ||
for (i = 0; i < 20; i++) { | ||
printf("write_task_%d in %u\n", i, (int) pthread_self()); | ||
} | ||
pthread_rwlock_unlock(&rwlock); | ||
} | ||
|
||
int main() { | ||
thpool = thpool_init(4); | ||
pthread_rwlock_init(&rwlock, NULL); | ||
|
||
int i; | ||
|
||
for (i = 0; i < 4; i++) { | ||
if (i == 2) { | ||
thpool_add_work(thpool, (void *) write_task, NULL); | ||
} | ||
thpool_add_work(thpool, (void *) read_task, NULL); | ||
} | ||
|
||
sleep(1); | ||
thpool_destroy(thpool); | ||
pthread_rwlock_destroy(&rwlock); | ||
return 0; | ||
} |