Solved

pthread uisng C in Unix/Linux Platform

Posted on 2000-03-15
2
312 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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

10 Experts available now in Live!

Get 1:1 Help Now