Sponsored Content
Top Forums Programming pthread_mutex_lock in ANSI C vs using Atomic builtins of GCC Post 302448684 by Corona688 on Thursday 26th of August 2010 04:23:43 PM
Old 08-26-2010
"volatile" has nothing to do with atomic, "volatile" just tells the compiler to never assume that variable is whatever it set it to last. (otherwise, it might assume it never changes and hardcode it in the instructions.)

No variables (except the gcc atomic extensions) are guaranteed to be atomic, at all, ever. There's other problems than being atomic, too -- on a multicore system, one core might have a different copy of the memory still in cache, causing even "volatile" to fail. You need to inform other cores you're modifying values out from under them, which is what memory barriers are for. pthreads does memory barriers for you. I don't know if gcc atomic ops do.

Last edited by Corona688; 08-26-2010 at 05:33 PM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert file from Unix - ANSI to PC - ANSI

Hi, I am creating a file in Unix using a shell script. The file is getting created in the Unix - ANSI format. My requirement is to convert it to the PC - ANSI format. Can anyone tell me how to do this? Thanks, Sunil (0 Replies)
Discussion started by: ssmallya
0 Replies

2. Programming

pthread_rwlock_lock vs pthread_mutex_lock

I have been wondering what the difference between pthread_rwlock_lock and pthread_mutex_lock is. Both these routines acquire an exclusive rw lock on an enclosed region. So I performed a simple experiment in which I execute both these routines multiple times in a loop. Here are the results:... (1 Reply)
Discussion started by: kmehta
1 Replies

3. Programming

Interesting issue with pthread_mutex_lock and siglongjmp in AIX 5.3 (and no other OS)

Executive summary: Code (posted below) cores in AIX 5.3, despite being compiled and run successfully on several other operating systems. Code is attempting to verify that pthread_mutex_lock can be successfully aborted by siglongjmp. I do not believe this is an unreasonable requirement. If... (1 Reply)
Discussion started by: DreamWarrior
1 Replies

4. Shell Programming and Scripting

program name and function name builtins

Hi Is there a way to get the program/script name or function name usng built ins. Like in many languages arg holds the program name regards (2 Replies)
Discussion started by: xiamin
2 Replies

5. Programming

why the implementatoin of Bakery algorithm in ANSI C does not work in ANSI C

