/** * @brief Creates a new thread * @param thread: A pointer to store ID of the new thread * @param attr: Attributes of thw new thread * @param start_routine: execution method of the new thread * @param arg: the argument of start_routine */ intpthread_create(pthread_t *thread, constpthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
/** * @brief Waits for the thread specified by thread ID to terminate * @param thread: Joinable thread * @param retval: The result of thread execution method */ intpthread_join(pthread_t thread, void **retval);
/** * @brief lock a reader-writer lock object for reading * @param A pointer to reader-writer lock */ intpthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
/** * @brief Perform the operation suggested by the name, * @return The value that had previously been in memory */ type __sync_fetch_and_add(type *ptr, type value_); type __sync_fetch_and_sub(type *ptr, type value_); type __sync_fetch_and_or(type *ptr, type value_); type __sync_fetch_and_and(type *ptr, type value_); type __sync_fetch_and_xor(type *ptr, type value_); type __sync_fetch_and_nand(type *ptr, type value_);
/** * @brief Perform the operation suggested by the name * @return The new value after operation */ type __sync_add_and_fetch(type *ptr, type value_); type __sync_sub_and_fetch(type *ptr, type value_); type __sync_or_and_fetch(type *ptr, type value_); type __sync_and_and_fetch(type *ptr, type value_); type __sync_xor_and_fetch(type *ptr, type value_); type __sync_nand_and_fetch(type *ptr, type value_);
/** * @brief Perform an atomic compare and swap, * if the current value of *ptr is oldval, * then write newval into *ptr. * @return The contents of *ptr before the operation */ type __sync_val_compare_and_swap(type *ptr, type oldval type newval);
/** * @brief Issues a full memory barrier. */ void __sync_synchronize();
C11也提供了atomic operations:
#include<stdatomic.h> /** * @brief Atomically loads atomic variable. * @param obj: pointer to the atomic object to access. * @return The current value pointed to obj */ C atomic_load(constvolatile A* obj);
/** * @brief Atomically replaces the value of the variable * pointed to `obj` with `desired`. * @param obj: pointer to the atomic object to modify * @param desired: The value to be stored in the atomic object. */ voidatomic_store(volatile A* obj , C desired);
/** * @brief Atomically perform obj the operation suggested by the name with arg * @return The value obj held previously. */ C atomic_fetch_add(volatile A* obj, M arg); C atomic_fetch_sub(volatile A* obj, M arg); C atomic_fetch_or(volatile A* obj, M arg); C atomic_fetch_xor(volatile A* obj, M arg); C atomic_fetch_and(volatile A* obj, M arg);
/** * @brief Establishes memory synchronization ordering of * non-atomic and relaxed atomic accesses * @param order: the memory ordering executed by this fence */ voidatomic_thread_fence(memory_order order);
/** * @brief Create a thread-specific data key visible to all threads in the process * @return If successful, return 0. Otherwise, return an error number */ intpthread_key_create(pthread_key_t *key, void (*destructor)(void *));
/** * @brief Deletes thread-specific data keys created with pthread_key_create(), * The data associated with key do not need to be NULL when the key is deleted. * @return If successful, return 0. Otherwise, return an error number */ intpthread_key_delete(pthread_key_t key);
/** * @brief Associates the data `value` with the key `key`. * @return If successful, return 0. Otherwise, return -1 and set errno */ intpthread_setspecific(pthread_key_t key, void *value);
/** * @brief Get the value currently bound to the key on the calling thread */ void *pthread_getspecific(pthread_key_t key);