¿alguna idea?
Código:
#include <pthread.h>
#include <stdio.h>
#include <err.h>
#define NUM_THREADS 10
pthread_mutex_t mutex;
pthread_cond_t tickets_ready;
int tickets = 2;
void* pthread_function(void *myline)
{
char *theline;
pthread_mutex_lock(&mutex);
while(tickets==0)
{
pthread_cond_wait(&tickets_ready,&mutex);
}
-- tickets;
pthread_mutex_unlock(&mutex);
theline = (char *)myline;
printf("Thread working: ¡this is my line! -> %s\n", theline);
sleep(1);
pthread_mutex_lock(&mutex);
++tickets;
if (tickets>=1)
{
pthread_cond_broadcast(&tickets_ready);
/* code */
}
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main(int argc, char** argv)
{
pthread_t thread[NUM_THREADS];
long int i=0;
char *line;
//char *lines[NUM_THREADS];
FILE *file = fopen("commands.txt", "r");
char *data;
size_t len;
if(file == NULL)
{
err(1, "commands.txt");
}
while( i < NUM_THREADS && getline(&line,&len,file) != -1)
{
//fprintf(&lines[i], "%s\n", &line);
pthread_create(thread+i, NULL, pthread_function, (void *)line);
++i;
}
for ( i = 0; i < NUM_THREADS; ++i)
{
pthread_join(thread[i], NULL);
}
return 0;
}