943,871 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1827
  • C RSS
Mar 13th, 2006
1

Need help with pthread's

Expand Post »
Hi Guys,
So I'm trying to experiment with threads, though I'm running into some problems. Basically I'm trying to create multiple threads, where each thread is passed a different set of data. In my small example below, I'm starting off with a vector<string> of data. Each element of the vector is the data for each thread. The problem I'm having is that all threads seem to be receiving the LAST entries data. (As if all threads are sharing the same data...) For the time being, I want all threads to act independently..

Temp.cpp:
  1. 104 nmmgdsm40.ny.fw.gs.com|/home/hoffda/code> cat Temp.cpp
  2. extern "C"
  3. {
  4. #include "pthread.h"
  5. #include "Thread.h"
  6. }
  7. #include <iostream>
  8. #include <vector>
  9. #include "Temp.h"
  10. using namespace std;
  11. void manageThreads(vector<string> & temp);
  12. int startSingleThread( char * params )
  13. {
  14. cout<<"In thread, param:"<<params<<endl;
  15. return 0;
  16. }
  17. int main()
  18. {
  19. vector<string> a;
  20. a.push_back( "FIRST");
  21. a.push_back( "SECOND");
  22. a.push_back( "THIRD");
  23. manageThreads( a );
  24. }
  25. void manageThreads(vector<string> & temp)
  26. {
  27. unsigned int size = temp.size();
  28. int * status = new int[size];
  29. pthread_t * thread = new pthread_t[size];
  30. for ( unsigned int i=0;i < size;i++)
  31. {
  32. char myString[1000];
  33. strcpy( myString, temp[i].c_str() );
  34. pthread_create( &thread[i], NULL, myTest, &myString);
  35. }
  36. for ( unsigned int i=0;i < size;i++)
  37. pthread_join(thread[i], (void **) &status[i] );
  38. cout<<"Deleting threads and status..."<<endl;
  39. delete [] thread;
  40. delete [] status;
  41. }
Temp.h:
  1. int startSingleThread( char * param );
Thread.h:
  1. #include "stdio.h"
  2. #include "Temp.h"
  3. void * myTest(void * arg)
  4. {
  5. char * test = (char * ) arg;
  6. printf( "Starting thread with param:'%s'\n", test );
  7. return (void *) startSingleThread( test );
  8. }
CC Temp.cpp -lpthread -o Temp

./Temp
Starting thread with param:'THIRD'
Starting thread with param:'THIRD'
Starting thread with param:'THIRD'
In thread, param:THIRD
In thread, param:THIRD
In thread, param:THIRD
Deleting threads and status...
Similar Threads
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Mar 13th, 2006
1

Re: Need help with pthread's

I modified Thread.h to add the pthread_self() which shows which thread id it has...

  1. #include "stdio.h"
  2. #include "Temp.h"
  3. pthread_mutex_t ilock;
  4. void * myTest(void * arg)
  5. {
  6. char * test = (char * ) arg;
  7. printf( "Starting thread '%d' with param:'%s'\n", pthread_self(), test );
  8. return (void *) startSingleThread( test );
  9. }

I can tell that threads are actually being used because I get a different order of processing each time it is run... (Still have the problem/question about the char * being passed...)

./Temp
Starting thread '2' with param:'2, THIRD'
Starting thread '4' with param:'2, THIRD'
In thread, paramtarting thread '3' with param:'2, THIRD'
2, THIRD
In thread, param:2, THIRD
In thread, param:2, THIRD
Deleting threads and status...

./Temp
Starting thread '2' with param:'2, THIRD'
Starting thread '4' with param:'2, THIRD'
Starting thread '3' with param:'2, THIRD'
In thread, param:2, THIRD
In thread, param:2, THIRD
In thread, param:2, THIRD
Deleting threads and status...

