abumq / easyloggingpp

C++ logging library. It is extremely powerful, extendable, light-weight, fast performing, thread and type safe and consists of many built-in features. It provides ability to write logs in your own customized format. It also provide support for logging your classes, third-party libraries, STL and third-party containers etc.
MIT License
3.75k stars 918 forks source link

In Multi-threading,if define ELPP_THREAD_SAFE, error:Segmentation fault (core dumped) #813

Closed chenzhilinm closed 2 years ago

chenzhilinm commented 2 years ago

My simple code is as follows:

include

include

define ELPP_THREAD_SAFE

include "easylogging++.h"

INITIALIZE_EASYLOGGINGPP pthread_t task1; pthread_t task2; using namespace std; void task1_thread(void arg) { unsigned int count = 0; LOG(INFO) << "In task1"; while(1) { count++; if(count>4500) { LOG(INFO) << "In task1 count = "<<count; count = 0; } usleep(1000); } }

void task2_thread(void arg) { unsigned int count = 0; LOG(INFO) << "In task2"; while(1) { count++; if(count>4500) { LOG(INFO) << "In task2 count = "<<count; count = 0; } usleep(1000); } }

int main(int argc, char *argv[]) {

if 1

void* thread_result;
int res1,res2;
res1 = pthread_create(&task1,NULL,task1_thread,NULL);
if(res1!=0)
{
    cout <<"create task1 failed!"<<endl;
}
else
{
    LOG(DEBUG) <<"create task1 successfully!"<<endl;
}
res2 = pthread_create(&task2,NULL,task2_thread,NULL);
if(res2!=0)
{
    cout << "create task2 failed"<<endl;
}
else
{
    LOG(DEBUG) << "create task2 successfully!"<<endl;
}
pthread_join(task1,&thread_result);
pthread_join(task2,&thread_result);

endif

LOG(INFO) << "This is just a test!";
return 0;

}

g++ main.cpp easylogging++.cc easylogging++.h -std=c++11 -lpthread -o log_test ./log_test

error: Segmentation fault (core dumped)

chenzhilinm commented 2 years ago

if I comment the macro ELPP_THREAD_SAFE, the program can run normally,Why?

chenzhilinm commented 2 years ago

Adding the macro ELPP_THREAD_SAFE when compling and the problem is solved