Solved

pthread uisng C in Unix/Linux Platform

Posted on 2000-03-15
2
317 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ubuntu server 12.04 creating broadcast on network 6 88
UPD maximums on Red Hat 6 114
Ubuntu VNC server rejects access by client 7 158
Redirect mail server from IP to IP 2 96
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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