Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

FATAL error with pthread_cancel in multi-thread program #102

@cs-wang

Description

@cs-wang

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";
    }
}


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions