Some confusion about POSIX threads

This is a discussion on Some confusion about POSIX threads within the Linux Programming forums, part of the Platform Specific Boards category; I decided to learn threading for a program of mine which seems (I may be wrong) to scream its necessity. ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    Some confusion about POSIX threads

    I decided to learn threading for a program of mine which seems (I may be wrong) to scream its necessity.
    I thought threads were supposed to be executed in parallel in machines having more than one core.
    So why am I getting the following output from the following program?
    [I left out joining for now...Does that play a role here ?]
    Code:
    #define _REENTRANT
    #include<pthread.h>
    #include<iostream>
    using namespace std;
    
    void* foo(void* arg);
    int main()
    {
        int return_value;
        pthread_t reference;
        return_value= pthread_create(&reference,NULL,foo,NULL);
        if(return_value!=0)
        {
            cout<<"Creation Failed";
            return 1;
        }
        int x(10);
        while(x--)
            cout<<8;
        
                    
        return 0;
        
    }
    void* foo(void* arg)
    {
        //cout<<"Thread Running.."<<endl;
        int x(10);
        
        while(x--)
            cout<<9;
        return (void*)(arg);
    }
    The output is
    99999999998888888888
    instead of my expected jumble of 9s and 8s. Why is it so ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You want to use pthread_detach() after pthread_create.

    Also: there are no guarantees about the order of events with separate threads. Do not even presume that you will get:

    98989898989

    You are just as likely to get 999999888888 again, or 888888999999. If you need to guarantee a sequence, you will need to use mutex locks. Without locking, threads are fine for doing things that do not have to happen in any particular order (because they won't).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Qn on Posix threads
    By sangamesh in forum C Programming
    Replies: 3
    Last Post: 06-11-2011, 11:04 AM
  2. Posix Threads
    By tsiknas in forum Linux Programming
    Replies: 2
    Last Post: 11-28-2009, 03:07 AM
  3. POSIX threads
    By Boylett in forum Linux Programming
    Replies: 2
    Last Post: 09-10-2008, 01:33 PM
  4. Using POSIX threads in C on Linux
    By muthus in forum C Programming
    Replies: 1
    Last Post: 01-19-2008, 04:34 PM
  5. POSIX-Threads
    By posixunil in forum Linux Programming
    Replies: 2
    Last Post: 04-17-2007, 06:43 AM

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