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:

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;
}

Temp.h:

int startSingleThread( char * param );

Thread.h:

#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 );
}

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...

Recommended Answers

All 5 Replies

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

#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:Starting 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:Starting 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

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

int startSingleThread( void * nothing );

Thread.h

#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:SECOND
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:SECOND
In thread[2], param:THIRD
Deleting threads and status...

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.

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...

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..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.