Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

异步输出可能导致日志输出不完整 #48

Closed
z0306c opened this issue Jul 26, 2019 · 6 comments
Closed

异步输出可能导致日志输出不完整 #48

z0306c opened this issue Jul 26, 2019 · 6 comments

Comments

@z0306c
Copy link

z0306c commented Jul 26, 2019

EasyLogger/src/elog_async.c:
开启ELOG_ASYNC_OUTPUT_USING_PTHREAD情况下,主线程创建线程完成后,才将init_ok设置为true.如:
#ifdef ELOG_ASYNC_OUTPUT_USING_PTHREAD
pthread_attr_t thread_attr;
struct sched_param thread_sched_param;

sem_init(&output_notice, 0, 0);
......
**pthread_create(&async_output_thread, &thread_attr, async_output, NULL);**
......

#endif
//此时,子线程极有可能已经开始执行async_output,而在async_out一开始就断言init_ok为false,
如: ELOG_ASSERT(init_ok); 子程序就有可能再也没有机会执行后边的sem_wait了。
init_ok = true;

最后,只会输出 level < OUTPUT_LVL内容了。

@armink
Copy link
Owner

armink commented Jul 26, 2019

取消 ELOG_ASSERT(init_ok); 断言,或者你还有其他什么解决思路吗?

@z0306c
Copy link
Author

z0306c commented Jul 26, 2019

取消 ELOG_ASSERT(init_ok); 断言,或者你还有其他什么解决思路吗?

我现在就是删掉这里的断言解决的,这里应该也不需要断言的吧

@armink
Copy link
Owner

armink commented Jul 26, 2019

或者就先改为下面方式吧,方便提个 pr 上来吗?

if (!init_ok) {
    return;
}

@z0306c
Copy link
Author

z0306c commented Jul 26, 2019

或者就先改为下面方式吧,方便提个 pr 上来吗?

if (!init_ok) {
    return;
}

这种操作不行的。这样做,子线程就直接退出了,仍然会出现日志输出不完整的问题。这地方不需要对init_ok做判断,因为while循环里有sem_wait,如果没有内容需要输出,就自然等待了

@z0306c
Copy link
Author

z0306c commented Jul 26, 2019

或者就先改为下面方式吧,方便提个 pr 上来吗?

if (!init_ok) {
    return;
}

已经提了一个PR给您

@armink
Copy link
Owner

armink commented Jul 26, 2019

好的,感谢你的贡献哈~~

@armink armink closed this as completed Jul 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants