Reputation: 2027
I have implemented PThreads in a fairly elementary way as:
#include<iostream>
#include<pthread.h>
#include<stdio.h>
using namespace std;
class ThreadParameter
{
public:
char symbol_char;
int count;
};
void* print_char (void* param)
{
ThreadParameter* p = (ThreadParameter*)param;
for (int i=0; i< p->count; i++)
{
cout<< p->symbol_char <<endl;
i++;
}
return NULL;
}
int main ()
{
pthread_t thread1_id;
ThreadParameter param1;
param1.symbol_char = 'X';
param1.count = 27;
pthread_create (&thread1_id, NULL, &print_char, ¶m1);
int i = 0;
while (i<10)
{
cout<<"O"<<endl;
i++;
}
pthread_join(thread1_id,NULL);
return 0;
}
And its output is not showing the expected nos of X. Am I doing some mistake in calling join function or what? Thanks for help.
P.S: I have tried various values of X from 5 to 20, but it always gives me lesser than the desired numbers of X.
Upvotes: 0
Views: 306
Reputation: 22154
You are incrementing i
twice in print_char()
.
Change
for (int i=0; i< p->count; i++)
{
cout<< p->symbol_char <<endl;
i++;
}
into
for (int i=0; i< p->count; i++)
{
cout<< p->symbol_char <<endl;
}
Upvotes: 1