output of client thread

L

luckybalaji

Hello,

I have searched in group. I didnt get answer for my doubts. So i'm
posting this. It is very basic doubt on thread. I wrote below program
to start understanding the thread.

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>

extern int errno;
ofstream out;

void* do_loop(void* data)
{
int i;
int me = *((int*)data);
for (i=0; i<10; i++)
out << " i " << i << " me " << me << endl;
pthread_exit(NULL);
return data;
}
void perror(char *str)
{
cout << str << " : " << errno << endl;
exit (1);
}

int main(int argc, char* argv[])
{
int thr_id;
pthread_t p_thread;
int a = 1; /* new thread */
int b = 2; /* main thread */

thr_id = pthread_create(&p_thread, NULL, do_loop, (void*)&a);

if ( thr_id != 0 )
perror("Thread create failed");

out.open("log.txt", ios::app);
out << "I'm from main thread " << thr_id << endl;
out.close();

return 0;
}

Q1 : Always i get "Thread create failed". I dont find why?
Q2 : I wrote this program to see output writen by client thread in log
file? Is the logic correct?

I have tried the above sample program in HP-Unix B.11.11 U 9000/800 and
SunOS 5.6, Both are running in multiCPUs.... But It didnt work...

thx in advance for your help.

Regards,
Balaji.
 
V

Victor Bazarov

I have searched in group. I didnt get answer for my doubts. [...]

The only reason you didn't find any answers is simple: threads are
off-topic here. Try 'comp.programming.threads'.

V
 
J

Jonathan Mcdougall

Hello,

I have searched in group. I didnt get answer for my doubts. So i'm
posting this. It is very basic doubt on thread. I wrote below program
to start understanding the thread.

I don't know what book you are reading, but I think you should have a
look at www.accu.org for other suggestions.
#include <iostream.h>
#include <fstream.h>

These are not standard. They should be

# include <iostream>
# include <fstream>

Standard names are in namespace std. To quickly make your program work,
add

using namespace std;

but do read about namespaces (especially
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5).
#include <stdlib.h>
#include <errno.h>

These should be

# include <cstdlib>
# include said:
#include <pthread.h>

This does not exist in standard C++, the topic of this newsgroup. For
thread related questions, you'll be better served elsewhere. See
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9 for
newsgroup suggestions.
extern int errno;
ofstream out;

Advice: globals are best avoided, *especially* with threads.
void* do_loop(void* data)
{
int i;
int me = *((int*)data);

Use C++ style casts:

int me = *reinterpret_cast said:
for (i=0; i<10; i++)
out << " i " << i << " me " << me << endl;
pthread_exit(NULL);
return data;
}
void perror(char *str)
{
cout << str << " : " << errno << endl;
exit (1);
}

int main(int argc, char* argv[])
{
int thr_id;
pthread_t p_thread;
int a = 1; /* new thread */
int b = 2; /* main thread */

C++ is not C. See
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.7.
thr_id = pthread_create(&p_thread, NULL, do_loop, (void*)&a);

if ( thr_id != 0 )
perror("Thread create failed");

out.open("log.txt", ios::app);
out << "I'm from main thread " << thr_id << endl;
out.close();

return 0;
}


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top