source: CIVL/examples/pthread/esbmc/indexer_true.c@ 83af34d

1.23 2.0 main test-branch
Last change on this file since 83af34d was e3151da, checked in by Ziqing Luo <ziqing@…>, 11 years ago

re-organized example directory

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1763 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include <stdlib.h>
2#include <pthread.h>
3
4#define SIZE 128
5#define MAX 4
6#define NUM_THREADS 13
7
8int table[SIZE];
9pthread_mutex_t cas_mutex[SIZE];
10
11pthread_t tids[NUM_THREADS];
12
13
14int cas(int * tab, int h, int val, int new_val)
15{
16 int ret_val = 0;
17 pthread_mutex_lock(&cas_mutex[h]);
18
19
20 if ( tab[h] == val ) {
21 tab[h] = new_val;
22 ret_val = 1;
23 }
24
25 pthread_mutex_unlock(&cas_mutex[h]);
26
27
28 return ret_val;
29}
30
31
32
33void * thread_routine(void * arg)
34{
35 int tid;
36 int m = 0, w, h;
37 tid = *((int *)arg);
38
39 while(1){
40 if ( m < MAX ){
41 w = (++m) * 11 + tid;
42 }
43 else{
44 pthread_exit(NULL);
45 }
46
47 h = (w * 7) % SIZE;
48
49 if (h<0)
50 {
51 ERROR: goto ERROR;
52 ;
53 }
54
55 while ( cas(table, h, 0, w) == 0){
56 h = (h+1) % SIZE;
57 }
58 }
59
60}
61
62
63int main()
64{
65 int i, arg;
66
67 for (i = 0; i < SIZE; i++)
68 pthread_mutex_init(&cas_mutex[i], NULL);
69
70 for (i = 0; i < NUM_THREADS; i++){
71 arg=i;
72 pthread_create(&tids[i], NULL, thread_routine, &arg);
73 }
74
75 for (i = 0; i < NUM_THREADS; i++){
76 pthread_join(tids[i], NULL);
77 }
78
79 for (i = 0; i < SIZE; i++){
80 pthread_mutex_destroy(&cas_mutex[i]);
81 }
82
83}
Note: See TracBrowser for help on using the repository browser.