Bramble
Bramble

Reputation: 1395

How to print using g++ with printf correctly?

I am compiling this code with g++:

#include <pthread.h>
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>

using namespace std;

#define num_threads 3
#define car_limit 4

pthread_mutex_t mutex; // mutex lock
pthread_t cid;         // thread id
pthread_attr_t attr;   // thread attrubutes

void *OneCar(void *dir);
void ArriveBridge(int *direction);
void CrossBridge();
void ExitBridge(int *direction);


int main()
{

    int dir[3] = {0,1,1};
    pthread_mutex_init(&mutex, NULL);
    pthread_attr_init(&attr);

    //cout<< "Pthread Create" << endl;
    printf("Pthread Create\n");

    for(int i = 0; i < num_threads; i++)
    {
        pthread_create(&cid, &attr, OneCar, (void *)&dir[i]);
    }


return 0;
}


void ArriveBridge(int *direction)
{
    //cout<<"Arrive"<<*direction << endl;
    int dr;

    if(*direction == 0)
        dr=0;
    else
        dr=1;

    printf("Arrive%d", dr);
}

void CrossBridge(int *dir)
{   
    char d;

    if(*dir == 0)
        d = 'N';
    else
        d = 'S';

    //cout<<"Crossing Bridge going:"<<d<<endl;
    printf("Crossing Bridge going %c", d);
}

void ExitBridge(int *direction)
{
    //cout<<"Exit" <<*direction<<endl;
    int dr;

    if(*direction == 0)
        dr=0;
    else
        dr=1;

    printf("Exit%d\n", dr);
}

void *OneCar(void *dir)
{
    int *cardir;
    cardir = (int *) dir;

    //cout<<*cardir;

    ArriveBridge(cardir);
    CrossBridge(cardir);
    ExitBridge(cardir);

    return 0;
}

and I am expecting this result printed to the screen:

> Pthread Create
> Arrive0Crossing Bridge going NExit0
> Arrive1Crossing Bridge going SExit1
> Arrive1Crossing Bridge going NExit1

But i get this instead:

Pthread Create
Arrive0Crossing Bridge going NExit0

Why doesnt it print the rest out?

Upvotes: 1

Views: 3411

Answers (3)

Stephan
Stephan

Reputation: 1037

You need to use "pthread_join" in main to wait for all threads to exit before your program terminates. You should also use an array to hold the id of each thread that you create:

pthread_t cid[num_threads];  // thread id`

You'll then want to call join on every thread you create:

for(int i = 0; i < num_threads; i++)
{
    pthread_create(&cid[i], &attr, OneCar, (void *)&dir[i]);
}


for(int i = 0; i < num_threads; ++i)
{
   pthread_join(cid[i], NULL);
};

Running the modified code now gives:

Pthread Create
Arrive0Crossing Bridge going NExit0
Arrive1Crossing Bridge going SExit1
Arrive1Crossing Bridge going SExit1

Upvotes: 3

DarkDust
DarkDust

Reputation: 92336

You missed the newlines ("\n"):

printf("Arrive%d\n", dr);
printf("Crossing Bridge going %c\n", d);

Because of that, the streams are probably not flushed. Additionally, if you don't wait for your threads (pthread_join) your program will exit before the threads could do their work.

Upvotes: 1

Jeremiah Willcock
Jeremiah Willcock

Reputation: 30969

Have you tried joining your threads at the end of main? It could be that the program is terminating before the other threads are completely finished.

Upvotes: 3

Related Questions