mix usage of boost.thread and pthread?

Hi all, Is it OK to use both pthread and boost.thread in the same program under linux? E.g., there are two cases: 1. use pthread api to create/join/detach/cancel threads but use boost.thread's mutex/condition_variable etc. to do thread synchronization, and this_thread/sleep/get_id/thread_local_storage etc.. 2. use boost.thread api to create/join/detach/interrupt thread but use pthread api to do thread synchronization. I know boost.thread is a pthread api wrapper under linux, but I'm afraid of that the two cases above aren't safe if boost.thread uses some tricks I don't understand. Would you kindly give any hints? Thanks! Hua

On Fri, Jan 29, 2010 at 11:26 AM, Hua Su
Hi all, Is it OK to use both pthread and boost.thread in the same program under linux? E.g., there are two cases: 1. use pthread api to create/join/detach/cancel threads but use boost.thread's mutex/condition_variable etc. to do thread synchronization, and this_thread/sleep/get_id/thread_local_storage etc.. 2. use boost.thread api to create/join/detach/interrupt thread but use pthread api to do thread synchronization. I know boost.thread is a pthread api wrapper under linux, but I'm afraid of that the two cases above aren't safe if boost.thread uses some tricks I don't understand. Would you kindly give any hints?
I do not program much using pthreads, so I cannot say about it for sure, but about other threading back-ends I know that you can use them both in the same program, but they do not mix as Boost.Threads registers things and so forth (else things like this_thread will not work on non Boost.Threads). Why would you want to mix them anyway?

On Sat, Jan 30, 2010 at 6:46 AM, OvermindDL1
Hi all, Is it OK to use both pthread and boost.thread in the same program under linux? E.g., there are two cases: 1. use pthread api to create/join/detach/cancel threads but use boost.thread's mutex/condition_variable etc. to do
On Fri, Jan 29, 2010 at 11:26 AM, Hua Su
wrote: thread synchronization, and this_thread/sleep/get_id/thread_local_storage etc.. 2. use boost.thread api to create/join/detach/interrupt thread but use pthread api to do thread synchronization. I know boost.thread is a pthread api wrapper under linux, but I'm afraid of that the two cases above aren't safe if boost.thread uses some tricks I don't understand. Would you kindly give any hints?
I do not program much using pthreads, so I cannot say about it for sure, but about other threading back-ends I know that you can use them both in the same program, but they do not mix as Boost.Threads registers things and so forth (else things like this_thread will not work on non Boost.Threads).
Why would you want to mix them anyway?
My program is built with a third-party framework that uses some pthread things, but I want to use boost.thread to implement my own part.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hua Su
Is it OK to use both pthread and boost.thread in the same program under linux?
Yes.
1. use pthread api to create/join/detach/cancel threads but use boost.thread's mutex/condition_variable etc. to do thread synchronization, and this_thread/ sleep/get_id/thread_local_storage etc..
That's fine.
2. use boost.thread api to create/join/detach/interrupt thread but use pthread api to do thread synchronization.
And that's fine too. Anthony -- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

On Sat, Jan 30, 2010 at 7:37 AM, Anthony Williams
Hua Su
writes: Is it OK to use both pthread and boost.thread in the same program under linux?
Yes.
1. use pthread api to create/join/detach/cancel threads but use boost.thread's mutex/condition_variable etc. to do thread synchronization, and this_thread/ sleep/get_id/thread_local_storage etc..
That's fine.
Another reply of this topic by OvermindDL1 said "things like this_thread will not work on non Boost.Threads". Could you give any explanation about this?
2. use boost.thread api to create/join/detach/interrupt thread but use pthread api to do thread synchronization.
And that's fine too.
Anthony -- Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/ just::thread C++0x thread library http://www.stdthread.co.uk Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Fri, Jan 29, 2010 at 11:22 PM, Hua Su
On Sat, Jan 30, 2010 at 7:37 AM, Anthony Williams
wrote: Hua Su
writes: Is it OK to use both pthread and boost.thread in the same program under linux?
Yes.
1. use pthread api to create/join/detach/cancel threads but use boost.thread's mutex/condition_variable etc. to do thread synchronization, and this_thread/ sleep/get_id/thread_local_storage etc..
That's fine.
Another reply of this topic by OvermindDL1 said "things like this_thread will not work on non Boost.Threads". Could you give any explanation about this?
I may be wrong, but I could have *sworn* that at one point in time if I used native windows threads, then some of the Boost.Threads functionality just kind of choked. Am I wrong? If so, I would love to know. :)

