~bkerensa/ubuntu/raring/valgrind/merge-from-deb

« back to all changes in this revision

Viewing changes to callgrind/tests/threads.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2006-06-26 00:17:17 UTC
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20060626001717-qi51nzty57cb12q6
Tags: upstream-3.2.0
Import upstream version 3.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* A simple example with 4 threads */
 
2
 
 
3
#include <pthread.h>
 
4
#include <unistd.h>
 
5
 
 
6
double a[1000];
 
7
 
 
8
static void init()
 
9
{
 
10
        int i;
 
11
        for(i=0;i<1000;i++) a[i] = (double)i;
 
12
}
 
13
 
 
14
static void *th(void *v)
 
15
{
 
16
        double sum = 0.0;
 
17
        int i,j;
 
18
 
 
19
        for(j=0;j<1000;j++)
 
20
                for(i=0;i<1000;i++)
 
21
                        sum += a[i];
 
22
 
 
23
        *( (double*)v ) = sum;
 
24
 
 
25
        /* make sure that no threads is so fast that it finishes
 
26
         * before last thread is created, thus reusing the TID */
 
27
        sleep(1);
 
28
 
 
29
        return 0;
 
30
}
 
31
 
 
32
int main()
 
33
{
 
34
        pthread_t t[4];
 
35
        double sum[4];
 
36
        int i;
 
37
 
 
38
        init();
 
39
 
 
40
        for(i=0;i<4;i++)
 
41
                pthread_create(&t[i], NULL, th, &sum[i]);       
 
42
 
 
43
        for(i=0;i<4;i++)
 
44
                pthread_join(t[i], NULL);
 
45
 
 
46
        return 0;
 
47
}