pthread_mutex vs std::mutex


(Han Sang-jin) #1

I'm a newbie in this world. I have a qustion.

Is there any reason to prefer pthread_mutex_* to std::mutex in the swift
stdlib ?

Nowadays in C++, the std::mutex is the standard and more portable than
pthread_mutex_*.

If I'm porting the swift stdlib to non-POSIX system, which method should I
choose ?

Adding #if-#else-#endif or changing pthread to std::mutex ?

Thanks

Han Sangjin


(Dmitri Gribenko) #2

I don't think there is a problem with using std::mutex, but I know
that you will encounter issues down the road (in tests and other
places) that use pthreads. It will be a massive task to port them.
You might consider writing a shim library implementing the required
parts of pthreads instead.

Dmitri

···

On Thu, Feb 25, 2016 at 5:51 PM, Sangjin Han via swift-dev <swift-dev@swift.org> wrote:

I'm a newbie in this world. I have a qustion.

Is there any reason to prefer pthread_mutex_* to std::mutex in the swift
stdlib ?

Nowadays in C++, the std::mutex is the standard and more portable than
pthread_mutex_*.

If I'm porting the swift stdlib to non-POSIX system, which method should I
choose ?

Adding #if-#else-#endif or changing pthread to std::mutex ?

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/


(John McCall) #3

The runtime already uses std::mutex in a few places, so if there were portability problems with it, we’d already be hitting them. I agree that we should prefer std::mutex where possible.

John.

···

On Feb 25, 2016, at 5:51 PM, Sangjin Han via swift-dev <swift-dev@swift.org> wrote:
I'm a newbie in this world. I have a qustion.

Is there any reason to prefer pthread_mutex_* to std::mutex in the swift stdlib ?

Nowadays in C++, the std::mutex is the standard and more portable than pthread_mutex_*.

If I'm porting the swift stdlib to non-POSIX system, which method should I choose ?

Adding #if-#else-#endif or changing pthread to std::mutex ?


(Joe Groff) #4

We should definitely standardize on one or the other, at least. std::mutex would definitely make things easier for ports to less POSIXish platforms.

-Joe

···

On Feb 26, 2016, at 10:35 AM, John McCall via swift-dev <swift-dev@swift.org> wrote:

On Feb 25, 2016, at 5:51 PM, Sangjin Han via swift-dev <swift-dev@swift.org> wrote:
I'm a newbie in this world. I have a qustion.

Is there any reason to prefer pthread_mutex_* to std::mutex in the swift stdlib ?

Nowadays in C++, the std::mutex is the standard and more portable than pthread_mutex_*.

If I'm porting the swift stdlib to non-POSIX system, which method should I choose ?

Adding #if-#else-#endif or changing pthread to std::mutex ?

The runtime already uses std::mutex in a few places, so if there were portability problems with it, we’d already be hitting them. I agree that we should prefer std::mutex where possible.