add multithread demo

pull/1/head
deffpuzzl 2018-07-24 09:45:28 +08:00
parent 25ad40756d
commit 6c1d3731b5
4 changed files with 104 additions and 1 deletions

View File

@ -29,8 +29,9 @@ POPUP=popup
PRESSURE=press_demo PRESSURE=press_demo
TVMPUSH=tvmpush TVMPUSH=tvmpush
ASYPUSH=asypush ASYPUSH=asypush
THREAD=thread_demo
all: $(CREATE) $(INSERT) $(QUEUE) $(PUSH) $(POP) $(SELECT) $(QUERY) $(DELETE) $(UPDATE) $(COUNT) $(GROUP) $(EXTREME) $(TRUNCATE) $(DROP) $(PRESSURE) $(CLICK) $(POPUP) $(REPLACE) $(TVMPUSH) $(ASYPUSH) $(TVMPOP) $(TVMREPLACE) clean all: $(CREATE) $(INSERT) $(QUEUE) $(PUSH) $(POP) $(SELECT) $(QUERY) $(DELETE) $(UPDATE) $(COUNT) $(GROUP) $(EXTREME) $(TRUNCATE) $(DROP) $(PRESSURE) $(CLICK) $(POPUP) $(REPLACE) $(TVMPUSH) $(ASYPUSH) $(TVMPOP) $(TVMREPLACE) $(THREAD) clean
$(CREATE): create_table.o $(CREATE): create_table.o
$(CC) -o $@ $< $(LIBDIR) $(CC) -o $@ $< $(LIBDIR)
$(TVMREPLACE): tvmreplace.o $(TVMREPLACE): tvmreplace.o
@ -75,6 +76,8 @@ $(POPUP): popup.o
$(CC) -o $@ $< $(LIBDIR) $(CC) -o $@ $< $(LIBDIR)
$(PRESSURE): press_demo.o $(PRESSURE): press_demo.o
$(CC) -o $@ $< $(LIBDIR) $(CC) -o $@ $< $(LIBDIR)
$(THREAD): thread_demo.o
$(CC) -o $@ $< $(LIBDIR)
.SUFFIXES: .c .o .SUFFIXES: .c .o

93
demo/thread_demo.c Normal file
View File

@ -0,0 +1,93 @@
#include "tmain.h"
#define QUEUE_USER_INFO 21
typedef struct __QUEUE_USER_INFO
{
long acct_id;
char user_no[21];
char user_type[2];
char user_nm[81];
char user_addr[161];
char user_phone[31];
}dbUser;
typedef unsigned long long uint64;
extern uint64 get_tick_time();
typedef struct _ST_ARG_
{
int threadIndex;
uint64 rows;
uint64 start;
}ARG;
void* vPushUserInfo(void *arg)
{
int i = 0;
dbUser stUser;
ARG *pArgInfo = (ARG *)arg;
SATvm *pstSavm = (SATvm *)pCloneSATvm();
queueinit(pstSavm, stUser, QUEUE_USER_INFO); // 绑定变量
for(i = 0; i < pArgInfo->rows; i++)
{
stUser.acct_id = pArgInfo->start + i; // 对结构体赋值
strcpy(stUser.user_no, "20180223"); // 对结构体赋值
strcpy(stUser.user_type, "1"); // 对结构体赋值
strcpy(stUser.user_nm, "Savens Liu"); // 对结构体赋值
strcpy(stUser.user_addr, "China"); // 对结构体赋值
strcpy(stUser.user_phone, "18672911111"); // 对结构体赋值
if(RC_SUCC != lPush(pstSavm)) // 插入记录
{
fprintf(stderr, "Insert error:(%d)(%s)\n", pstSavm->m_lErrno,
sGetTError(pstSavm->m_lErrno));
return NULL;
}
}
vCloneFree(pstSavm);
return NULL;
}
int main(int argc, char *argv[])
{
uint64 uTime = 0;
ARG arg[100];
pthread_t thread[10];
int i = 0, j = 0, rows = 0, num;
SATvm *pstSavm = (SATvm *)pGetSATvm();
if(1 != argc)
num = strlen(argv[1])>0?atoi(argv[1]):1;
else
num = 1;
/* 初始化QUEUE_USER_INFO表每张表都需要初始化一次, 对于表重建后,需要重新初始化一次。*/
vHoldConnect(pstSavm);
if(RC_SUCC != lAttchTable(pstSavm, QUEUE_USER_INFO))
{
fprintf(stderr, "attch failed, err:(%d)(%s)\n", pstSavm->m_lErrno, sGetTError(pstSavm->m_lErrno));
return RC_FAIL;
}
rows = 4000000 / num;
for(uTime = get_tick_time(); i < num; i++)
{
arg[i].threadIndex = i + 1;
arg[i].start = rows * i;
arg[i].rows = rows;
pthread_create(&thread[i], NULL, vPushUserInfo, (void*)&arg[i]);
}
for(j = 0; j < num; j++)
pthread_join(thread[j], NULL);
fprintf(stdout, "cost_time:[%lld]\r\n", get_tick_time() - uTime);
vTvmDisconnect(pstSavm);
fprintf(stdout, "新增记录成功, completed successfully!!!\n");
fflush(stderr);
return RC_SUCC;
}

View File

@ -166,6 +166,12 @@ long lRestoreTables(SATvm *pstSavm)
snprintf(szPath, sizeof(szPath), "%s/backup", getenv("TVMDBD")); snprintf(szPath, sizeof(szPath), "%s/backup", getenv("TVMDBD"));
if ((NULL == (dir = opendir(szPath)))) if ((NULL == (dir = opendir(szPath))))
{ {
if(ENOENT == errno)
{
mkdir(szPath, S_IRWXU | S_IRGRP);
return RC_SUCC;
}
vRedeError(pstSavm->m_lErrno = 127, strerror(errno)); vRedeError(pstSavm->m_lErrno = 127, strerror(errno));
return RC_FAIL; return RC_FAIL;
} }

View File

@ -569,6 +569,7 @@ long lAttchTable(SATvm *pstSovm, TABLE t)
return RC_FAIL; return RC_FAIL;
memcpy((RunTime *)pGetRunTime(pstSovm, t), pstRun, sizeof(RunTime)); memcpy((RunTime *)pGetRunTime(pstSovm, t), pstRun, sizeof(RunTime));
vSetQueueAttch(pstRun, 1);
return RC_SUCC; return RC_SUCC;
} }