~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to debuggers/gdb/tests/threads/threads.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This is a test program for KDevelop GDB debugger support.
 
2
 
 
3
   There are two worker threads, they are programmed to call
 
4
   the 'foo' function in strictly interleaved fashion.
 
5
*/
 
6
 
 
7
#include <pthread.h>
 
8
#include <stdio.h>
 
9
#include <unistd.h>
 
10
 
 
11
int schedule[] = {1, 2};
 
12
int schedule_size = sizeof(schedule)/sizeof(schedule[0]);
 
13
int index = 0;
 
14
int exit = 0;
 
15
 
 
16
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
17
pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
 
18
 
 
19
void foo(int thread, int i)
 
20
{
 
21
    printf ("hi there, from thread %d on iteration %d\n", thread, i);
 
22
}
 
23
 
 
24
void runner(int id)
 
25
{
 
26
    for(int i = 0; i < 1000000 && !exit; ++i)
 
27
    {
 
28
        pthread_mutex_lock(&mutex);
 
29
 
 
30
        while (schedule[index] != id) {
 
31
            pthread_cond_wait(&condition, &mutex);
 
32
        }
 
33
 
 
34
        foo(id, i);
 
35
        
 
36
        ++index;
 
37
        if (index >= schedule_size)
 
38
            index = 0;
 
39
               
 
40
        pthread_cond_broadcast(&condition);
 
41
        pthread_mutex_unlock(&mutex);
 
42
 
 
43
        sleep(1);
 
44
    }
 
45
}
 
46
 
 
47
void* thread(void* p)
 
48
{
 
49
    runner((int)p);
 
50
    return NULL;
 
51
}
 
52
 
 
53
int main()
 
54
{
 
55
    pthread_t p1, p2;
 
56
    
 
57
    pthread_create(&p1, 0, &thread, (void*)1);
 
58
    pthread_create(&p2, 0, &thread, (void*)2);    
 
59
    
 
60
    pthread_join(p1, 0);
 
61
    pthread_join(p2, 0);
 
62
    
 
63
    return 0;
 
64
}