pthreads中的内存模型规范 [英] Memory model spec in pthreads

查看:75
本文介绍了pthreads中的内存模型规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以保证何时使用pthread在一个线程中进行的内存写入在其他线程中可见?

Are there any guarantees on when a memory write in one thread becomes visible in other threads using pthreads?

与Java相比,Java语言规范具有

Comparing to Java, the Java language spec has a section that specifies the interaction of locks and memory that makes it possible to write portable multi-threaded Java code.

是否有相应的pthreads规范?

Is there a corresponding pthreads spec?

当然,您总是可以使共享数据易失,但这不是我要的.

Sure, you can always go and make shared data volatile, but that is not what I'm after.

如果这取决于平台,那么是否存在事实上的标准?还是应该使用另一个线程库?

If this is platform dependent, is there a de facto standard? Or should another threading library be used?

推荐答案

POSIX在 4.11内存同步:

应用程序应确保限制一个以上控制线程(线程或进程)对任何内存位置的访问,以使任何控制线程都无法读取或修改一个内存位置,而另一个控制线程可以对其进行修改.使用同步线程执行并相对于其他线程同步内存的函数来限制此类访问.以下功能可将内存与其他线程进行同步:

Applications shall ensure that access to any memory location by more than one thread of control (threads or processes) is restricted such that no thread of control can read or modify a memory location while another thread of control may be modifying it. Such access is restricted using functions that synchronize thread execution and also synchronize memory with respect to other threads. The following functions synchronize memory with respect to other threads:

  • fork()
  • pthread_barrier_wait()
  • pthread_cond_broadcast()
  • pthread_cond_signal()
  • pthread_cond_timedwait()
  • pthread_cond_wait()
  • pthread_create()
  • pthread_join()
  • pthread_mutex_lock()
  • pthread_mutex_timedlock()
  • pthread_mutex_trylock()
  • pthread_mutex_unlock()
  • pthread_spin_lock()
  • pthread_spin_trylock()
  • pthread_spin_unlock()
  • pthread_rwlock_rdlock()
  • pthread_rwlock_timedrdlock()
  • pthread_rwlock_timedwrlock()
  • pthread_rwlock_tryrdlock()
  • pthread_rwlock_trywrlock()
  • pthread_rwlock_unlock()
  • pthread_rwlock_wrlock()
  • sem_post()
  • sem_timedwait()
  • sem_trywait()
  • sem_wait()
  • semctl()
  • semop()
  • wait()
  • waitpid()
  • fork()
  • pthread_barrier_wait()
  • pthread_cond_broadcast()
  • pthread_cond_signal()
  • pthread_cond_timedwait()
  • pthread_cond_wait()
  • pthread_create()
  • pthread_join()
  • pthread_mutex_lock()
  • pthread_mutex_timedlock()
  • pthread_mutex_trylock()
  • pthread_mutex_unlock()
  • pthread_spin_lock()
  • pthread_spin_trylock()
  • pthread_spin_unlock()
  • pthread_rwlock_rdlock()
  • pthread_rwlock_timedrdlock()
  • pthread_rwlock_timedwrlock()
  • pthread_rwlock_tryrdlock()
  • pthread_rwlock_trywrlock()
  • pthread_rwlock_unlock()
  • pthread_rwlock_wrlock()
  • sem_post()
  • sem_timedwait()
  • sem_trywait()
  • sem_wait()
  • semctl()
  • semop()
  • wait()
  • waitpid()

对于给定的pthread_once_t对象,pthread_once()函数应为每个线程中的第一次调用同步内存.

The pthread_once() function shall synchronize memory for the first call in each thread for a given pthread_once_t object.

如果互斥锁类型为PTHREAD_MUTEX_RECURSIVE并且调用线程已经拥有该互斥锁,则pthread_mutex_lock()函数无需同步内存.如果互斥锁类型为PTHREAD_MUTEX_RECURSIVE并且互斥锁的锁数大于1,则pthread_mutex_unlock()函数无需同步内存.

The pthread_mutex_lock() function need not synchronize memory if the mutex type if PTHREAD_MUTEX_RECURSIVE and the calling thread already owns the mutex. The pthread_mutex_unlock() function need not synchronize memory if the mutex type is PTHREAD_MUTEX_RECURSIVE and the mutex has a lock count greater than one.

除非另有明确说明,否则如果上述函数之一返回错误,则不确定调用是否导致内存同步.

Unless explicitly stated otherwise, if one of the above functions returns an error, it is unspecified whether the invocation causes memory to be synchronized.

应用程序可能允许多个控制线程同时读取一个内存位置.

Applications may allow more than one thread of control to read a memory location simultaneously.

这篇关于pthreads中的内存模型规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