简体   繁体   English

wxThread文档示例有用或不好的编码实践?

[英]wxThread documentation example usefull or bad coding practice?

I have a C++ wxWidgets program, that uses threads. 我有一个使用线程的C ++ wxWidgets程序。 Therefore I needed to make sure the threads are safely accessed. 因此,我需要确保可以安全地访问线程。 In the wxThread documentation it is explained how to do so. wxThread文档中 ,说明了如何执行此操作。 They use friend classes and wxCriticalSection to secure their threads. 他们使用friend类和wxCriticalSection来保护其线程。 I did it like in the example and it works fine, though when I talked to a collegue about it, he told me friend classes are evil and should be avoided alltogether to avoid unexpected behaviour. 我按照示例中的方法进行操作,并且效果很好,但是当我与一位同事交谈时,他告诉我朋友班是邪恶的,应该完全避免,以免发生意料之外的行为。 I should use wxMutex instead. 我应该改用wxMutex

Now I understand his point, because having my main as a friend class gives the thread class complete access to it. 现在,我理解了他的观点,因为将我的main作为朋友类可以使线程类完全访问它。 I understand, that this can cause problems, for example if I have similarly named variables or uninentionally access something else that I should not use outside of main. 我知道,这可能会引起问题,例如,如果我有类似命名的变量,或者无意间访问了我不应该在main之外使用的其他内容。 But I wonder if there are any advantages of this method. 但是我想知道这种方法是否有任何优势。 I mean, there has to be something, otherwise I can't understand why this way should be (as the only way) described in the wxWidgets documentation? 我的意思是,必须有某些东西,否则我不明白为什么应该(唯一的方式)在wxWidgets文档中描述这种方式?

Could someone please enlighten me about the advantages and disadvantages of both methods? 有人可以启发我介绍这两种方法的优缺点吗? Or is there maybe a third way how I can access only the wxCriticalSection from the main without using friend or making it public? 还是有第三种方式,我可以不使用friend或将其公开而wxCriticalSection主系统访问wxCriticalSection Thank you for your help. 谢谢您的帮助。


Edit : As I realized that the critical part in my code is an artifact from long time ago, that is not neccessary anymore, the question is not vital for my programming. 编辑 :由于我意识到代码中的关键部分是很久以前的工件,因此不再需要了,这个问题对我的编程而言并不重要。 Nevertheless I think this is an interesting topic and would be usefull for future situations. 不过,我认为这是一个有趣的话题,并且对将来的情况很有用。

There are 2 completely orthogonal things in this question: 这个问题有2个完全正交的事物:

  1. Using friend is indeed a bad idea and the example in wxThread documentation could (and should) be rewritten to avoid it. 使用friend确实不是一个好主意,可以(并且应该)重写wxThread文档中的示例来避免这种情况。 The simplest way to do it would be to reset the thread pointer in wxEVT_COMMAND_MYTHREAD_COMPLETED event handler. 最简单的方法是在wxEVT_COMMAND_MYTHREAD_COMPLETED事件处理程序中重置线程指针。
  2. wxCriticalSection is semantically exactly the same as wxMutex and, in fact, under non-Windows platforms they are just exactly the same. wxCriticalSection在语义上与wxMutex完全相同,实际上,在非Windows平台下,它们也完全相同。 Under Windows, wxCriticalSection is just a more efficient kind of mutex (both classes directly correspond to their counterparts in Win32 API). 在Windows下, wxCriticalSection只是一种更有效的互斥体(两个类都直接对应于Win32 API中的对应类)。 It's perfectly fine and, in fact, preferred, to use wxCS instead of wxMutex if all you use it for is protecting some shared data. 很好,实际上,如果您使用它来保护某些共享数据,则首选使用wxCS而不是wxMutex You need to use wxMutex with a wxCondition however. 但是,您需要将wxMutexwxCondition一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM