且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多线程程序中的输出问题

更新时间:2022-11-08 17:22:27

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

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

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

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

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

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

....
out<< 我是线号 &LT;&LT; 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" &LT; FL ************ @ gmail.com&GT;写道:
我试着在这里写,因为也许我的问题很常见。

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

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

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

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

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

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

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




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

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


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

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


&gt ;请记住,在您的代码中调用了三个独立的输出函数
,除非您已锁定所有其他线程使用
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