You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Michi Mutsuzaki <mi...@yahoo-inc.com> on 2010/11/05 23:52:12 UTC

TThreadPoolServer thread safety

Hi,

I'm using TThreadPoolServer with C++. Does my handler need to be
thread-safe? More specifically, my handler has a static member, and I'm
wondering if I need to protect it using a mutex.

Thanks!
--Michi


RE: TThreadPoolServer thread safety

Posted by Richard Salz <rs...@us.ibm.com>.
> Otherwise, I'd probably take a look at boost's TLS mechanism: 
> 
http://www.boost.org/doc/libs/1_39_0/doc/html/thread/thread_local_storage.html


I would go this route.  I haven't looked at the BOOST implementation, but 
there have been issues in the past with pthead_key_create.  If performance 
is an issue, post here and we can talk about ways to optimize that.

It would be nice if Thrift had a thread_create_hook and a 
thread_delete_hook, since thread lifetime isn't discussed in 
TThreadPoolServer.  Is that worth opening an issue?

Hope this helps.

        /r$

--
STSM, WebSphere Appliance Architect
https://www.ibm.com/developerworks/mydeveloperworks/blogs/soma/



RE: TThreadPoolServer thread safety

Posted by Mark Slee <ms...@facebook.com>.
You will have to use a thread-local store construct for this. This has definitely come up before and I'm pretty sure there are some patches out there for it, in one capacity or another.

If your buffers are of a fixed, reasonable size, I'd recommend just sticking them on the stack (as local variables in the handler method) -- though it sounds from your message like this is not the case and you want to avoid allocations.

If writes to your buffer are rare, a read-write lock might suffice. Otherwise, I'd probably take a look at boost's TLS mechanism: http://www.boost.org/doc/libs/1_39_0/doc/html/thread/thread_local_storage.html

I haven't been working at this level of C++ for quite a while, someone else on the list probably has better recommendation. As far as I'm aware, we don't have an API for this in the Thrift library itself.

Any other opinions here?

Cheers,
mcslee

-----Original Message-----
From: Michi Mutsuzaki [mailto:michim@yahoo-inc.com] 
Sent: Friday, November 05, 2010 5:25 PM
To: user@thrift.apache.org
Subject: Re: TThreadPoolServer thread safety

Thank you for your reply, Mark.

Is there a way to have per-thread data? For example, can I allocate a buffer
for each thread in the threadpool? Otherwise, each thread needs to
allocate/deallocate a buffer in the handler function for each request.

Thanks!
--Michi

On 11/5/10 4:16 PM, "Mark Slee" <ms...@facebook.com> wrote:

> TThreadPoolServer is *not* thread-safe. The threads it creates are not
> synchronized in any way.
> 
> You must implement thread-safety yourself if you are sharing any data across
> request handlers.
> 
> -----Original Message-----
> From: Michi Mutsuzaki [mailto:michim@yahoo-inc.com]
> Sent: Friday, November 05, 2010 3:52 PM
> To: user@thrift.apache.org
> Subject: TThreadPoolServer thread safety
> 
> Hi,
> 
> I'm using TThreadPoolServer with C++. Does my handler need to be
> thread-safe? More specifically, my handler has a static member, and I'm
> wondering if I need to protect it using a mutex.
> 
> Thanks!
> --Michi
> 
> 


Re: TThreadPoolServer thread safety

Posted by Michi Mutsuzaki <mi...@yahoo-inc.com>.
Thank you for your reply, Mark.

Is there a way to have per-thread data? For example, can I allocate a buffer
for each thread in the threadpool? Otherwise, each thread needs to
allocate/deallocate a buffer in the handler function for each request.

Thanks!
--Michi

On 11/5/10 4:16 PM, "Mark Slee" <ms...@facebook.com> wrote:

> TThreadPoolServer is *not* thread-safe. The threads it creates are not
> synchronized in any way.
> 
> You must implement thread-safety yourself if you are sharing any data across
> request handlers.
> 
> -----Original Message-----
> From: Michi Mutsuzaki [mailto:michim@yahoo-inc.com]
> Sent: Friday, November 05, 2010 3:52 PM
> To: user@thrift.apache.org
> Subject: TThreadPoolServer thread safety
> 
> Hi,
> 
> I'm using TThreadPoolServer with C++. Does my handler need to be
> thread-safe? More specifically, my handler has a static member, and I'm
> wondering if I need to protect it using a mutex.
> 
> Thanks!
> --Michi
> 
> 


RE: TThreadPoolServer thread safety

Posted by Mark Slee <ms...@facebook.com>.
TThreadPoolServer is *not* thread-safe. The threads it creates are not synchronized in any way.

You must implement thread-safety yourself if you are sharing any data across request handlers.

-----Original Message-----
From: Michi Mutsuzaki [mailto:michim@yahoo-inc.com] 
Sent: Friday, November 05, 2010 3:52 PM
To: user@thrift.apache.org
Subject: TThreadPoolServer thread safety

Hi,

I'm using TThreadPoolServer with C++. Does my handler need to be
thread-safe? More specifically, my handler has a static member, and I'm
wondering if I need to protect it using a mutex.

Thanks!
--Michi