Solved

pthread uisng C in Unix/Linux Platform

Posted on 2000-03-15
2
321 Views
Last Modified: 2010-03-18
I have written this program using pthread, in which one thread prints "Hello" and the other thread prints "World!". But after I compiled it (gcc -Wall -o pth pth.c), there is nothing printed out. Please help me to solve this, thanks a lot!
Here is the program :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <pthread.h>
#include <thread.h>

void print_message_function( void *ptr );

int main(void)
{
        pthread_t thread1, thread2;
        pthread_attr_t attr;
        char *message1 = "Hello";
        char *message2 = "World";

        pthread_attr_init(&attr);
        pthread_create( &thread1, &attr,
                (void*)&print_message_function, (void*) message1);
        pthread_create(&thread2, &attr,
                (void*)&print_message_function, (void*) message2);
}

void print_message_function( void *ptr )
{
        char *message;
        message = (char *) ptr;
        printf("%s ", message);
        pthread_exit(0);
}
0
Comment
Question by:kwokhing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
fremsley earned 50 total points
ID: 2623032
hi

try adding:

{
  pthread_addr_t status;
  pthread_join(thread1, &status);
  pthread_join(thread2, &status);
}

at the end of main()
0
 
LVL 1

Expert Comment

by:shivers
ID: 2727690
The reason nothing is being printed out is that your program is exitting before the threads have a chance to do anything.

The pthread_create() calls return immediately - leaving you main() function to drop out.

Adding a delay here will give the threads time to do stuff (but not using sleep() because that pauses all threads - use usleep() or pthread_delay_np()

this is not really the best way of doing it - best to use semaphores really - there's loads of good tutorials on the net although I can't remember a URL offhand
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have seen several blogs and forum entries elsewhere state that because NTFS volumes do not support linux ownership or permissions, they cannot be used for anonymous ftp upload through the vsftpd program.   IT can be done and here's how to get i…
Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question