add test file src/tests/test_pthread_lock.c

pull/37/head
YuQing 2020-01-19 15:52:23 +08:00
parent c8c75666cf
commit 916ad1f9e0
3 changed files with 59 additions and 1 deletions

View File

@ -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

View File

@ -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:

View File

@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <inttypes.h>
#include <sys/time.h>
#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;
}