| | |
Need help with pthread's
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
Temp.h:
Thread.h:
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...
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:
C Syntax (Toggle Plain Text)
104 nmmgdsm40.ny.fw.gs.com|/home/hoffda/code> cat Temp.cpp extern "C" { #include "pthread.h" #include "Thread.h" } #include <iostream> #include <vector> #include "Temp.h" using namespace std; void manageThreads(vector<string> & temp); int startSingleThread( char * params ) { cout<<"In thread, param:"<<params<<endl; return 0; } int main() { vector<string> a; a.push_back( "FIRST"); a.push_back( "SECOND"); a.push_back( "THIRD"); manageThreads( a ); } void manageThreads(vector<string> & temp) { unsigned int size = temp.size(); int * status = new int[size]; pthread_t * thread = new pthread_t[size]; for ( unsigned int i=0;i < size;i++) { char myString[1000]; strcpy( myString, temp[i].c_str() ); pthread_create( &thread[i], NULL, myTest, &myString); } for ( unsigned int i=0;i < size;i++) pthread_join(thread[i], (void **) &status[i] ); cout<<"Deleting threads and status..."<<endl; delete [] thread; delete [] status; }
C Syntax (Toggle Plain Text)
int startSingleThread( char * param );
C Syntax (Toggle Plain Text)
#include "stdio.h" #include "Temp.h" void * myTest(void * arg) { char * test = (char * ) arg; printf( "Starting thread with param:'%s'\n", test ); return (void *) startSingleThread( test ); }
./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...
I modified Thread.h to add the pthread_self() which shows which thread id it has...
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, param
tarting 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, param
tarting 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...
C Syntax (Toggle Plain Text)
#include "stdio.h" #include "Temp.h" pthread_mutex_t ilock; void * myTest(void * arg) { char * test = (char * ) arg; printf( "Starting thread '%d' with param:'%s'\n", pthread_self(), test ); return (void *) startSingleThread( test ); }
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, param
tarting 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, param
tarting 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...
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
Temp.h
Thread.h
./Temp
Starting thread '2'
Starting thread '4'
Starting thread '3'
In thread[0], param:FIRST
In thread[1], param
ECOND
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], param
ECOND
In thread[2], param:THIRD
Deleting threads and status...
Temp.cpp
C Syntax (Toggle Plain Text)
extern "C" { #include "pthread.h" #include "Thread.h" } #include <iostream> #include <vector> #include "Temp.h" using namespace std; pthread_mutex_t ilock = PTHREAD_MUTEX_INITIALIZER; int threadCount=0; void manageThreads(vector<string> & temp); vector<string> a; int startSingleThread( void * nothing ) { pthread_mutex_lock(&ilock); string tempString = a[threadCount]; cout<<"In thread["<<threadCount<<"], param:"<<tempString<<endl; threadCount++; pthread_mutex_unlock(&ilock); return 0; } int main() { a.push_back( "FIRST"); a.push_back( "SECOND"); a.push_back( "THIRD"); manageThreads( a ); } void manageThreads(vector<string> & temp) { unsigned int size = temp.size(); int * status = new int[size]; pthread_t * thread = new pthread_t[size]; for ( unsigned int i=0;i < size;i++) { pthread_create( &thread[i], NULL, myTest, &temp); } for ( unsigned int i=0;i < size;i++) pthread_join(thread[i], (void **) &status[i] ); cout<<"Deleting threads and status..."<<endl; delete [] thread; delete [] status; }
Temp.h
C Syntax (Toggle Plain Text)
int startSingleThread( void * nothing );
Thread.h
C Syntax (Toggle Plain Text)
#include "Temp.h" #include "stdio.h" void * myTest(void * arg) { printf( "Starting thread '%d'\n", pthread_self() ); return (void *) startSingleThread( NULL ); }
./Temp
Starting thread '2'
Starting thread '4'
Starting thread '3'
In thread[0], param:FIRST
In thread[1], param
ECONDIn 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], param
ECONDIn thread[2], param:THIRD
Deleting threads and status...
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
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.
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.
![]() |
Similar Threads
- pthread (C)
- Slow CPU causes damage to robot (C)
- PThreads: question on basic problem (or POSIX Threads) (C)
- Multithreading problem (C)
- strtok_r() error on solaris 8 (C)
- Launching an application from a thread (C++)
Other Threads in the C Forum
- Previous Thread: Hollow box program updated
- Next Thread: conversion
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation testing threads unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi





