This will print out a hexadecimal representation of a pthread_t
, no matter what that actually is:
void fprintPt(FILE *f, pthread_t pt) {
unsigned char *ptc = (unsigned char*)(void*)(&pt);
fprintf(f, "0x");
for (size_t i=0; i<sizeof(pt); i++) {
fprintf(f, "%02x", (unsigned)(ptc[i]));
}
}
To just print a small id for a each pthread_t
something like this could be used (this time using iostreams):
void printPt(std::ostream &strm, pthread_t pt) {
static int nextindex = 0;
static std::map<pthread_t, int> ids;
if (ids.find(pt) == ids.end()) {
ids[pt] = nextindex++;
}
strm << ids[pt];
}
Depending on the platform and the actual representation of pthread_t
it might here be necessary to define an operator<
for pthread_t
, because std::map
needs an ordering on the elements:
bool operator<(const pthread_t &left, const pthread_t &right) {
...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…