Lawrence Ironjuly
Lawrence Ironjuly

Reputation: 90

creating C threads from text file

Let's say I have a text file composed like this

#####
typeofthread1

#####
typeofthread2

etc...

in my main I want to read that file, get the strings typeofthread1, typeofthread2 and create different threads using

pthread_t threads[NUM_THREADS];
for (i=0;i<NUM_THREADS;i++)
    pthread_create(&threads[i], NULL, -> HERE <- , void * arg);

how can I put the just read typeofthread1, typeofthread2 strings into -> HERE <- making the main create two threads that point to two different thread prototype?

I want to do this because I want to create a program that creates different types of threads, depending on what I want to do, and choosing that from text file (sort of a configuration file)

any suggestion?

Upvotes: 1

Views: 252

Answers (2)

Andr&#233; Caron
Andr&#233; Caron

Reputation: 45224

Map the string names to function pointers.

void * thread_type_1 ( void * );
void * thread_type_2 ( void * );

typedef  void * (*start_routine_t)(void *);

typedef struct mapping_t {

     const char * name;
     start_routine_t function;

} mapping_t;

const mapping_t mappings[] = {
    {"thread-type-1", &thread_type_1},
    {"thread-type-2", &thread_type_2},
};
const size_t mapping_count =
    sizeof(mappings)/sizeof(mappings[0]);

To select the proper thread function, loop over items in mappings and grab the function when the name matches.

start_routine_t get_start_routine ( const char * name )
{
    size_t i;
    for ( i=0; i < mapping_count; ++i )
    {
        if (strcmp(name,mappings[i].name) == 0) {
            return mappings[i].function;
        }
    }
    return NULL;
}

In wherever you launch the thread, you can use this as:

start_routine_t start_routine;

/* find start routine matching token from file. */
start_routine = get_start_routine(name);
if (start_routine == NULL) {
    /* invalid type name, handle error. */
}

/* launch thread of the appropriate type. */
pthread_create(&threads[i], NULL, start_routine, (void*)arg);

Upvotes: 3

Jason
Jason

Reputation: 32510

A better approach would be to create a default thread_dispatch function that you launch all your pthreads with. This dispatch function would take a structure that contained a void* to a structure that contained the thread-specific data, and a string that specified the type of the thread-function you wanted to run. You could then, using a look-up table mapping the string to the function-pointer type created in a code-module, find the appropriate function pointer, and pass the thread-specific data to that function. So this would look something like the following:

typedef struct dispatch_data
{
    char function_type[MAX_FUNCTION_LENGTH];
    void* thread_specific_data;
} dispatch_data;

void* thread_dispatch(void* arg)
{
    dispatch_data* data = (dispatch_data*)arg;

    //... do look-up of function_pointer based on data->function_type string

    return function_pointer(data->thread_specific_data);
}

Upvotes: 0

Related Questions