Home › Forums › C Programming › threads Win32 <-> Linux › Reply To: threads Win32 <-> Linux
April 29, 2008 at 11:15 am
#3387
Priyansh Agrawal
Participant
Ok, I found the problem/solution but I’m not satisfied: All works fine when the optimizations in gcc are turned off.
Each optimization (O, O1, O2, O3, Os) will cause an endless loop.
Here an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include <br /> #include <br /> <br /> int wait1,wait2;<br /> <br /> void *mythread(void *ptr)<br /> {<br /> int *parm;<br /> int i;<br /> <br /> parm=(int *)ptr;<br /> <br /> for (i=0;i<10;i++)<br /> {<br /> // branch for thread no 1<br /> if (parm[0])<br /> {<br /> printf("nLoop %in",i);<br /> printf("Thread 1 started and waiting for thread 2 ...n");<br /> wait2=0;<br /> while (wait1);<br /> wait1=1;<br /> <br /> printf("Thread 1 resumed.n");<br /> }<br /> // branch for thread no 2<br /> else<br /> {<br /> while (wait2);<br /> wait2=1;<br /> printf("Thread 2 started and telling Thread 1 to resume ...n");<br /> wait1=0;<br /> }<br /> }<br /> <br /> return NULL;<br /> }<br /> <br /> int main()<br /> {<br /> pthread_t thr1,thr2;<br /> int parm1[1],parm2[1];<br /> <br /> wait1=1;<br /> wait2=1;<br /> parm1[0]=1;<br /> parm2[0]=0;<br /> <br /> pthread_create(&thr1,NULL,mythread,(void *)parm1);<br /> pthread_create(&thr2,NULL,mythread,(void *)parm2);<br /> <br /> pthread_join(thr1,NULL);<br /> pthread_join(thr2,NULL);<br /> <br /> return 0;<br /> } |