Pthreads...

This is a discussion on Pthreads... within the C++ Programming forums, part of the General Programming Boards category; I started using pthreads recently and tried to execute the following code in quad core processor.. Code: #include <pthread.h> #include<iostream> ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    Pthreads...

    I started using pthreads recently and tried to execute the following code in quad core processor..
    Code:
    #include <pthread.h>
    #include<iostream>
    #include <vector>
    
    using namespace std;
    
    vector<int> num;
    
    void *threadfunc(void *arg)
    {
      int *input=(int*) arg;
    
      for(int i=1;i<5;i++)
        {
          num.push_back((*input)*i);
        }
    
      return NULL;
    }
    
    int main()
    {
      pthread_t threadID[4];
      void *exit_status;
     
      for(int i=0;i<4;i++)
        {
          pthread_create(&threadID[i],NULL,threadfunc,&i);
        }
      for(int i=0;i<4;i++)
        {
          pthread_join(threadID[i],&exit_status);
        }
      for(int i=0;i<num.size();i++)
        {
          cout<<num[i]<<endl;
        }
    
      return 0;
    }
    Here is another version of same code
    Code:
    #include <pthread.h>
    #include<iostream>
    #include <vector>
    
    using namespace std;
    
    vector<int> num;
    vector<int> contain;
    
    void *threadfunc(void *arg)
    {
      num.clear();
      int *input=(int*) arg;
    
      for(int i=1;i<5;i++)
        {
          num.push_back((*input)*i);
        }
    
      //cout<<"K"<<endl;
      return &num;
    }
    
    int main()
    {
      pthread_t threadID[4];
      void *exit_status;
      vector<int> *hold;
      int count=0;
      vector<int>:: iterator it;
      void *threadfunc(void *arg);
    
      it=contain.begin();
    
      count=10;
     
      for(int i=0;i<4;i++)
        {
          pthread_create(&threadID[i],NULL,threadfunc,&i);
        }
      for(int i=0;i<4;i++)
        {
          pthread_join(threadID[i],&exit_status);
          hold=(vector<int> *)exit_status;
    
          contain.insert(it,(*hold).begin(),(*hold).end());
          (*hold).clear();
          exit_status=NULL;
    
        }
    
      for(int i=0;i<contain.size();i++)
        {
          cout<<contain[i]<<endl;
        }
    
      return 0;
    }
    It compiles fine and gives an output in a single processor but in quad core, I get

    Segmentation Fault (core dumped)

    Kindly help....

  2. #2
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    90
    wait...
    you have one global variable num, right?
    but it is accessed by several threads...
    don't you think you should be careful on that...
    i mean like putting putting some flags so that it won't be accessed by many threads at the same time...

    correct me if i am wrong...

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Look up mutexes. You need some sort of access control around num, at the very least. You also have concurrency problems with i, the variable you are passing to your threads. Go look up any threading tutorial, and read until they've taught you mutexes and deadlocks.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using pthreads with an API that uses pthreads
    By MK27 in forum C Programming
    Replies: 3
    Last Post: 03-06-2009, 01:47 PM
  2. Pthreads performance
    By C_ntua in forum C Programming
    Replies: 42
    Last Post: 06-17-2008, 11:29 AM
  3. Replies: 3
    Last Post: 04-16-2007, 12:02 PM
  4. Difference between win32 and linux pthreads
    By philipsan in forum C Programming
    Replies: 1
    Last Post: 02-07-2006, 03:57 PM
  5. pthreads and resources
    By ubermensch in forum C Programming
    Replies: 2
    Last Post: 02-07-2006, 01:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21