Thanks to visit codestin.com
Credit goes to sourceforge.net

Menu

[31e5cd]: / src / PThreads.cpp  Maximize  Restore  History

Download this file

37 lines (30 with data), 1.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* Copyright 2002, Log4cpp Project. All rights reserved.
*
* See the COPYING file for the terms of usage and distribution.
*/
#include <cstdlib>
#include <log4cpp/threading/Threading.hh>
#if defined(LOG4CPP_HAVE_THREADING) && defined(LOG4CPP_USE_PTHREADS)
namespace log4cpp {
namespace threading {
std::string getThreadId() {
char buffer[4 * sizeof(long)];
int bufsize = sizeof(buffer) / sizeof(buffer[0]);
int n = ::snprintf(buffer, bufsize, "%lu", pthread_self()); // thread id unsigned
if (n >= bufsize) {
// escape to heap allocation
char* buff_alloc;
int size = ::asprintf(&buff_alloc, "%lu", pthread_self()); // thread id unsigned
if (size < 0) {
throw std::bad_alloc();
}
std::string res(buff_alloc);
free(buff_alloc);
return res;
}
return std::string(buffer);
}
} // namespace threading
} // namespace log4cpp
#endif // LOG4CPP_HAVE_THREADING && LOG4CPP_USE_PTHREADS