Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,176 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,397 people online right now. Registration is fast and FREE... Join Now!




pthread

 
Reply to this topicStart new topic

pthread

kornni
8 Jul, 2008 - 09:23 AM
Post #1

New D.I.C Head
*

Joined: 1 Jul, 2008
Posts: 9


My Contributions
why not print z = 0.1
z=0.2
z=0.3,
but print z = 0.00

CODE


#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM_THREADS     3
          
#define pi     4 * atan(1.0)
          
double x,s,u,z;
          
void *PrintHello(void *threadid)
{    x += 0.1;  
     int tid;
     tid = (int)threadid;
     printf("Hello World! It's me, thread #%d!\n", tid);
//     printf("*** %f ****\n",x);
    
     z = (tid+1)/(10);
     printf("\n----%f----\n", z);

     u = 100;  
    
    for(s = 0.0; s<0.61; s += 0.01)
    {  
          u = u*sin((z-0.1)*pi) - u*sin(z*pi) + u*sin((z+0.1)*pi);
          printf("%f \n",u);      
    }
    pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
     pthread_t threads[NUM_THREADS];
     x = 0;
     int rc, t;
     for(t=0; t<NUM_THREADS; t++){

         printf("In main: creating thread %d\n", t);

         rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
         if (rc){
              printf("ERROR; return code from pthread_create() is %d\n", rc);
              exit(-1);
         }
     }
     pthread_exit(NULL);
}

User is offlineProfile CardPM
+Quote Post

Hyper_Eye
RE: Pthread
8 Jul, 2008 - 12:56 PM
Post #2

D.I.C Head
**

Joined: 13 Sep, 2007
Posts: 72



Thanked: 5 times
My Contributions
There are few things you need to know here.

First, the way you are approaching this is dangerous to your data (if these values or the data being manipulated were important.) You are allowing independent threads to access global data with no restrictions. There are a few things that can happen here. Let's pretend that there only two threads being thread A and B and let's stick with what you were doing to x.

1) Thread A evaluates x and finds it's value to be 0. According to instruction it proceeds to increment x to 0.1. In the meantime Thread B has evaluated x and has found it's value to be 0. It proceeds to increment it's value to 0.1. Oops. You expected a value of 0.2 but you don't get it. That's if they aren't messing with that memory at the same time and you just end up with junk in there.

2) Thread A increments x to 0.1 and then B increments it to 0.2 which is what you are expecting. Then thread A and B both go to print x. You see:

0.2
0.2

The reason you see that is because both threads are printing the same global variable as opposed to a local copy. The value of the variable is 0.2 for both threads because it is the same memory.

You really need to make sure you understand how accessing items in memory works, scope, and thread execution before you start making threaded apps. With those things understood you then need to learn about mutexes and conditions in pthreads. These will allow you to change those global variables within your threads without the threads stepping on each others toes.
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Pthread
9 Jul, 2008 - 06:05 PM
Post #3

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,461



Thanked: 10 times
Dream Kudos: 325
My Contributions
http://www.yolinux.com/TUTORIALS/LinuxTuto...sixThreads.html

I had a link to a better tutorial but I can't find it, sorry. sad.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 10:47AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month