Solved

pthread uisng C in Unix/Linux Platform

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netcat nc -l reads data in socket too fast - slow it down 18 579
linux, squid, exchange 14 142
OpenVPN Speed limitation to only 10 mbps 7 87
Configuring windows 7 host in Nagios 4 5 38
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now