----- Original Message -----
From: "OvermindDL1"
On Fri, Jan 29, 2010 at 11:22 PM, Hua Su
wrote: On Sat, Jan 30, 2010 at 7:37 AM, Anthony Williams
wrote: Hua Su
writes: Is it OK to use both pthread and boost.thread in the same program under linux?
Yes.
1. use pthread api to create/join/detach/cancel threads but use boost.thread's mutex/condition_variable etc. to do thread synchronization, and this_thread/ sleep/get_id/thread_local_storage etc..
That's fine.
Another reply of this topic by OvermindDL1 said "things like this_thread will not work on non Boost.Threads". Could you give any explanation about this?
I may be wrong, but I could have *sworn* that at one point in time if I used native windows threads, then some of the Boost.Threads functionality just kind of choked. Am I wrong? If so, I would love to know. :)
Hi, the functions at the this_thread namespace can be used in native threads without any know issue. You are not alone to think that and an example on the documentation should be added to show the possibility. This was a requirement as libraries don't know which kind of threads calls its functions. In general all the combinations should work, as pointed by Anthony. The usage of Boost.Thread resources by other parts is available using the native_handle() API (note that not every resource has the native_handle() function). The oposite is not supported except by the current thread., i.e. if a part of the program gives the reference of a pthread resource, there is no mean to retrieve the Boost.Thread resource. The application needs in this case to map the resources if the need appear in his application. The use cases presented should not be risky as there is no need to exchange the resources if I have understood. HTH, Vicente

Thanks for your answer!
And I suggest adding a line in boost.thread document to say something like:
"it's safe to mix usage of boost.thread and native thread api in cases: blah
blah..."
so others won't be hesitant to change using boost.thread with existing
frameworks or in legacy systems.
On Mon, Feb 1, 2010 at 3:11 PM, vicente.botet
----- Original Message ----- From: "OvermindDL1"
To: Sent: Saturday, January 30, 2010 8:23 AM Subject: Re: [Boost-users] mix usage of boost.thread and pthread? On Fri, Jan 29, 2010 at 11:22 PM, Hua Su
wrote: On Sat, Jan 30, 2010 at 7:37 AM, Anthony Williams <
anthony.ajw@gmail.com>
wrote:
Hua Su
writes: Is it OK to use both pthread and boost.thread in the same program
under
linux?
Yes.
1. use pthread api to create/join/detach/cancel threads but use boost.thread's mutex/condition_variable etc. to do thread synchronization, and this_thread/ sleep/get_id/thread_local_storage etc..
That's fine.
Another reply of this topic by OvermindDL1 said "things like this_thread will not work on non Boost.Threads". Could you give any explanation about this?
I may be wrong, but I could have *sworn* that at one point in time if I used native windows threads, then some of the Boost.Threads functionality just kind of choked. Am I wrong? If so, I would love to know. :)
Hi,
the functions at the this_thread namespace can be used in native threads without any know issue. You are not alone to think that and an example on the documentation should be added to show the possibility. This was a requirement as libraries don't know which kind of threads calls its functions.
In general all the combinations should work, as pointed by Anthony. The usage of Boost.Thread resources by other parts is available using the native_handle() API (note that not every resource has the native_handle() function). The oposite is not supported except by the current thread., i.e. if a part of the program gives the reference of a pthread resource, there is no mean to retrieve the Boost.Thread resource. The application needs in this case to map the resources if the need appear in his application.
The use cases presented should not be risky as there is no need to exchange the resources if I have understood.
HTH, Vicente
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hua Su wrote:
Thanks for your answer!
And I suggest adding a line in boost.thread document to say something like:
"it's safe to mix usage of boost.thread and native thread api in cases: blah blah..."
so others won't be hesitant to change using boost.thread with existing frameworks or in legacy systems.
I agree. The best way to help this be done as soon as possible could be to make a Trac ticket feature request, so Anthony will have this in memory. Could you do such a ticket? Best, Vicente -- View this message in context: http://old.nabble.com/mix-usage-of-boost.thread-and-pthread--tp27376549p2740... Sent from the Boost - Users mailing list archive at Nabble.com.

On Mon, Feb 1, 2010 at 10:56 PM, Vicente Botet Escriba < vicente.botet@wanadoo.fr> wrote:
Hua Su wrote:
Thanks for your answer!
And I suggest adding a line in boost.thread document to say something like:
"it's safe to mix usage of boost.thread and native thread api in cases: blah blah..."
so others won't be hesitant to change using boost.thread with existing frameworks or in legacy systems.
I agree. The best way to help this be done as soon as possible could be to make a Trac ticket feature request, so Anthony will have this in memory. Could you do such a ticket?
The ticket is filed here: https://svn.boost.org/trac/boost/ticket/3885 :-)
Best, Vicente -- View this message in context: http://old.nabble.com/mix-usage-of-boost.thread-and-pthread--tp27376549p2740... Sent from the Boost - Users mailing list archive at Nabble.com.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (5)
-
Anthony Williams
-
Hua Su
-
OvermindDL1
-
Vicente Botet Escriba
-
vicente.botet