스레드를 모니터링하고 싶습니다. 내가 보낼 & HeartBeat & 수신 확인을위한 조건 변수를 사용했습니다.
scnMonitor_t는 모니터 구조입니다. 새 스레드가 추가되면 모니터 &이 scnThreadlist_t에 추가되어 등록됩니다. monitorHeartbeatCheck는 프로그램으로 시작하는 스레드이고 monitorHeartbeatProcess는 모든 스레드 함수에 추가되는 API입니다.하트 비트 신호를 사용하는 스레드 모니터링
사실 내 문제는 프로세스의 인덱스가 제대로 뒤 따르지 않는다는 것입니다. 3rd 스레드에 대해 대기 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;
}
그렇습니다.하지만 HB & ACS 신호 모두에 대해 동일한 뮤텍스를 사용하면 자체적으로 잠금을 해제하고 쓸모가 없다고 생각했습니다. –