与cout和printf的区别,而c ++中的多线程 [英] Difference with cout and printf while multithreading in c++
问题描述
一些背景:
我有一个使用pthreads的多线程的c ++程序。该程序是一个酒店预订系统,10名客人(每个自己的线程),一个值机桌(1个线程)和退房桌(1线程)。
I have a c++ program that is multithreaded using pthreads. The program is a hotel reservation system, with 10 guests (each their own thread), a check-in desk (1 thread) and a check-out desk (1 thread). There are only 5 rooms in the hotel that a guest can be in. I am using semaphores to enforce mutual exclusion and event ordering in this program.
问题:
这里是我的代码(只是需要的部分...)
here is my code (Just parts that are needed...)
sem_init(&openRooms, 0, 5);
sem_wait(&openRooms); //waits for there to be an open room at the hotel
cout << "Guest " << guestNumber << " waits for check-in." << endl;
sem_wait(&checkInLine); //waits for an open spot at the check-in desk
有5位客人可以在一次的酒店,因为有5间房间。当我运行程序,我得到的输出(或类似...)
There are 5 guests that are able to be in the hotel at a time, because there are 5 rooms. When I run the program, I get the output (or something like...)
Guest Guest Guest Guest 24 waits for check-in. waits for check-in.1 waits for check-in.
3 waits for check-in.
似乎cout是让多个打印同时运行,这就是为什么Guest在一行中多次打印。
It seems that cout is letting multiple prints be run at the same time, which is why "Guest" is printed multiple times in a row.
我试着用printf这样做,同样的问题不会发生。整个语句在另一个线程打印语句之前打印。
I have tried doing this with printf, and the same issue doesn't happen. The entire statement is printed before another thread can print the statement.
sem_wait(&checkInSt); //Only one person at check in receptionist at a time
printf("Guest %ld goes to the check-in receptionist.\n", my_rank);
currentThreadIn = my_rank; //Rank for receptionist to use in room assignment
输出:
Guest 1 waits for check in.
Guest 3 waits for check in.
Guest 2 waits for check in.
Guest 4 waits for check in.
Guest 0 waits for check in.
为什么会发生这种情况?是否有某种bug的cout使用?此外,有没有办法避免使用cout?我可以使用额外的信号量来确保cout语句在另一个可以打印之前打印,但我正在寻找一种不使用额外信号量的方法。
Why does this happen? Is there some sort of bugger that cout uses? Additionally, is there any way to avoid this using cout? I could use an additional semaphore to ensure that the cout statement prints before another can print, but I'm looking for a way without using that additional semaphore
推荐答案
printf("Guest %ld goes to the check-in receptionist.\n", my_rank);
当您使用 printf
格式化为内部缓冲区,然后通过单个操作(Guest 2等待签入
)输出到控制台。
When you use printf
, your string is formatted into an internal buffer and then output to the console in a single operation ("Guest 2 waits for check-in."
).
cout << "Guest " << guestNumber << " waits for check-in." << endl;
当您使用 cout
输出到控制台多个部分 - Guest
,其次是 guestNumber
,后跟 等待签入。
,后跟 endl
。这是因为对<<
运算符的每次调用都像是一个单独的函数调用一样发生(它返回对同一 cout
When you use cout
, your string is output to the console in multiple parts - "Guest"
, followed by guestNumber
, followed by " waits for check-in."
, followed by endl
. This is because each call to the <<
operator takes place as if it were a separate function call (it returns a reference to the same cout
object for the next call to use).
因此,虽然写入控制台本身是线程安全的和原子的,在 cout
case它只对每个单独的子字符串使用原子。
So although writing to the console itself is thread-safe and atomic, in the cout
case it's only atomic for each separate sub-string.
解决方案如果你不想使用 printf
将是a)使用信号量或其他锁定机制,或b)在使用 stringstream
打印之前格式化文本,然后将其作为单个字符串输出。
The solution if you don't want to use printf
would be to a) use a semaphore or other locking mechanism, or b) format the text before printing it using stringstream
, and then output it as a single string.
这篇关于与cout和printf的区别,而c ++中的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!