저는 실제로 구식 일 것입니다.하지만 저는 여러분의 코드를 계측하고 직접 측정한다고 말합니다. 예를 들어, 다음 코드와 같은 코드를 프로그램에 추가하고 검색 및 교체를 수행하여 모든 프로그램의 pthread_mutex_lock() 호출을 instrumented_pthread_mutex_lock()으로 변경하십시오.
그런 다음 stdout을 파일로 리디렉션하여 프로그램을 실행하십시오. 그 후에 파일을보고 오랫동안 뮤텍스를 기다리고 있던 스레드를 볼 수 있습니다.
의 printf (()가 다소 프로그램의 타이밍을 변경됩니다 호출 있습니다 만,이 목적을 위해 나는 훨씬 중요합니다 생각하지 않습니다) 아마
#include <stdio.h>
#include <unistd.h>
#include <sys/times.h>
static unsigned long long GetCurrentClockTimeMicroseconds()
{
static clock_t _ticksPerSecond = 0;
if (_ticksPerSecond <= 0) _ticksPerSecond = sysconf(_SC_CLK_TCK);
struct tms junk; clock_t newTicks = (clock_t) times(&junk);
return ((((unsigned long long)newTicks)*(1000000))/_ticksPerSecond);
}
int instrumented_pthread_mutex_lock(pthread_mutex_t * mtx)
{
unsigned long long beforeTime = GetCurrentClockTimeMicroseconds();
int ret = pthread_mutex_lock(mtx);
unsigned long long afterTime = GetCurrentClockTimeMicroseconds();
unsigned long long elapsedTime = (afterTime-beforeTime);
if (elapsedTime > 1000) // or whatever threshold you like; I'm using 1 millisecond here
{
printf("Thread %li took %llu microseconds to acquire mutex %p\n", (long int) pthread_self(), elapsedTime, mtx);
}
return ret;
}
하지,하지만 당신은 아마 그것을 할 수 자신 - 대기하기 전에 플래그를 true로 설정하고 기다린 후 false로 설정하십시오. – immibis
"리소스"라고 말하면 pthread 잠금/뮤텍스 또는 다른 것을 의미합니까? –
@JeremyFriesner 예, "리소스"라고 말하면, pthread 뮤텍스를 의미합니다. –