Thread: Thread problem?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    30

    Thread problem?

    So I'm having trouble with threads. What I'm trying to do is create an argc amount of threads that runs some sort of task. In this case, at least open a file..

    When I use printf before the p=fopen(arg,"r");, I get the argument name perfectly fine. However, it prints absolutely nothing when I put the printf AFTER that line. Why?


    Code:
    void *find_t(void *arg)
    {
            FILE *p;
            p = fopen(arg, "r");
    
            pthread_exit(NULL);
    }
    
    int main(int argc, char *argv[])
    {
            pthread_t thread[argc];
            pthread_attr_t attr;
    
    
            int t;
            for(t=1; t<=argc;t++){
                    pthread_create(&thread[t], &attr, find_t, argv[t]);
                    printf("%s", words[0]->word);
            }
            return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A couple of points
    1. argv[argc] is a NULL pointer.
    2. You're overrunning your thread array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    for(t=1; t<=argc;t++){
    Arrays: *always* indexed from 0 to sizeof(array) - 1. Learn it, live it, love it.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Also, you don't need the pthread_attr_t. Just pass NULL unless you actually want to set attributes.

    Use pthread_join after you create all the threads (in main). Otherwise, your program will terminate before the threads get a chance to run.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You must call pthread_join on threads that you create, unless the thread was created detached or explicitly detached.

    If you allow main() to return, your process and all of its threads will die. You can either have main() call pthread_join on the threads, or detach the threads and call pthread_exit at the end of main().

    gg

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    30
    Ok, that worked. though I'm a little confused as to how pthread_join works.

    I have another question. Does my program above run sequentially or concurrently?

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> how pthread_join works
    If you mean what is does, then "man pthread_join".

    >> Does my program above run sequentially or concurrently?
    Abstractly speaking, you can think of each find_t() as running concurrently. But if you only have 1 CPU, then they are not running "at the same time". Assuming you joined with the threads in a second for-loop, after creating all threads first.

    gg

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    30
    So, concurrent if I have multiple cpus and no if I don't for the case of a second for loop. What if I have the thread joining at the first loop after I created them?

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Academically speaking, if you have multiple threads running then you can think of them as running concurrently.

    If you only have a single CPU, then all threads take turns running on that single CPU. If you have more than 1 CPU, then the scheduler divides the work-load among them.

    Thread (computing) - Wikipedia, the free encyclopedia

    >> What if I have the thread joining at the first loop after I created them?
    You must start reading the manual. In the "man pthread_join" link above, it describes that pthread_join() will wait until the given thread terminates before it returns. Here is the pseudo-code:
    Code:
    for (...)
    {
       create thread
       wait for thread to terminate
    }
    The created threads never run concurrently with each other in this case. But if you do this:
    Code:
    for (...)
       create thread
    
    for (...)
       wait for thread to terminate
    Now the created threads have a chance to run concurrently.

    gg

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    30
    I actually tried that, well, I got weird results. I'm making a problem that keeps count of the word with the highest frequency. Say, the finding part is within

    void *find_t(void *arg)


    Assume it works. Cause it works fine when I create the thread and then use pthread_join after like so:

    Code:
         for(t=1; t<argc;t++)
            {
                    pthread_create(&thread[t], &attr, find_t, argv[t]);
                    pthread_join(thread[t],NULL);
    
                    temp[t-1]->freq = words[0]->freq;
            }
    But if I do it like concurrently, I bump into a problem, it works fine if I read in one file, but I want to read multiple files frequent word and end up getting an obscurely large number which I believe may be the frequency of all the files.

    Code:
        for(t=1; t<argc;t++)
            {
                    pthread_create(&thread[t], &attr, find_t, argv[t]);
                    temp[t-1]->freq = words[0]->freq;
            }
    
            int q;
            for(q=1; q<argc;q++)
            {
                    pthread_join(thread[q],NULL);
                   temp[q-1]->freq = words[0]->freq;
    
            }
    Any suggestion?


    EDIT:

    More precisely, if I were to input a.out filename1 filename2

    i would want two numbers,

    freq of filename1, freq of filename2

    what I'm actually getting is

    obscurely large number of one of the two files, another obscurely large number of one of the two files(or the same number as the former)
    Last edited by bungkai; 02-12-2012 at 01:09 AM.

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You should post all your code if you want all the issues to be pointed out to you. But you haven't fixed the issues we've pointed out so far. Go back and read post #2 and post #3 of this thread.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-20-2011, 12:01 AM
  2. Rogers Problem Thread
    By rogers in forum C Programming
    Replies: 1
    Last Post: 10-29-2011, 10:44 AM
  3. Yet another newbie problem thread
    By High Overlord in forum C++ Programming
    Replies: 2
    Last Post: 03-05-2011, 09:39 AM
  4. WINAPI thread problem
    By samguddy in forum Windows Programming
    Replies: 1
    Last Post: 11-13-2009, 03:02 PM
  5. Remote thread problem
    By RubbeR DuckY in forum C++ Programming
    Replies: 6
    Last Post: 08-08-2006, 12:24 PM