Search
 
SCRIPT & CODE EXAMPLE
 

C

mutex c

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#ifndef NUM_THREADS
#define NUM_THREADS 4
#endif

int shared = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void *func3(void* param)
{
    pthread_mutex_lock(&mutex);
    printf("Incrementing the shared variable...
");
    for (int i = 0; i < 10000; ++i) {
        shared += 1;
    }
    pthread_mutex_unlock(&mutex);
    return 0;
}

int main()
{
    pthread_t threads[NUM_THREADS];

    for (int i = 0; i < NUM_THREADS; ++i) {
        pthread_create(&threads[i], NULL, func3, NULL);
    }

    for (int i = 0; i < NUM_THREADS; ++i) {
        pthread_join(threads[i], NULL);
    }

    printf("%d
", shared);
    exit(EXIT_SUCCESS);
}
Comment

PREVIOUS NEXT
Code Example
C :: Graphics in C Draw Circle 
C :: connect servo to arduino 
C :: list c 
C :: c print multiple variables 
C :: pthread c 
C :: how make a character in c scanf 
C :: c string 
C :: c program to print the multiplication table 
C :: go Iterating over an array using a range operator 
C :: .sh template 
C :: make a function makefile 
C :: read from stdin c 
C :: geom boxplot remove outliers 
C :: search in gz file 
C :: houdini vex loop over points 
C :: c program to find the frequency of characters in a string 
C :: c check if character is an alphabet 
C :: link list c 
C :: multiplication of matrix in c 
C :: c print 2d array 
C :: ecrire programme en C une fonction remplir tableau et un fonction inverser 
C :: how to insert elements in and array and print it in c 
C :: stddef.h 
C :: define constant c 
C :: compile in c 
C :: example of header file in c 
C :: ansi c read write bmp 
C :: metw.cc 
C :: <fileset joomla 
C :: c create array 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =