Hi, All
I run into the following issues.
Firstly, I have two threads and our NDK version is nr5c. I run in
Samsung Nexus S.
Thread A calls Queue.Enqueue and Thread B calls Queue::Dequeue.
Thread A and Thread B are frequently switched (about 150 times /
second). In this case, the CPU time of Queue:Dequeue function is
pretty high after I profile it use the profiler:
http://code.google.com/p/android-ndk-profiler/. It seems that more
than 50% process CPU time is consumed by this function.
I guess when threads are waiting for mutex lock acquisition, it should
not take CPU time. Why the CPU time of this function so high?
Could someone help me clarify it?
Thanks
The code snippet is as following
BOOL
Queue::Enqueue(
int TaskId,
int TaskParam
)
{
QueueItem* pEntry = Alloc();
pEntry->mTaskId = TaskId;
pEntry->mTaskParam = TaskParam;
pEntry->m_Next = NULL;
pthread_mutex_lock(&m_Mutext);
if (m_Tail) {
m_Tail->m_Next = pEntry;
m_Tail = pEntry;
} else {
m_Head = m_Tail = pEntry;
}
pthread_mutex_unlock(&m_Mutext);
pthread_cond_signal(&m_CondVar);
return TRUE;
}
BOOL
Queue::Dequeue(
QueueItem* entry,
uint32_t uCount,
uint32_t* uNumEntriesRemoved
)
{
uint32_t cnt = 0;
QueueItem * pEntry;
int UnixErr = 0;
pthread_mutex_lock(&m_Mutext);
while(1) {
pEntry = m_Head;
while(pEntry && cnt < uCount) {
QueueItem* pNext;
entry[cnt].TaskId = pEntry->mTaskId;
entry[cnt].TaskParam = pEntry->mTaskParam;
pNext = pEntry->m_Next;
Free(pEntry);
pEntry = pNext;
cnt++;
}
m_Head = pEntry;
if (m_Head == NULL) {
m_Tail = NULL;
}
if (cnt > 0) {
break;
}
pthread_cond_wait(&m_CondVar, &m_Mutext);
}
pthread_mutex_unlock(&m_Mutext);
*uNumEntriesRemoved = cnt;
return cnt != 0;
}
Thanks,
-Mike