Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-27-2005, 08:09 AM   #1
Registered: Dec 2003
Location: Petaling Jaya
Distribution: Ubuntu
Posts: 475

Rep: Reputation: 31
program with thread and time


This is my goal. I want to make a program that make three threads. Each thread has his own strings ( 3 strings ). Each thread will display string one by one to stdout. There is two seconds between display between strings for each thread.

So this is the code:
#include <pthread.h>
#include <iostream>
#include <vector>
#include <sys/time.h>
#include <cerrno>
#include <unistd.h>
#include <cstdlib>
using namespace std;

vector<string> myVector[3];
vector<string>::iterator myVectorIterator;
pthread_mutex_t condition_mutex[3];
pthread_cond_t condition_cond[3];

struct timeval now;
struct timespec timeout[3];

void* function( void* parameters )
    int* i = (int *)parameters;
    bool keep = true;

    cout << "This thread prints number " << *i << endl;

    int rc = 0;


	for( myVectorIterator = myVector[*i].begin();
		myVectorIterator != myVector[*i].end();
		myVectorIterator++ )
	pthread_mutex_lock( &condition_mutex[*i] );
	    timeout[*i].tv_sec = timeout[*i].tv_sec + 2;

	    rc = pthread_cond_timedwait( &condition_cond[*i], &condition_mutex[*i], &timeout[*i] );

		cout << "String thread : " << *myVectorIterator << endl;

	pthread_mutex_unlock( &condition_mutex[*i]);


    return NULL;

int main()
    for( int i=0; i<3; i++ ) {
	pthread_mutex_init( &condition_mutex[i], NULL );
	pthread_cond_init( &condition_cond[i], NULL );

    pthread_t thread_id[3];
    gettimeofday( &now, NULL );
    for( int i=0; i<3; i++ ) {
	timeout[i].tv_sec = now.tv_sec + 2;
	timeout[i].tv_nsec = now.tv_usec * 1000;

    //fill the vector
    myVector[0].push_back("1. Stupid damn you");
    myVector[0].push_back("1. I hate you!");
    myVector[0].push_back("1. Could you die, please!");
    myVector[1].push_back("2. I love you!");
    myVector[1].push_back("2. Beautiful life...");
    myVector[1].push_back("2. Sea coming from heaven...");
    myVector[2].push_back("3. Peace mind...");
    myVector[2].push_back("3. Glory of the heart!");
    myVector[2].push_back("3. Kindness means happiness...");

    int a = 0;
    int b = 1;
    int c = 2;

    pthread_create( &thread_id[a], NULL, &function, &a );

    // this is the problem

    //pthread_create( &thread_id[b], NULL, &function, &b );
    //pthread_create( &thread_id[c], NULL, &function, &c );

   //end of the problem

    for(int i=0; i<3; i++)
	pthread_join(thread_id[i], NULL);

    return 0;
Please see this section:
    // this is the problem

    //pthread_create( &thread_id[b], NULL, &function, &b );
    //pthread_create( &thread_id[c], NULL, &function, &c );

   //end of the problem
With that section commented, my program that created just one thread do just fine. The thread will wait for two seconds before print another string. But if I uncomment that section which will make my program create another two thread, my program get segmentation fault.

Somebody could help me???
Thank you.
Old 05-28-2005, 02:52 AM   #2
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
just for extra reference , there is a similar tutorial threading program here and maybe can solve your problem :.

the C program for thread-join is down here :

really hope that this can help you in anyway ......

Last edited by alred; 05-28-2005 at 02:53 AM.
Old 05-28-2005, 04:16 AM   #3
Registered: Dec 2003
Location: Petaling Jaya
Distribution: Ubuntu
Posts: 475

Original Poster
Rep: Reputation: 31

Alred, I make that program based on that tutorial anyway......


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
c thread program produces no output kpachopoulos Programming 7 10-14-2005 09:11 PM
What one should do if there's no answers to his thread/post for a long time ? kuser:) LQ Suggestions & Feedback 10 09-27-2005 05:03 PM
How to program thread in Unix OS?? lamvd Programming 2 01-27-2003 06:14 PM
How to program thread in Linux??? lamvd Linux - Newbie 2 01-27-2003 07:51 AM
Thread Program Not Working ssuman Programming 0 04-20-2002 07:32 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:36 PM.

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