多线程程序中的输出问题 [英] Problems with output in a multithreaded program

查看:70
本文介绍了多线程程序中的输出问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写在这里,因为我的问题可能很常见。


我有一个相当复杂的多线程程序,它使用POSIX

pthread标准。

主程序调用一系列从属例程。


我的问题是关于输出。如果我使用标准语法:


.....

cout<< 我是线号 << i<<结束;

.....


任何事情都是正确的。但是如果相反我尝试将输出发送到文件,

以通常的方式打开:


.....

out.open(" Calcolo_Parallelo.log",ios :: app);

.....


.....

out<< 我是线号 << i<< endl;

.....


有时输出不会被写入。我还没有理解哪些代码是写的b $ b代码和其他代码没有,但是大约50%的

请求的总输出被遗漏(以及它在哪里完了???)。


有人知道如何解决这个问题吗?


谢谢,


Flavio Cimolin

解决方案

Flavio写道:

我试着写这可能是因为我的问题很常见。

我有一个相当复杂的多线程程序,它使用POSIX
pthread标准。
主例程调用一系列从例程。

我的问题是关于输出。如果我使用标准语法:

....
cout<< 我是线号 << i<<结束;
....

任何事情都是正确的。但是,如果相反,我尝试将输出发送到文件,
以通常的方式打开:

....
out.open(" Calcolo_Parallelo.log", ios :: app);
....

....
out<< 我是线号 << i<< endl;
....

有时输出不是写的。我还不知道代码的哪些代码是写的,哪些不是代码,但是大约有50%的代码要求被遗漏(以及它在哪里完成了?)。 />
有人知道如何解决这个问题?




你应该在comp.programming.threads中询问。这个新闻组只用标准的C ++语言处理

,它对线程一无所知。

(比较这个FAQ:
http://www.parashift.com/c++-faq -lit ... t.html#faq-5.9 。)


干杯! --M


文章< 11 ********************* @ p79g2000cwp .googlegroups。 com>,

" Flavio" < FL ************ @ gmail.com>写道:

我试着在这里写,因为也许我的问题很常见。

我有一个相当复杂的多线程程序,它使用POSIX
pthread标准。
主例程调用一系列从属例程。

我的问题是关于输出。如果我使用标准语法:

....
cout<< 我是线号 << i<<结束;
....

任何事情都是正确的。但是,如果相反,我尝试将输出发送到文件,
以通常的方式打开:

....
out.open(" Calcolo_Parallelo.log", ios :: app);
....

....
out<< 我是线号 << i<< endl;
....

有时输出不是写的。我还不知道代码的哪些代码是写的,哪些不是代码,但是大约有50%的代码要求被遗漏(以及它在哪里完成了?)。 />
有人知道如何解决这个问题吗?




请记住,有三个独立的输出函数是
$ b你的代码中调用$ b,除非你已经锁定所有其他线程使用

''out''它们可能会干扰输出。


这不是这真的是一个C ++问题,因为C ++没有线程概念。

你应该尝试一个专门用于多线程的新闻组。


> ;请记住,在您的代码中调用了三个独立的输出函数

,除非您已锁定所有其他线程使用
out,否则它们可能会干扰该输出。


是的,我还使用互斥锁向''out'发送消息,以防止此类冲突发生。但似乎不是问题。

这不是一个C ++问题,因为C ++没有线程概念。
你应该尝试一个专门用于多线程的新闻组。 / blockquote>


好​​的,谢谢,我要问另一个新闻组。


再见,


Flavio


Hi, I try to write here because maybe my problem is a common one.

I have a rather complicated multithreaded program, which uses the POSIX
pthread standard.
A master routine calls a series of slave routines.

My problems is about output. If I use the standard syntax:

.....
cout << "Hi, I am thread number" << i << endl;
.....

anything goes right. But if instead I try to send the output to a file,
opened in the usual way:

.....
out.open("Calcolo_Parallelo.log", ios::app);
.....

.....
out << "Hi, I am thread number" << i << endl;
.....

sometimes the output is not written. I haven''t understood which lines
of code are written and which others not, but approximately a 50% of
the total output requested is missed (and where is it finished???).

Somebody has an idea of how to solve this problem?

Thank you,

Flavio Cimolin

解决方案

Flavio wrote:

Hi, I try to write here because maybe my problem is a common one.

I have a rather complicated multithreaded program, which uses the POSIX
pthread standard.
A master routine calls a series of slave routines.

My problems is about output. If I use the standard syntax:

....
cout << "Hi, I am thread number" << i << endl;
....

anything goes right. But if instead I try to send the output to a file,
opened in the usual way:

....
out.open("Calcolo_Parallelo.log", ios::app);
....

....
out << "Hi, I am thread number" << i << endl;
....

sometimes the output is not written. I haven''t understood which lines
of code are written and which others not, but approximately a 50% of
the total output requested is missed (and where is it finished???).

Somebody has an idea of how to solve this problem?



You should ask in comp.programming.threads. This newsgroup deals only
with the standard C++ language proper, and it knows nothing of threads.
(Compare this FAQ:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.9.)

Cheers! --M


In article <11*********************@p79g2000cwp.googlegroups. com>,
"Flavio" <fl************@gmail.com> wrote:

Hi, I try to write here because maybe my problem is a common one.

I have a rather complicated multithreaded program, which uses the POSIX
pthread standard.
A master routine calls a series of slave routines.

My problems is about output. If I use the standard syntax:

....
cout << "Hi, I am thread number" << i << endl;
....

anything goes right. But if instead I try to send the output to a file,
opened in the usual way:

....
out.open("Calcolo_Parallelo.log", ios::app);
....

....
out << "Hi, I am thread number" << i << endl;
....

sometimes the output is not written. I haven''t understood which lines
of code are written and which others not, but approximately a 50% of
the total output requested is missed (and where is it finished???).

Somebody has an idea of how to solve this problem?



Please keep in mind that three independent output functions are being
called in your code, unless you have locked all other threads from using
''out'' they could interfere with that output.

This isn''t really a C++ question since C++ has no concept of threads.
You should try one of the newsgroups dedicated to multithreading.


> Please keep in mind that three independent output functions are being

called in your code, unless you have locked all other threads from using
''out'' they could interfere with that output.
Yes, I also used a mutex to send a message to ''out'' in order to prevent
such conflicts. But it seems that is not the problem.
This isn''t really a C++ question since C++ has no concept of threads.
You should try one of the newsgroups dedicated to multithreading.



Ok, thank you, I''m going to ask to the other newsgroup.

Bye,

Flavio


这篇关于多线程程序中的输出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