Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 09-30-2009, 02:05 AM   #1
LQ Newbie
Registered: Sep 2009
Posts: 4

Rep: Reputation: 0
Problem creating threads in C++

I have a program and I need to make 3 separate threads that all will run. The problem is when ever I create the threads it doesn't automatically run the startup code that I have entered (It will not run anything in the f_thread, m_thread, or d_thread). Here is the main of my program. I think that I may have to do something with pthread_join, but everything that I have tried didn't seem to work. I am very new to threads so anything would help.

int main ()
SandersBal.sharedChecking = 0;
SandersBal.sharedSavings = 0;

pthread_t father;
pthread_t mother;
pthread_t daughter;

pthread_create(&father, NULL, f_thread, &SandersBal);
pthread_create(&mother, NULL, m_thread, &SandersBal);
pthread_create(&daughter, NULL, d_thread, &SandersBal);

return 0;

Old 09-30-2009, 03:51 AM   #2
LQ Newbie
Registered: Mar 2005
Distribution: Slackware
Posts: 22

Rep: Reputation: 15
This question is probably in the wrong section (Programming?).

However, the problem is that your main-function ends before the threads you create are even up and running. And as your main process terminates, all of its threads will do the same.
Remember that your code isn't sequential anymore when you launch threads. They take a bit of time to launch, and they will be running in any order your OS wishes (unless you take counter-measures).

run 'man pthreads', read it, and look at the "SEE ALSO" section (like pthread_cond_wait).


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
Creating Kernel threads in uClinux!!! ananth86coolguy Programming 0 03-14-2009 08:40 AM
Creating threads -Block in accept ! dhara Linux - Networking 1 03-30-2007 12:51 AM
Creating Threads lucky6969b Linux - Software 2 12-02-2005 02:44 AM
Creating Linux Threads in C++ George_gk Programming 2 01-27-2005 04:29 AM
Creating a milisecond timeout timer for threads cardias Programming 5 08-05-2004 08:18 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:15 AM.

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