使用心跳信号在c中进行线程监控

时间:2011-12-07 09:44:08

标签: c multithreading monitoring

我想要监控线程。我使用条件变量send和amp;收到HeartBeat&确认信号。
scnMonitor_t是一个监视器结构。随着新线程的添加,它会在监视器和放大器中注册。添加到scnThreadlist_t。 monitorHeartbeatCheck是以程序开头的线程, monitorHeartbeatProcess是添加到所有线程函数的API。

实际上我的问题是没有正确遵循流程索引 它以第3个线程&的等待HB条件结束。死锁是创建的。 应该是什么问题?
提前致谢。

typedef struct scnThreadList_{
        osiThread_t     thread;
        struct scnThreadList_   *next;
} scnThreadList_t;

typedef struct scnMonitor_{
        bool            started;
        osiThread_t     heartbeatThread; 
        osiMutex_t      heartbeatMutex;
        osiMutex_t      ackMutex;
        osiCond_t       heartbeatCond;
        scnThreadList_t *threads;
} scnMonitor_t; 
static scnMonitor_t *s_monitor = NULL;

// Main heartbeat check thread
void* monitorHeartbeatCheck( void *handle )
{
        scnThreadList_t *pObj = NULL;
        static int idx = 0;
        static bool waitAck = false;

        while ( 1 ) { 
                pObj = s_monitor->threads;
        while ( pObj && ( pObj != s_monitor->heartbeatThread ) ) { //skip it-self from monitoring.
                ++idx;
                printf("\"HB Check No.%d\"\n",idx);
                // send heartbeat
                usleep( 250 * 1000 );
                pthread_mutex_lock( s_monitor->heartbeatMutex, 1 );
                pthread_cond_signal( s_monitor->heartbeatCond );    
                printf("-->C %d HB sent\n",idx);
                pthread_mutex_unlock( s_monitor->heartbeatMutex );
                // wait for ACK
                while( !waitAck ){
                        pthread_mutex_lock( s_monitor->ackMutex, 1 );
                        printf("|| C %d wait Ack\n",idx);
                        waitAck = true;
                        pthread_cond_wait( s_monitor->heartbeatCond, s_monitor->ackMutex );
                        waitAck = false;
                        printf("<--C %d received Ack\n",idx);
                        pthread_mutex_unlock( s_monitor->ackMutex );
                        LOG_INFO( SCN_MONITOR, "ACK from thread %p \n", pObj->thread );
                }
                        pObj = pObj->next;
                }
        } // while, infinite
        return NULL;
}

// Waits for hearbeat and acknowledges
// Call this API from every thread function that are registered
int monitorHeartbeatProcess( void )
{
        static int id = 0;
        static bool waitHb = false;
        ++ id;
        printf("\"HB Process No.%d\"\n",id);
        // wait for HB
        while(!waitHb){
                pthread_mutex_lock( s_monitor->heartbeatMutex, 1 );
                printf("|| P %d wait for HB\n",id);
                waitHb = true;
                pthread_cond_wait( s_monitor->heartbeatCond, s_monitor->heartbeatMutex );
                waitHb = false;
                printf("<--P %d HB received \n",id);
                pthread_mutex_unlock( s_monitor->heartbeatMutex );
        }
        // send ACK
        uleep( 250 * 1000 );
        pthread_mutex_lock( s_monitor->ackMutex, 1 );
        pthread_cond_signal( s_monitor->heartbeatCond );
        printf("-->P %d ACK sent\n",id);
        pthread_mutex_unlock( s_monitor->ackMutex );
        return 1;
}

2 个答案:

答案 0 :(得分:1)

您应始终只将一个互斥锁与一个条件相关联。同时使用具有相同条件的两个不同的互斥锁可能会导致应用程序出现不可预测的序列化问题。

http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Fapis%2Fusers_78.htm

你有两种不同的互斥体,条件为heartbeatCond。

答案 1 :(得分:1)

我认为你在这里遇到了僵局。调用monitorHeartbeatProcess()的线程在heartbeatMutex上接受互斥,并等待条件变量heartbeatCond上的信号。线程调用monitorHeartbeatCheck()在ackMutex上使用互斥量并等待条件变量heartbeatCond上的sognal。因此两个线程都在条件变量heartbeatCond上等待导致死锁。如果你特别使用两个互斥量,为什么不是两个条件变量?