473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Array of threads

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

Expand|Select|Wrap|Line Numbers
  1. #include <netdb.h>
  2. #include <netinet/in.h>
  3. #include <unistd.h>
  4. #include <iostream>
  5. #include <pthread.h>
  6. #include <sys/types.h>
  7. #include <ctype.h>
  8. #include <fstream.h>
  9.  
  10. using namespace std;
  11.  
  12. void *client_function(void *);
  13.  
  14. int main()
  15. {
  16.     int M;
  17.     pthread_t *client;
  18.  
  19.     cout << "Enter the number of hosts: " << endl;
  20.     cin >> M;
  21.  
  22.     //create M thread to simulate M clients
  23.     //dynamically allocate threads
  24.     client = new pthread_t[M];
  25.     for(int i = 0 ; i < M ; i++)
  26.     {
  27.         //create M threads
  28.         pthread_create( &(client[i]) , NULL, client_function , &i);
  29.         cout << "thread " << i+1 << " created." << endl;
  30.     }
  31.  
  32.     //join the threads
  33.     for(int i = 0 ; i < M ; i++)
  34.     {
  35.         pthread_join(client[i] , NULL);
  36.     }
  37.  
  38. }
  39.  
  40. /****************************************************************/
  41. void *client_function(void *input_value)
  42. {
  43.     int *i;
  44.     int temp;
  45.     char line[12];
  46.     ifstream infile;
  47.  
  48.     i = static_cast<int *>(input_value);
  49.     temp = *i + 1;
  50.     sprintf(line, "file%04d.in", temp);
  51.  
  52.                 infile.open(line);
  53.     if(!infile.is_open())
  54.     {
  55.         cout << "cannot open the file " << line << endl;
  56.         exit(0);
  57.     }
  58.     else
  59.         cout << "the file " << line << " succesfully opened." << endl;
  60. }
  61.  
Apr 18 '07 #1
2 17427
AdrianH
1,251 Expert 1GB
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
Apr 18 '07 #2
AdrianH
1,251 Expert 1GB
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
Apr 18 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
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...
9
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? ...
1
by: Ricardo | last post by:
how can I create and use a thread array???
1
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 =...
5
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...
5
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...
6
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
12
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...
1
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...
0
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...
0
isladogs
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...
0
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.