add exit condition and fix potential memory leak
This commit is contained in:
40
main.c
40
main.c
@@ -18,7 +18,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef Debug
|
#ifdef Debug
|
||||||
#define DPRINT(s, __VA_ARGS__) \
|
#define DPRINT(s, __VA_ARGS__) \
|
||||||
{ \
|
{ \
|
||||||
printf(s, ...); \
|
printf(s, ...); \
|
||||||
}
|
}
|
||||||
@@ -37,11 +37,13 @@ long long current_timestamp();
|
|||||||
size_t cb(char *, size_t, size_t, void *);
|
size_t cb(char *, size_t, size_t, void *);
|
||||||
char *get_enc();
|
char *get_enc();
|
||||||
void parise(void *);
|
void parise(void *);
|
||||||
|
void exit_handler();
|
||||||
|
|
||||||
const char ENC_URL[] = "https://lbapi-rk.chaoxing.com/lb/parise/enc/get?subRoomId=%s×tamp=%llu&ss=%s";
|
const char ENC_URL[] = "https://lbapi-rk.chaoxing.com/lb/parise/enc/get?subRoomId=%s×tamp=%llu&ss=%s";
|
||||||
const char URL[] = "https://zhibo.chaoxing.com/apis/live/setLivePariseCountByEnc?subroomId=%s&enc=%s×tamp=%llu";
|
const char URL[] = "https://zhibo.chaoxing.com/apis/live/setLivePariseCountByEnc?subroomId=%s&enc=%s×tamp=%llu";
|
||||||
const char SUBROOMID[] = "381236458034402305";
|
const char SUBROOMID[] = "381236458034402305";
|
||||||
const char SS[] = "381237373432790016";
|
const char SS[] = "381237373432790016";
|
||||||
|
static int flag = 1;
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@@ -57,27 +59,25 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
curl_global_init(CURL_GLOBAL_ALL);
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
|
|
||||||
printf("线程\t状态\t点赞量\n");
|
printf("线程\t状态\t点赞量\t(按下回车键退出)\n");
|
||||||
|
|
||||||
pthread_t **thread_pool = malloc(sizeof(pthread_t *) * thread);
|
pthread_t *thread_pool = malloc(sizeof(pthread_t) * thread);
|
||||||
|
|
||||||
for (int i = 0; i < thread; i++)
|
for (int i = 0; i < thread; i++)
|
||||||
{
|
{
|
||||||
thread_pool[i] = malloc(sizeof(pthread_t));
|
if (i == thread - 1)
|
||||||
pthread_create(thread_pool[i], NULL, (void *)&parise, (void *)i);
|
pthread_create(&thread_pool[i], NULL, (void *)&exit_handler, NULL);
|
||||||
|
else
|
||||||
|
pthread_create(&thread_pool[i], NULL, (void *)&parise, (void *)i);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < thread; i++)
|
for (int i = 0; i < thread; i++)
|
||||||
{
|
pthread_join(thread_pool[i], NULL);
|
||||||
pthread_join(*thread_pool[i], NULL);
|
|
||||||
}
|
free(thread_pool);
|
||||||
|
|
||||||
curl_global_cleanup();
|
curl_global_cleanup();
|
||||||
|
|
||||||
for (int i = 0; i < 8; i++)
|
|
||||||
{
|
|
||||||
free(thread_pool[i]);
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +130,7 @@ size_t cb(char *data, size_t size, size_t nmemb, void *clientp)
|
|||||||
return 0; /* out of memory! */
|
return 0; /* out of memory! */
|
||||||
|
|
||||||
mem->response = ptr;
|
mem->response = ptr;
|
||||||
memcpy(&(mem->response[mem->size]), data, realsize);
|
memcpy(&mem->response[mem->size], data, realsize);
|
||||||
mem->size += realsize;
|
mem->size += realsize;
|
||||||
mem->response[mem->size] = 0;
|
mem->response[mem->size] = 0;
|
||||||
|
|
||||||
@@ -164,10 +164,11 @@ char *get_enc()
|
|||||||
// {'data': 293687, 'msg': '操作成功', 'result': 1}
|
// {'data': 293687, 'msg': '操作成功', 'result': 1}
|
||||||
void parise(void *arg)
|
void parise(void *arg)
|
||||||
{
|
{
|
||||||
while (true)
|
const int idx = (int)arg;
|
||||||
|
|
||||||
|
while (flag)
|
||||||
{
|
{
|
||||||
char url[200];
|
char url[200];
|
||||||
const int idx = (int)arg;
|
|
||||||
struct memory json_c = {0};
|
struct memory json_c = {0};
|
||||||
const char *enc = get_enc();
|
const char *enc = get_enc();
|
||||||
const cJSON *result = NULL;
|
const cJSON *result = NULL;
|
||||||
@@ -196,4 +197,11 @@ void parise(void *arg)
|
|||||||
|
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
}
|
}
|
||||||
}
|
printf("Signal: exit, T%d exiting\n", idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void exit_handler()
|
||||||
|
{
|
||||||
|
getchar();
|
||||||
|
flag = 0;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user