I follow the description of wiki (Lamport's bakery algorithm - Wikipedia, the free encyclopedia), then implement that algorithm in C, but it doesn't work, Starving is still here, is the implementation worry? Only print out: Thread ID: 0 START! Thread ID: 0 END! Thread ID: 0 START!... (2 Replies)
Discussion started by: sehang
2 Replies

6. UNIX for Advanced & Expert Users

Writing Custom Builtins for KSH93

I am looking to create some ksh93 extensions using the custom builtin feature. I can successfully create a builtin function, load it using the builtin -f command and get an output. However, I want to get/set values of KSH variables from within my built-in. For example, lets say I am creating... (2 Replies)
Discussion started by: a_programmer
2 Replies

7. UNIX for Dummies Questions & Answers

Why does /bin contain binaries for builtins?

Why do shell builtins like echo and pwd have binaries in /bin? When I do which pwd, I get the one in /bin. that means that I am not using the builtin version? What determines which one gets used? Is the which command a definitive way to determine what is being run when I enter pwd? (16 Replies)
Discussion started by: glev2005
16 Replies

8. Programming

Using ANSI color codes in gcc compiled program

I have put some yellow color codes and works well. I call the funstion using print_usage(stderr, 0); I would like to know if there is any way, to store the ansi color codes in variables and then call them inside fprintf. Or have a format followed by the strings I want to output. ... (5 Replies)
Discussion started by: kristinu
5 Replies

9. UNIX for Dummies Questions & Answers

Shell and bash builtins...

Not sure if this is the right forum but I have collated a listing of shell and bash builtins. Builtins is a loose word and may include the '/bin' drawer/folder/directory but NOT any others in the path list. In the case of my Macbook Pro, OSX 10.7.5 the enabled internals is also listed... ... (1 Reply)
Discussion started by: wisecracker
1 Replies
ATOMIC_VAR_INIT(3)					   BSD Library Functions Manual 					ATOMIC_VAR_INIT(3)

NAME
ATOMIC_VAR_INIT, atomic_init, atomic_load, atomic_store, atomic_exchange, atomic_compare_exchange_strong, atomic_compare_exchange_weak, atomic_fetch_add, atomic_fetch_and, atomic_fetch_or, atomic_fetch_sub, atomic_fetch_xor, atomic_is_lock_free -- type-generic atomic opera- tions SYNOPSIS
#include <stdatomic.h> _Atomic(T) v = ATOMIC_VAR_INIT(c); void atomic_init(_Atomic(T) *object, T value); T atomic_load(_Atomic(T) *object); T atomic_load_explicit(_Atomic(T) *object, memory_order order); void atomic_store(_Atomic(T) *object, T desired); void atomic_store_explicit(_Atomic(T) *object, T desired, memory_order order); T atomic_exchange(_Atomic(T) *object, T desired); T atomic_exchange_explicit(_Atomic(T) *object, T desired, memory_order order); _Bool atomic_compare_exchange_strong(_Atomic(T) *object, T *expected, T desired); _Bool atomic_compare_exchange_strong_explicit(_Atomic(T) *object, T *expected, T desired, memory_order success, memory_order failure); _Bool atomic_compare_exchange_weak(_Atomic(T) *object, T *expected, T desired); _Bool atomic_compare_exchange_weak_explicit(_Atomic(T) *object, T *expected, T desired, memory_order success, memory_order failure); T atomic_fetch_add(_Atomic(T) *object, T operand); T atomic_fetch_add_explicit(_Atomic(T) *object, T operand, memory_order order); T atomic_fetch_and(_Atomic(T) *object, T operand); T atomic_fetch_and_explicit(_Atomic(T) *object, T operand, memory_order order); T atomic_fetch_or(_Atomic(T) *object, T operand); T atomic_fetch_or_explicit(_Atomic(T) *object, T operand, memory_order order); T atomic_fetch_sub(_Atomic(T) *object, T operand); T atomic_fetch_sub_explicit(_Atomic(T) *object, T operand, memory_order order); T atomic_fetch_xor(_Atomic(T) *object, T operand); T atomic_fetch_xor_explicit(_Atomic(T) *object, T operand, memory_order order); _Bool atomic_is_lock_free(const _Atomic(T) *object); DESCRIPTION
The header <stdatomic.h> provides type-generic macros for atomic operations. Atomic operations can be used by multithreaded programs to pro- vide shared variables between threads that in most cases may be modified without acquiring locks. Atomic variables are declared using the _Atomic() type specifier. These variables are not type-compatible with their non-atomic counter- parts. Depending on the compiler used, atomic variables may be opaque and can therefore only be influenced using the macros described. The atomic_init() macro initializes the atomic variable object with a value. Atomic variables can be initialized while being declared using ATOMIC_VAR_INIT(). The atomic_load() macro returns the value of atomic variable object. The atomic_store() macro sets the atomic variable object to its desired value. The atomic_exchange() macro combines the behaviour of atomic_load() and atomic_store(). It sets the atomic variable object to its desired value and returns the original contents of the atomic variable. The atomic_compare_exchange_strong() macro stores a desired value into atomic variable object, only if the atomic variable is equal to its expected value. Upon success, the macro returns true. Upon failure, the desired value is overwritten with the value of the atomic variable and false is returned. The atomic_compare_exchange_weak() macro is identical to atomic_compare_exchange_strong(), but is allowed to fail even if atomic variable object is equal to its expected value. The atomic_fetch_add() macro adds the value operand to atomic variable object and returns the original contents of the atomic variable. The atomic_fetch_and() macro applies the and operator to atomic variable object and operand and stores the value into object, while returning the original contents of the atomic variable. The atomic_fetch_or() macro applies the or operator to atomic variable object and operand and stores the value into object, while returning the original contents of the atomic variable. The atomic_fetch_sub() macro subtracts the value operand from atomic variable object and returns the original contents of the atomic vari- able. The atomic_fetch_xor() macro applies the xor operator to atomic variable object and operand and stores the value into object, while returning the original contents of the atomic variable. The atomic_is_lock_free() macro returns whether atomic variable object uses locks when using atomic operations. BARRIERS
The atomic operations described previously are implemented in such a way that they disallow both the compiler and the executing processor to re-order any nearby memory operations across the atomic operation. In certain cases this behaviour may cause suboptimal performance. To mitigate this, every atomic operation has an _explicit() version that allows the re-ordering to be configured. The order parameter of these _explicit() macros can have one of the following values. memory_order_relaxed No operation orders memory. memory_order_consume Perform consume operation. memory_order_acquire Acquire fence. memory_order_release Release fence. memory_order_acq_rel Acquire and release fence. memory_order_seq_cst Sequentially consistent acquire and release fence. The previously described macros are identical to the _explicit() macros, when order is memory_order_seq_cst. COMPILER SUPPORT
These atomic operations are typically implemented by the compiler, as they must be implemented type-generically and must often use special hardware instructions. As this interface has not been adopted by most compilers yet, the <stdatomic.h> header implements these macros on top of existing compiler intrinsics to provide forward compatibility. This means that certain aspects of the interface, such as support for different barrier types may simply be ignored. When using GCC, all atomic operations are executed as if they are using memory_order_seq_cst. Instead of using the atomic operations provided by this interface, ISO/IEC 9899:2011 (``ISO C11'') allows the atomic variables to be modified directly using built-in language operators. This behaviour cannot be emulated for older compilers. To prevent unintended non-atomic access to these variables, this header file places the atomic variable in a structure when using an older compiler. When using GCC on architectures on which it lacks support for built-in atomic intrinsics, these macros may emit function calls to fallback routines. These fallback routines are only implemented for 32-bits and 64-bits datatypes, if supported by the CPU. SEE ALSO
pthread(3), atomic(9) STANDARDS
These macros attempt to conform to ISO/IEC 9899:2011 (``ISO C11''). HISTORY
These macros appeared in FreeBSD 10.0. AUTHORS
Ed Schouten <ed@FreeBSD.org> David Chisnall <theraven@FreeBSD.org> BSD
December 27, 2011 BSD
All times are GMT -4. The time now is 06:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy