source: CIVL/examples/pthread/CDAC/pthread-demo-datarace.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/***********************************************************************************
2 C-DAC Tech Workshop : hyPACK-2013
3 October 15-18,2013
4 Example : pthread-demo-datarace.c
5
6 Objective : Write Pthread code to illustrate Data Race Condition
7 and its solution using MUTEX.
8
9 Input : Nothing.
10
11 Output : Value of Global variable with and without using Mutex.
12
13 Created :MAY-2013
14
15 E-mail : hpcfte@cdac.in
16
17****************************************************************************/
18
19#include <pthread.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <unistd.h>
23
24
25int myglobal; // declaration of global variable
26pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER; // initialization of MUTEX variable
27
28
29void *thread_function_datarace(void *arg) // Function which operates on myglobal without using mutex
30{
31 int i,j;
32 for ( i=0; i<20; i++ )
33 {
34 j=myglobal;
35 j=j+1;
36 printf("\nIn thread_function_datarace..\t"); // Incrementing j and assign myglobal value of j
37// fflush(stdout);
38 sleep(1);
39 myglobal=j;
40 }
41 return NULL;
42}
43
44void *thread_function_mutex(void *arg) // Function which operates on myglobal using mutex
45{
46 int i,j;
47 for ( i=0; i<20; i++ )
48 {
49 pthread_mutex_lock(&mymutex);
50 j=myglobal;
51 j=j+1;
52 printf("\nIn thread_function_mutex..\t");
53 // fflush(stdout);
54 sleep(0.1);
55 myglobal=j;
56 pthread_mutex_unlock(&mymutex);
57 }
58 return NULL;
59}
60
61int main(void)
62{
63
64 pthread_t mythread;
65 int i;
66
67 if ( pthread_create( &mythread, NULL, thread_function_datarace, NULL) ) // calling thread_function_datarace
68 {
69 printf("error creating thread.");
70 abort();
71 }
72
73 printf("\n\t\t---------------------------------------------------------------------------");
74 printf("\n\t\t Centre for Development of Advanced Computing (C-DAC)");
75 printf("\n\t\t Email : hpcfte@cdac.in");
76 printf("\n\t\t---------------------------------------------------------------------------");
77 printf("\n\t\t Objective : Pthread code to illustrate data race condition and its solution \n ");
78 printf("\n\t\t..........................................................................\n");
79
80
81
82 for ( i=0; i<20; i++)
83 {
84 myglobal=myglobal+1;
85 printf("\nIn main..\t");
86 // fflush(stdout);
87 sleep(1);
88 }
89
90 if ( pthread_join ( mythread, NULL ) )
91 {
92 printf("error joining thread.");
93 abort();
94 }
95
96 printf("\nValue of myglobal in thread_function_datarace is : %d\n",myglobal);
97
98 printf("\n ----------------------------------------------------------------------------------------------------\n");
99
100 myglobal = 0;
101
102 if ( pthread_create( &mythread, NULL, thread_function_mutex, NULL) ) // calling thread_function_mutex
103 {
104 printf("error creating thread.");
105 abort();
106 }
107
108 for ( i=0; i<20; i++)
109 {
110 pthread_mutex_lock(&mymutex);
111 myglobal=myglobal+1;
112 pthread_mutex_unlock(&mymutex);
113 printf("\nIn main..\t");
114 // fflush(stdout);
115 sleep(1);
116 }
117
118 if ( pthread_join ( mythread, NULL ) )
119 {
120 printf("error joining thread.");
121 abort();
122 }
123
124 printf("\n");
125 printf("\nValue of myglobal in thread_function_mutex is : %d\n",myglobal);
126
127
128 exit(0);
129
130}
131
Note: See TracBrowser for help on using the repository browser.