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

FATAL error with pthread_cancel in multi-thread program #102

Closed
cs-wang opened this issue Apr 15, 2016 · 0 comments
Closed

FATAL error with pthread_cancel in multi-thread program #102

cs-wang opened this issue Apr 15, 2016 · 0 comments

Comments

@cs-wang
Copy link

cs-wang commented Apr 15, 2016

while the main thread send cancel signal with pthread_cancel(child_pid), and the child thread set cancel state enable with pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate); the program will aborted abnormally with core dumped.
glog version is glog-0.3.3

SOURCE CODE:


#include <glog/logging.h>
#include <glog/raw_logging.h>
#include <pthread.h>
using namespace std;

void * child(void*a);

int main(int argc, char**argv) {
    google::InitGoogleLogging(argv[0]);
    FLAGS_colorlogtostderr = true;
    FLAGS_log_dir = "glogpath";
   // FLAGS_stderrthreshold = google::FATAL;
   // FLAGS_logbufsecs = 0;
   // FLAGS_max_log_size = 100;
   // FLAGS_stop_logging_if_full_disk = true;
    google::SetStderrLogging(google::GLOG_INFO); 
    google::InstallFailureSignalHandler();

    //google::InstallFailureWriter(&GlogSignalHandle);

    // initialize the logger before it can receive LOG calls
    pthread_t pid;
    int error = pthread_create( &pid, NULL, child, NULL );
    if (error != 0 ) 
    {
        printf("failed to create thread: %d: %s\n", error, strerror(error));
        return 0;
    }


    //sleep(1);
   // printf("main pid:[%d] cancel send statue:[%d]\n", getpid(), pthread_cancel(pid));
   // cout << "main pthreadid: [" << pthread_self() << "] child pthreadid: [" << pid << "]" << endl;
//    cout << "main pid:[" << getpid() << "] cancel send statue: [" <<  pthread_cancel(pid) << "]" << endl;
    for(auto i =0; i < 300; i++)
    {
        if(290 == i)  
        {
    //        pthread_cancel(pid);
          //  sleep(1);
       printf("main pid:[%d] cancel send statue:[%d]\n", getpid(), pthread_cancel(pid));
        sleep(1);
        }
        LOG(INFO) << "["<< i << "]" << "gmain log LOG INFO";
    //  cout << "["<< i << "]" << "gmain log INFO" << endl;
           printf("[%d] gmain printf log\n", i);
   //   RAW_LOG(INFO, "[%d] gmain log INFO", i);
   //     LOG(WARNING) << "["<< i << "]" << "gmain log WARNING";
   //  RAW_LOG(WARNING, "[%d] gmain log WARNING", i);
   //   LOG(DEBUG) << "["<< i << "]" << "gmain log DEBUG";
   //   LOG(FATAL) << "["<< i << "]" << "gmain log FATAL";
   //     LOG(ERROR) << "["<< i << "]" <<"gmain log ERROR";
   //  RAW_LOG(ERROR, "[%d] gmain log ERROR", i);
    }

    void* res;
    pthread_join(pid, &res);

    google::ShutdownGoogleLogging();
}

void* child(void *a)
{
    int oldstate = 0;
    int oldtype = 0;
  //  pthread_detach(pthread_self());
   // pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
   // pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
   // pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
    for(auto i =0; i < 300; i++)
    {
        if( 200 == i )
        {
            pthread_testcancel();
        }
      //pthread_testcancel();
      //cout << "["<< i << "]" << "child log INFO" << endl;
        LOG(INFO) << "["<< i << "]" << "child log LOG INFO";
        printf("[%d] child printf log\n", i);
   //     LOG(WARNING) << "["<< i << "]" << "child log WARNING";
   //     LOG(ERROR) << "["<< i << "]" <<"child log ERROR";
    }
}


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