wwbmmm commented on issue #1449:
URL: https://github.com/apache/incubator-brpc/issues/1449#issuecomment-872665659
I had encountered this problem before, finally I created a wrapper class
that ensure calling bthread_getspecific before bthread_setspecific:
```cpp
template<typename T>
class BThreadLocal {
public:
BThreadLocal() {
bthread_key_create(&m_key,
[](void* p) { delete (T*)p; });
}
T* get() const {
return (T*)bthread_getspecific(m_key);
}
T* get_or_create() {
T *p = get();
if (p == nullptr) {
p = new T();
set(p);
}
return p;
}
T *operator->() {
return get_or_create();
}
T &operator*() {
return *get_or_create();
}
private:
// User can't call set(), that may cause memory leak
int set(T* p) {
return bthread_setspecific(m_key, p);
}
bthread_key_t m_key;
};
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]