LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-26-2009, 05:27 AM   #1
raghuhrcta
LQ Newbie
 
Registered: May 2009
Posts: 14

Rep: Reputation: 0
Problem with the pthread_create


Hi All,

I am not able to understand the problem in this below given code snippet.

#include <iostream>
#include <pthread.h>
using namespace std;


extern "C" void *print_message(void *);

void *print_message(void *)
{
printf("Going to sleep\n");
sleep(10);
}


int main()
{
int value;
pthread_t thread1;
printf("creating the thread now\n");
value = pthread_create(&thread1, NULL, print_message, NULL);
printf("came back from thread\n");
return 0;
}

In this code "creating the thread now" and "came back from thread" are getting printed properly. But the "Going to sleep" is not printing. Also the sleep(10) is not executing. I feel its not at all entering the void *print_message(void *) method. Can anyone help me out what may be problem in this code.

Waiting for responses

Raghavendra. H. R
 
Old 10-26-2009, 08:06 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Have a look at pthread_join

From http://www.amparo.net/ce155/thread-ex.html

Code:
    /* Main block now waits for both threads to terminate, before it exits
       If main block exits, both threads exit, even if the threads have not
       finished their work */ 
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
That's what happens in your case

PS There is a dedicated programming section at LQ
 
Old 10-28-2009, 01:56 AM   #3
raghuhrcta
LQ Newbie
 
Registered: May 2009
Posts: 14

Original Poster
Rep: Reputation: 0
thanks for reply but i had another approach

Thanks for your reply sir, i tried by giving some sleep in the main only.
i.e.,

#include <iostream>
#include <pthread.h>
using namespace std;


extern "C" void *print_message(void *);

void *print_message(void *)
{
printf("Going to sleep\n");
sleep(10);
}
int main()
{
int value;
pthread_t thread1;
printf("creating the thread now\n");
value = pthread_create(&thread1, NULL, print_message, NULL);
sleep(20);
printf("came back from thread\n");
return 0;
}

and now it is working properly, it will go into the print_message method, waits there for 10secs meanwhile 10 secs will be elapsed in the main() also. So after coming out from print_message() method it wait for another 10secs so totally it will complete 20 secs and the program will terminate.


Regards,

Raghavendra. H. R
 
Old 10-28-2009, 03:46 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Problem is that this works in your current example but can not be applied in an universal way. What if you don't now how long the new thread will take to complete?


PS If your problem is solved, can you please mark your thread as solved using the thread tools just above the first post. That will help others that are looking for solutions.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in installing CLIsh: Configure script unable to find pthread_create function avinash_ag1985 Linux - Kernel 3 07-22-2008 03:47 PM
problem compiling with "pthread_create" cleopard Linux - General 3 05-15-2008 11:20 AM
pthread_create... os2 Programming 4 05-26-2005 04:09 PM
pthread_create problem live_dont_exist Programming 5 04-24-2005 07:21 AM
pthread_create tushar123 Programming 2 06-07-2004 09:15 AM


All times are GMT -5. The time now is 12:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration