82 lines
2.3 KiB
C
82 lines
2.3 KiB
C
/*
|
||
* @Author: Lucius
|
||
* @Date: 2023-03-25 14:05:20
|
||
* @LastEditTime: 2023-03-27 17:18:47
|
||
* @LastEditors: Lucius
|
||
* @Description:
|
||
* @FilePath: /23Mar25_PrintLOG/program/main.c
|
||
*/
|
||
#include <stdio.h>
|
||
#include <unistd.h>
|
||
#include <pthread.h>
|
||
#include <string.h>
|
||
#include "printlog.h"
|
||
|
||
void thread_func(void *arg)
|
||
{
|
||
char *str = (char *)arg;
|
||
PrintDebug(dDebugFlag_main, "Child thread: I'm child thread, arg = %s", str);
|
||
|
||
// 子线程do something...
|
||
while (1)
|
||
{
|
||
PrintErr(dDebugFlag_main, "线程循环中。。。");
|
||
sleep(2);
|
||
PrintWarn(dDebugFlag_main, "线程循环中。。。");
|
||
sleep(2);
|
||
PrintInfo(dDebugFlag_main, "线程循环中。。。");
|
||
sleep(2);
|
||
PrintDebug(dDebugFlag_main, "线程循环中。。。");
|
||
sleep(2);
|
||
}
|
||
|
||
// 退出子线程
|
||
PrintDebug(dDebugFlag_main, "Child thread: I'll execute pthread_exit()");
|
||
pthread_exit(NULL);
|
||
|
||
/*此处不会再执行了*/
|
||
PrintDebug(dDebugFlag_main, "xxxxxxxxxxxxxxxxxxxxxxxxxx");
|
||
return;
|
||
}
|
||
|
||
int main(int argc, char **argv)
|
||
{
|
||
#if 1
|
||
SetDebugFlag(dDebugFlag_main);
|
||
#endif
|
||
if (argc > 1)
|
||
{
|
||
int value = 0;
|
||
SetLogTypesByParams((char *)argv[1]);
|
||
value = PRINT_PROCESS_MODE;
|
||
SetPrintParameter(PRINT_RUN_MODE, &value, sizeof(value));
|
||
SetprocessName("main");
|
||
}
|
||
|
||
pthread_t tid;
|
||
|
||
// 这个本身是一个进程,我是在同一进程中创建多个线程,
|
||
// 同时我们一般把主流程叫主线程
|
||
// PrintInfo("Main thread: I'll create some child threads.");
|
||
|
||
char *str = "Farsight thread demo";
|
||
pthread_create(&tid, NULL, (void *)thread_func, (void *)str);
|
||
|
||
// PrintInfo("Main thread: After created thread!");
|
||
|
||
while (1)
|
||
{
|
||
PrintErr(dDebugFlag_main, "进程循环中。。。");
|
||
sleep(1);
|
||
PrintWarn(dDebugFlag_main, "进程循环中。。。");
|
||
sleep(1);
|
||
PrintInfo(dDebugFlag_main, "进程循环中。。。");
|
||
sleep(1);
|
||
PrintDebug(dDebugFlag_main, "进程循环中。。。");
|
||
sleep(1);
|
||
}
|
||
|
||
PrintInfo(dDebugFlag_main, "Main thread: Before pthread_join()!");
|
||
pthread_join(tid, NULL); // 阻塞等待子线程退出,回收子线程所占用内核中8KB物理内存,避免子线程成为僵尸
|
||
PrintInfo(dDebugFlag_main, "Main thread: After pthread_join()!");
|
||
} |