Search
 
SCRIPT & CODE EXAMPLE
 

C

atomic variable c

#include <stdio.h>
#include <threads.h>
#include <stdatomic.h>

atomic_int acnt;
int cnt;

int f(void* thr_data)
{
    (void)thr_data;
    for(int n = 0; n < 1000; ++n) {
        ++cnt;
        ++acnt;
        // for this example, relaxed memory order is sufficient, e.g.
        // atomic_fetch_add_explicit(&acnt, 1, memory_order_relaxed);
    }
    return 0;
}

int main(void)
{
    thrd_t thr[10];
    for(int n = 0; n < 10; ++n)
        thrd_create(&thr[n], f, NULL);
    for(int n = 0; n < 10; ++n)
        thrd_join(thr[n], NULL);

    printf("The atomic counter is %u
", acnt);
    printf("The non-atomic counter is %u
", cnt);
}
Comment

PREVIOUS NEXT
Code Example
C :: c concatenate strings 
C :: how to mutex lock in c 
C :: c program 
C :: linear search program in c 
C :: c programming itoa() example 
C :: c integer to string 
C :: reverse of a string in c 
C :: how to print the first character of a string in c 
C :: how to combine strings in c 
C :: how to open a website in c 
C :: list c 
C :: sum average min max in c array 
C :: c if 
C :: merge sort for strings in c 
C :: 2 bytes integer c 
C :: count distinct characters in a string C 
C :: geom boxplot remove outliers 
C :: ** in c 
C :: bootstrap 4 forms 
C :: how to take blank space in c scanf 
C :: how to read 2d array from a file in c 
C :: getchar in c 
C :: Program to Check Whether Character is Lowercase or Not without using islower function 
C :: absolute value of intel intrinsic 
C :: fahrenheit to celcius 
C :: stack and heap memorym in ram 
C :: armstrong in c 
C :: boolean operators in c++ 
C :: yt derived field 
C :: float da 4 byte 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =