in the following program, I am trying to create M threads, each of which open a file called filexxxxin . for exmaple 4th thread will open file file0003.in.
When I run the program, it open the client0001 file for all threads. Any body knows what's wrong with my code.
appreciate your help. -
#include <netdb.h>
-
#include <netinet/in.h>
-
#include <unistd.h>
-
#include <iostream>
-
#include <pthread.h>
-
#include <sys/types.h>
-
#include <ctype.h>
-
#include <fstream.h>
-
-
using namespace std;
-
-
void *client_function(void *);
-
-
int main()
-
{
-
int M;
-
pthread_t *client;
-
-
cout << "Enter the number of hosts: " << endl;
-
cin >> M;
-
-
//create M thread to simulate M clients
-
//dynamically allocate threads
-
client = new pthread_t[M];
-
for(int i = 0 ; i < M ; i++)
-
{
-
//create M threads
-
pthread_create( &(client[i]) , NULL, client_function , &i);
-
cout << "thread " << i+1 << " created." << endl;
-
}
-
-
//join the threads
-
for(int i = 0 ; i < M ; i++)
-
{
-
pthread_join(client[i] , NULL);
-
}
-
-
}
-
-
/****************************************************************/
-
void *client_function(void *input_value)
-
{
-
int *i;
-
int temp;
-
char line[12];
-
ifstream infile;
-
-
i = static_cast<int *>(input_value);
-
temp = *i + 1;
-
sprintf(line, "file%04d.in", temp);
-
-
infile.open(line);
-
if(!infile.is_open())
-
{
-
cout << "cannot open the file " << line << endl;
-
exit(0);
-
}
-
else
-
cout << "the file " << line << " succesfully opened." << endl;
-
}
-
2 17427
in the following program, I am trying to create M threads, each of which open a file called filexxxxin . for exmaple 4th thread will open file file0003.in.
When I run the program, it open the client0001 file for all threads. Any body knows what's wrong with my code.
appreciate your help.
Might be due to the fact you are passing the pointer to a local variable to the other threads. Then you change that variable. Any number of random things could happen. Try passing a new int(i) instead. And don't forget to clean up the passed int in the thread it is passed to.
Adrian
Might be due to the fact you are passing the pointer to a local variable to the other threads. Then you change that variable. Any number of random things could happen. Try passing a new int(i) instead. And don't forget to clean up the passed int in the thread it is passed to.
Adrian
Alternatively, you can wait on a 'semaphore'[*] after spawning the thread. The thread would be responsible to release the 'semaphore'. Doing it this way would force the new thread to execute, store the i value and give control back to the parent thread.
Adrian
[*]sorry, pthreads uses a different name which is lighter weight for threads, semaphores are usually used for interprocess communication, I just can't remember the name at the moment
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Krackers |
last post by:
How do you write a function which returns a reference to an array. I
can only get a function to return a copy of the array itself. I've had
a look at some other threads in this group an the return...
|
by: Christopher Benson-Manica |
last post by:
Recently, the following definition was posted to comp.lang.c++:
> char **Arr;
1) Is the type of this object "array of 3 (array of two of pointer to
pointer to char)"? If not, what is it?
...
|
by: Ricardo |
last post by:
how can I create and use a thread array???
|
by: kelkel |
last post by:
I've created 5 threads to work in my program and I've also created a thread to monitor all 5 threads had still alive or not. Below is part of the monitor thread.
bool threadState;
threadState =...
|
by: Yuliaan |
last post by:
Hi everyone!
In program I wrote, I try to run 300 threads, when only 10 is running in the
same time. I use array to create 300 threads, but first I start only 10,
after that I add one thread when...
|
by: johannblake |
last post by:
I need to copy all the data from a one dimensional byte array to a
specific column in a two dimensional int array. For example:
byte byteArray = {4, 5, 6};
int intArray = new int;
After...
|
by: TommyB |
last post by:
Hi NG,
first of all sry about my english :) maybe it's somethin' funny to you
Following situation:
I've a structure like this:
Structure _Test
Dim IP as String
Dim UserLogin as String
|
by: amer.terzic |
last post by:
Here's what my code is supposed to do (it seems to work fine)
Take a char* array and remove a first character from it....as simple as
that.
The mentioned char* array is a 'global' var shared...
|
by: ToS1 |
last post by:
Hi,
I'm traying to make a thread array to a function with one variable, but i get everytime an error. I trayed to put the variable by the call, but the compiler doen't like that too.
static...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |