473,503 Members | 5,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

output of client thread

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.

May 24 '06 #1
2 2121
lu*********@gmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 24 '06 #2
lu*********@gmail.com wrote:
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-lit...html#faq-27.5).
#include <stdlib.h>
#include <errno.h>
These should be

# include <cstdlib>
# include <cerrno>
#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-lit...t.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<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 */
C++ is not C. See
http://www.parashift.com/c++-faq-lit....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

May 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1873
by: Dustin | last post by:
I've been trying to create a "byte streaming server", it's basically a chat server, but I'd like to use it for transfering large amounts of data between two connected clients. Sounds simple...
15
4449
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
7
5300
by: Sidd | last post by:
Hi, I tried finding and example of multithreaded client-serve program in python. Can any one please tell me how to write a multithreaded client-server programn in python such that 1.It can handle...
11
2744
by: Timothy Shih | last post by:
Hi, I am having a freezing issue with my application. My application serves several remotable objects, all of which must be initialized before their use. Furthermore, some of them depend on each...
8
2561
by: FS Liu | last post by:
Hi, I am writing ATL Service application (XML Web service) in VS.NET C++. Are there any sample programs that accept XML as input and XML as output in the web service? Thank you very much.
4
6706
by: rs | last post by:
how I the client tell the server that the socket is closed? or this there an even that informs the server that the clients socket is close? Oh, I am using vb.net 2003 Thanks
8
2718
by: Ankit Aneja | last post by:
i am doing here some some socket-client work in C# windows service it is working fine for multiple clients now i want to limit these multiple clients to 25 for example i want that when service...
0
930
by: mail_tamanna | last post by:
I have created a client-server application that reads from a dos shell / console. The output is available on the stdout. I have a thread that reads from stdout (using ReadLine() ) and raises an...
6
4897
by: Alec MacLean | last post by:
Hi, I've created a small application for our company extranet (staff bulletins) that outputs a list of links to PDF's that are stored in a SQL table. The user clicks a link and the PDF is...
0
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.