Hi I've some problems with a concurrent download&sendind of a file. this is my code:
in main()
while(1){
while(i < wrapper->request_get->maxconnect && nconn < 5){
wrapper->request_get->temp = i;
i++;nconn++;
if((pth=pthread_create(&id[i],NULL,thread_func,wrapper))!=0){
...
...
}
so I need to download a generic file with max 5 connection at once. variables "i" and "nconn" are global and start from value 0.
in thread_func():
void *thread_func(void* args){
pthread_mutex_lock(&mutex);
j++;//it's global set to -1
struct RequestWrapper *wr = args;
...
...//I set the range of data to request.
...//for example,thread #1 needs bytes from 1 to 100;#2 from 101 to 200 etc...
...//I do that with the value of "j".
handlerRequest(wr)//this function asks(using a connect) to server a file's single part and save it
//into a buffer of struct wr.
if(array[j]!=1){//if is not it my turn to send, wait
pthread_cond_wait(&cond, &mutex);}
SendData(wr)//send data conteined into a buffer of wr.
array[j+1]=1;//I enable the next thread to send
pthread_mutex_unlock(&mutex);
pthread_cond_broadcast(&cond);//unlock other threads
nconn--;
pthread_exit(NULL);
}
unfortunately, in this way I've a sequential download and sending of data.
thread#1 download---send---thread#2 download---send---etc etc
how can I download the five parts of file at the same time and then send them in a orderly manner as soon as ready? I'm newbie about threads, so I think that some unknown, for me, functions can help me but which?ps: I cannot use signal...