LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem creating threads in C++ (https://www.linuxquestions.org/questions/linux-newbie-8/problem-creating-threads-in-c-758597/)

mending73 09-30-2009 01:05 AM

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;
};



Thanks

Camino 09-30-2009 02:51 AM

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).


All times are GMT -5. The time now is 10:13 AM.