From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 16119 invoked by alias); 16 Oct 2003 05:21:32 -0000 Mailing-List: contact pthreads-win32-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: pthreads-win32-owner@sources.redhat.com Received: (qmail 16110 invoked from network); 16 Oct 2003 05:21:31 -0000 Received: from unknown (HELO web41014.mail.yahoo.com) (66.218.93.13) by sources.redhat.com with SMTP; 16 Oct 2003 05:21:31 -0000 Message-ID: <20031016052131.74819.qmail@web41014.mail.yahoo.com> Received: from [203.77.203.69] by web41014.mail.yahoo.com via HTTP; Wed, 15 Oct 2003 22:21:31 PDT Date: Thu, 16 Oct 2003 05:21:00 -0000 From: amit mishra Subject: Threading Issue - Program hangs on Ctrl-C - URGENT PLZ To: pthread MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-SW-Source: 2003/txt/msg00099.txt.bz2 Hi, I had a simple serial port communication application which uses pthread-win32 for threading. When the program exits, the application hangs and the only way to recover is to restart the machine. I have debugged and the issue seems to be regarding thread handling or signal handling. Code snippet is given below for reference. Feel free to ask for more information. Please reply asap. Many Thanks Amit --- CODE STARTS ------------------------------ // all header files not shown #include "pthread.h" #include "signal.h" // three threads for each ports pthread_t t[3]; int result = 0; int tid1, tid2, tid3; void* portOneFunc(void* arg); void* portTwoFunc(void* arg); void* portThreeFunc(void* arg); CDataOne* data1 = NULL; CDataTwo* data2 = NULL; CDataThree* data3 = NULL; // Custom Mutex class using pthread mutex CMyMutex myMutex; CWinApp theApp; ////////////////////////////////////////// // Main ////////////////////////////////////////// int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { // ---Initialize COMM Ports--- CCommPort p1(RS_COM1); // COM1 CCommPort p2(RS_COM2); // COM2 CCommPort p3(RS_COM3); // COM3 // Init various Data Analyzers data1 = new CDataOne(); data2 = new CDataTwo(); data3 = new CDataThree(); try { // --------- Create Threads ------------ // Pthread Create tid1 = pthread_create( &t[0], NULL, portOneFunc, &p1); tid2 = pthread_create( &t[1], NULL, portTwoFunc, &p2); tid3 = pthread_create( &t[2], NULL, portThreeFunc, &p3); // -------- Wait for Threads to complete ---- // Pthread Join int ret = 0; if (pthread_join(t[0], (void **) &result)) { cout << "pthread join error" << endl; } if (pthread_join(t[1], (void **) &result)) { cout << "pthread join error" << endl; } if (pthread_join(t[2], (void **) &result)) { cout << "pthread join error" << endl; } } PtW32CatchAll { cerr << "ERROR : Thread Error" << endl; } delete data1; delete data2; delete data3; return 0; } ////////////////////////////////////////// // Thread Functions ////////////////////////////////////////// void* PortOneFunc(void* arg) { CCommPort *x_oPort1 = (CCommPort*)arg; try { // Open Port with baudrate 4800 if (x_oPort1->Open(RS_4800)) { cout << "COM1 Opened" << endl; do { // Get till terminating chars string str = x_oPort1->GetLine().c_str(); myMutex.lock(); // Append the data for further analysis data1->Append(str.c_str()); myMutex.unlock(); } while(true); // Close the port x_oPort1->Close(); } else { cerr << "Error Opening COM 1 Port" << endl; } } catch(...) { cerr << "ERROR : Thread Exception" << endl; //throw; } return (void*)true; } //////////////////////////////////////////// void* PortTwoFunc(void* arg) { CCommPort *x_oPort2 = (CCommPort*)arg; try { // Open Port if (x_oPort2->Open(RS_1200)) { cout << "COM2 Opened" << endl; do { string str = x_oPort2->GetLine().c_str(); myMutex.lock(); data2->Append(str.c_str()); myMutex.unlock(); } while(true); x_oPort2->Close(); } else { cerr << "Error Opening COM 2 Port" << endl; } } catch(...) { cerr << "ERROR : Thread Exception" << endl; //throw; } return (void*)true; } //////////////////////////////////////////// void* PortThreeFunc(void* arg) { CCommPort *x_oPort3 = (CCommPort*)arg; try { // Open Port if (x_oPort3->Open(RS_1200)) { cout << "COM3 Opened" << endl; do { string str = x_oPort3->GetLine().c_str(); myMutex.lock(); data3->Append(str.c_str()); myMutex.unlock(); } while(true); x_oPort3->Close(); } else { cerr << "Error Opening COM 3 Port" << endl; } } catch(...) { cerr << "ERROR : Thread Exception" << endl; //throw; } return (void*)true; } -- CODE ENDS ------------------------------ __________________________________ Do you Yahoo!? The New Yahoo! Shopping - with improved product search http://shopping.yahoo.com