/docs/MyDocs

To get this branch, use:
bzr branch http://darksoft.org/webbzr/docs/MyDocs

« back to all changes in this revision

Viewing changes to Development/languages/C/Samples/pthreads/pthreads.c

  • Committer: Suren A. Chilingaryan
  • Date: 2009-04-09 03:21:08 UTC
  • Revision ID: csa@dside.dyndns.org-20090409032108-w4edamdh4adrgdu3
import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <pthread.h>
 
3
 
 
4
int n=0;
 
5
pthread_mutex_t n_mutex = PTHREAD_MUTEX_INITIALIZER;
 
6
pthread_cond_t n_cond = PTHREAD_COND_INITIALIZER;
 
7
pthread_mutex_t n_cond_mutex = PTHREAD_MUTEX_INITIALIZER;
 
8
 
 
9
void *reader(void *args) {
 
10
        while (1) {
 
11
                pthread_mutex_lock(&n_mutex);
 
12
                n=n+1;
 
13
                pthread_mutex_unlock(&n_mutex);
 
14
                pthread_cond_broadcast(&n_cond);
 
15
        }
 
16
}
 
17
 
 
18
 
 
19
void main() {
 
20
        pthread_t trd;
 
21
 
 
22
        pthread_mutex_lock(&n_cond_mutex);
 
23
                
 
24
        if (pthread_create(&trd,NULL,reader,NULL)) {
 
25
                printf("Error\n");
 
26
                exit(0);
 
27
        } 
 
28
        
 
29
        while (1) {
 
30
                pthread_cond_wait(&n_cond,&n_cond_mutex);
 
31
                pthread_mutex_lock(&n_mutex);
 
32
                printf("%u ",n);
 
33
                pthread_mutex_unlock(&n_mutex);
 
34
        }
 
35
}