Need help with pthread's

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Need help with pthread's

 
1
  #1
Mar 13th, 2006
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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Need help with pthread's

 
1
  #2
Mar 13th, 2006
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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Need help with pthread's

 
1
  #3
Mar 13th, 2006
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...
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Need help with pthread's

 
0
  #4
Mar 13th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Need help with pthread's

 
1
  #5
Mar 13th, 2006
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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC