diff --git a/HISTORY b/HISTORY index 9d33a9b..54982ce 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,7 @@ +Version 1.44 2020-01-19 + * add test file src/tests/test_pthread_lock.c + Version 1.43 2019-12-25 * replace function call system to getExecResult, system is the deprecated function in iOS 11 diff --git a/src/tests/Makefile b/src/tests/Makefile index 2b616b3..1ce1513 100644 --- a/src/tests/Makefile +++ b/src/tests/Makefile @@ -7,7 +7,7 @@ LIB_PATH = -lfastcommon -lpthread ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blocked_queue \ test_id_generator test_ini_parser test_char_convert test_char_convert_loader \ test_logger test_skiplist_set test_crc32 test_thourands_seperator test_sched_thread \ - test_json_parser + test_json_parser test_pthread_lock all: $(ALL_PRGS) .c: diff --git a/src/tests/test_pthread_lock.c b/src/tests/test_pthread_lock.c new file mode 100644 index 0000000..c8ad263 --- /dev/null +++ b/src/tests/test_pthread_lock.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include +#include "fastcommon/logger.h" +#include "fastcommon/shared_func.h" +#include "fastcommon/sched_thread.h" +#include "fastcommon/pthread_func.h" +#include "fastcommon/ini_file_reader.h" +#include "fastcommon/fast_allocator.h" + +#define LOOP_COUNT (30 * 1000 * 1000) + +int main(int argc, char *argv[]) +{ + int result; + int k; + int64_t sum; + int64_t start_time; + pthread_mutex_t lock; + + log_init(); + srand(time(NULL)); + g_log_context.log_level = LOG_DEBUG; + + if ((result=init_pthread_lock(&lock)) != 0) + { + logCrit("init_pthread_lock fail, result: %d", result); + return result; + } + + start_time = get_current_time_ms(); + sum = 0; + for (k=1; k<=LOOP_COUNT; k++) { + __sync_add_and_fetch(&sum, k); + } + + printf("atom add, sum: %"PRId64", time used: %"PRId64" ms\n", + sum, get_current_time_ms() - start_time); + + start_time = get_current_time_ms(); + sum = 0; + for (k=1; k<=LOOP_COUNT; k++) { + pthread_mutex_lock(&lock); + sum += k; + pthread_mutex_unlock(&lock); + } + + printf("locked add, sum: %"PRId64", time used: %"PRId64" ms\n", + sum, get_current_time_ms() - start_time); + return 0; +}