Solved

pthread uisng C in Unix/Linux Platform

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

696 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