Thread Programming Help 100% Lost!

This is a discussion on Thread Programming Help 100% Lost! within the C Programming forums, part of the General Programming Boards category; Hey all. I have been working on this problem for well over 3 hours and I think its time to ...

  1. #1
    Registered User
    Join Date
    Sep 2009

    Thread Programming Help 100% Lost!

    Hey all. I have been working on this problem for well over 3 hours and I think its time to ask for help. What the program should do is create a thread within a thread. Now the only way I know how to do this is with a struct. Now whenever I run my code I keep getting seg. faults. Here is the complete code...

    #include  <stdio.h>
    #include  <stdlib.h>
    #include  <pthread.h>
    #include  <ctype.h>
    int	  total_words ;
    pthread_mutex_t counter_lock = PTHREAD_MUTEX_INITIALIZER;
    struct command_t{
    	pthread_t pth;
    	int argc;
    	char *argv;
    	//char *argv2;
    main(int ac, char *av[])
    	printf("Main loop!");
    	void	  *count_words(void *);
    	int i = 0;
    	pthread_t t1, t2;
    	struct command_t command;
    	command.argc = i;
    	//command.argv[1] = av[1];
    	//command.argv[2] = av[2];
    	command.pth = t2;
    	printf("This is av[0] %s, av[1] %s, av[2] %s, av[3] %s",av[0],av[1],av[2],av[3]);
    	//if ( ac != 3 ){
    	//	printf("usage: %s file1 file2\n", av[0]);
    	//	exit(1);
    	total_words = 0;
    	printf("Right before pthread_create");
    	//pthread_create(&t1, NULL, count_words, (void *) &command);
    	pthread_join(t1, NULL);
    	pthread_join(t2, NULL);
    	printf("%5d: total words\n", total_words);
    //Method for counting words
    void *count_words(void *f)
    	printf("First line in method!");
    	struct command_t *my_data;
    	my_data = (struct command_t *) f;
    	char *filename = my_data->argv[my_data->argc];
    	/*if(my_data->argc > 0)
    	   filename = my_data->argv2;
    	   filename = my_data->argv;
    	FILE *fp;
    	int  c, prevc = '\0';
    	printf("i = %i", my_data->argc);
    //Creates a thread within the thread
    	if(my_data->argc = 0)
    		my_data->argc = my_data->argc + 1;
    		pthread_create(&my_data->pth, NULL, count_words, (void *) &my_data);
    		printf("Ohs Nos! We created another thread!!");
    	if ( (fp = fopen(filename, "r")) != NULL ){
    		while( ( c = getc(fp)) != EOF ){
    //The For loop for counting the number of words!
    			if ( !isalnum(c) && isalnum(prevc) )
    			prevc = c;
    	} else 
    	return NULL;

    I have a few questions as well...
    1. Is this legal:
    struct command_t{
            pthread_t pth;
            int argc;
            char *argv[];
            command.argc = i;
            command.argv[1] = av[1];
            command.argv[2] = av[2];
            command.pth = t2;
    What I want to do is make it so the array value argv gets set with the different files passed in. So then within the method I can use the counter to access the correct one.

      char *filename = my_data->argv[my_data->argc];
    I am not sure if this is correct. But I believe it might be.

      pthread_create(&my_data->pth, NULL, count_words, (void *) &my_data);
    Again is saying &my_data->pth correct? Also within the struct can I make the pth an array so then I can pass multiple thread names in?

    Please can anyone help me with this because I am about to start ripping my hair out!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    >> char *argv;

    I'm pretty sure you want that to be a char**. In this case, just point it directly to the pointer received from 'main' (eg: 'av'), but sometimes it's more appropriate to make a copy (just FYI).

    >> command.argv[1] = av[1];

    You definitely don't want to do that, since 'argv' doesn't have any memory allocated to it. Again, in this case just point it directly to 'av'.

    >> command.pth = t2;

    At this point, 't2' has no value, and it won't until after the call to pthread_create returns.

    >> pthread_create(&my_data->pth, NULL, count_words, (void *) &my_data);

    Really bad design, there. You're taking the data that belongs to one thread and giving it to another, which creates a host of problems (eg: raced conditions, etc), in a quasi-recursive fashion, no less.

    A word of advice: threads can be extremely difficult to get right. Even seasoned, experienced programmers have problems with them, if that gives you any idea. Judging from your current understanding of the language in general, you're really just asking for trouble. But maybe this is an assignment, or what not. In that case, first get everything else working properly and well tested, making sure to separate the logic completely from the threading aspect. Once that's done, carefully add the threading bit, again, making sure to test thoroughly along the way. Anyway, I'm not trying to discourage you here, but just stressing the need to be practical about it. Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 05:14 AM
  2. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 02:17 PM
  3. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:35 AM
  4. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 11:36 PM
  5. Lost thread
    By Barjor in forum A Brief History of
    Replies: 8
    Last Post: 07-01-2002, 03:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21