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

问题描述

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

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

与Java相比,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内存同步:

  • 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中的内存模型规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:42