./Temp
Starting thread '2' with param:'2, THIRD'
Starting thread '4' with param:'2, THIRD'
In thread, paramtarting thread '3' with param:'2, THIRD'
2, THIRD
In thread, param:2, THIRD
In thread, param:2, THIRD
Deleting threads and status...

./Temp
Starting thread '3' with param:'2, THIRD'
Starting thread '2' with param:'2, THIRD'
In thread, param:2, THIRD
In thread, param:2, THIRDStarting thread '4' with param:'2, THIRD'
In thread, param:2, THIRD
Deleting threads and status...
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Mar 13th, 2006
1

Re: Need help with pthread's

I figured out a way to do it, just not sure if it's the right way. Let me know what you guys think:
Temp.cpp
  1. extern "C"
  2. {
  3. #include "pthread.h"
  4. #include "Thread.h"
  5. }
  6. #include <iostream>
  7. #include <vector>
  8. #include "Temp.h"
  9. using namespace std;
  10. pthread_mutex_t ilock = PTHREAD_MUTEX_INITIALIZER;
  11. int threadCount=0;
  12. void manageThreads(vector<string> & temp);
  13. vector<string> a;
  14. int startSingleThread( void * nothing )
  15. {
  16. pthread_mutex_lock(&ilock);
  17. string tempString = a[threadCount];
  18. cout<<"In thread["<<threadCount<<"], param:"<<tempString<<endl;
  19. threadCount++;
  20. pthread_mutex_unlock(&ilock);
  21. return 0;
  22. }
  23. int main()
  24. {
  25. a.push_back( "FIRST");
  26. a.push_back( "SECOND");
  27. a.push_back( "THIRD");
  28. manageThreads( a );
  29. }
  30. void manageThreads(vector<string> & temp)
  31. {
  32. unsigned int size = temp.size();
  33. int * status = new int[size];
  34. pthread_t * thread = new pthread_t[size];
  35.  
  36. for ( unsigned int i=0;i < size;i++)
  37. {
  38. pthread_create( &thread[i], NULL, myTest, &temp);
  39. }
  40. for ( unsigned int i=0;i < size;i++)
  41. pthread_join(thread[i], (void **) &status[i] );
  42. cout<<"Deleting threads and status..."<<endl;
  43. delete [] thread;
  44. delete [] status;
  45. }

Temp.h
  1. int startSingleThread( void * nothing );

Thread.h
  1. #include "Temp.h"
  2. #include "stdio.h"
  3. void * myTest(void * arg)
  4. {
  5. printf( "Starting thread '%d'\n", pthread_self() );
  6. return (void *) startSingleThread( NULL );
  7. }

./Temp
Starting thread '2'
Starting thread '4'
Starting thread '3'
In thread[0], param:FIRST
In thread[1], paramECOND
In thread[2], param:THIRD
Deleting threads and status...

> ./Temp
Starting thread '4'
Starting thread '3'
Starting thread '2'
In thread[0], param:FIRST
In thread[1], paramECOND
In thread[2], param:THIRD
Deleting threads and status...
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Mar 13th, 2006
0

Re: Need help with pthread's

Can I make a suggestion before you spend six months learning this on your own?
It may save your spouse, children, and pets endless hours of anxiety watching you bash your head into a doorframe.

Richard Stevens book 'Advanced Programming in the UNIX Environment' spends about 90 pages on all aspects of threads, including 5 pages on threads and signals, which can drive you insane all by itself.

Consider reading it. There are dozens of gotchas in thread programming.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Mar 13th, 2006
1

Re: Need help with pthread's

I guess the main question I have is how to define a variable that is not shared among threads inside a function that is used by a thread...
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Mar 21st, 2011
0
Re: Need help with pthread's
hey winbatch!

m new to using threads and me too facing the same problem.. could you put some light onto the solution if you got any..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anurag.kyal is offline Offline
13 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: C Graphical User Interface
Next Thread in C Forum Timeline: File wont store floats





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC