add test file src/tests/test_pthread_lock.c
parent
c8c75666cf
commit
916ad1f9e0
3
HISTORY
3
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
|
Version 1.43 2019-12-25
|
||||||
* replace function call system to getExecResult,
|
* replace function call system to getExecResult,
|
||||||
system is the deprecated function in iOS 11
|
system is the deprecated function in iOS 11
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ LIB_PATH = -lfastcommon -lpthread
|
||||||
ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blocked_queue \
|
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_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_logger test_skiplist_set test_crc32 test_thourands_seperator test_sched_thread \
|
||||||
test_json_parser
|
test_json_parser test_pthread_lock
|
||||||
|
|
||||||
all: $(ALL_PRGS)
|
all: $(ALL_PRGS)
|
||||||
.c:
|
.c:
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue