c ++ |线程和信号处理

时间:2015-05-06 18:54:40

标签: c++ multithreading signals

我正在尝试理解线程和信号处理。 如果我调用signal内的main或我用来创建线程的function,请帮助我了解它有何不同?

# include <iostream>
# include <cstdlib>
# include <pthread.h>
# include <string.h>
# include <csignal>
using namespace std;
void signalHandler( int signum )
{
    cout <<"\n>> Interrupt signal (" << signum << ") received.";
    cout <<"\n>> Release all the resources.";
    cout <<"\n>> Going down.\n"; 

    // cleanup and close up stuff here  
    // terminate program  
    exit(signum);  

}

void *ShellThread(void *threadid)
{
    //signal(SIGINT, signalHandler);  // 1) signal inside the thread function
    string InputToShell;
    while(1)
    {
        cout<<"\n>> ";
        getline(cin, InputToShell);
        cout<<"Input received"<<InputToShell;
    }

    pthread_exit(NULL);
}

int main(int argc, char const *argv[])
{
    signal(SIGINT, signalHandler);  // 2) Signal inside the main function
    pthread_t MainShell;
    int rc;

    rc = pthread_create(&MainShell, NULL, ShellThread, (void *)1);

    if(rc)
    {
        cout<<"Unable to create thread";
    }
    pthread_exit(NULL); 
    return 0;
}

0 个答案:

没有答案
相关问题