在了解了shared变量目前没有受到内存障碍保护的困难之后,我现在遇到了另一个问题.要么我做错了,要么dmd中现有的编译器优化可以通过重新排序shared变量读取来破坏多线程代码.
例如,当我使用dmd -O(完全优化)编译可执行文件时,编译器很乐意优化o此代码中的局部变量(其中cas是比较和交换函数core.atomic)
shared uint cnt;
void atomicInc ( ) { uint o; do { o = cnt; } while ( !cas( &cnt, o, o + 1 ) );}
Run Code Online (Sandbox Code Playgroud)
这样的事情(参见下面的拆卸):
shared uint cnt;
void atomicInc ( ) { while ( !cas( &cnt, cnt, cnt + 1 ) ) { } }
Run Code Online (Sandbox Code Playgroud)
在"优化"代码cnt中从内存中读取两次,从而运行另一个线程cnt之间已经修改的风险.优化基本上破坏了比较和交换算法.
这是一个错误,还是有正确的方法来达到预期的效果?到目前为止,我发现的唯一解决方法是使用汇编程序实现代码.
完整的测试代码和其他详细信息
为了完整性,这里有一个完整的测试代码,可以显示这两个问题(没有内存障碍和优化问题).它在dmd 2.049和dmd 2.050的三台不同的Windows机器上产生以下输出(假设Dekker的算法没有死锁,可能会发生这种情况):
dmd -O -run optbug.d
CAS : …Run Code Online (Sandbox Code Playgroud) 我尝试通过IMAP将草稿电子邮件存储到MS Exchange上运行的文件夹中.一切都还可以,但Bcc收件人不会显示在服务器上存储的草稿邮件中.如果我使用MS Outlook发送密件抄送收件人也不会收到电子邮件.如果我在将消息存储到服务器后用Python读回消息,我可以在草稿中看到密件抄送.
以下Python代码重现此行为:
import imaplib
import time
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
message = MIMEMultipart()
message['Subject'] = 'Test Draft'
message['From'] = 'test@test.net'
message['to'] = 'test@test.com'
message['cc'] = 'testcc@test.com'
message['bcc'] = 'testbcc@test.com'
message.attach(MIMEText('This is a test.\n'))
server= imaplib.IMAP4('the.ser.ver.ip')
server.login('test', 'test')
server.append("Drafts"
,'\Draft'
,imaplib.Time2Internaldate(time.time())
,str(message))
server.logout()
Run Code Online (Sandbox Code Playgroud)
如果我运行此代码,草稿将存储到DraftExchange Server上的文件夹中.但是,如果我使用MS Outlook查看草稿,它不包括bccrecipient(message['bcc'] = 'testbcc@test.com').Message,to,from,ccOK,没有错误.
如果我从Exchange文件夹下载已包含密件抄送的草稿,我也可以看到密件抄送.只有上传对我不起作用.
任何帮助非常感谢.谢谢.顺便说一下,MAPI不是一个选择.
更新:谢谢.X-Receiver不适合我.至于在Outlook中使用IMAP-Folder,我得到了一个有趣的结果.如果我通过Outlook中的IMAP文件夹访问草稿,我会看到密件抄送.但是,如果我通过MAPI文件夹访问它,我看不到它.会发挥一点点.
结论:感谢您的投入.实际上,代码工作得很好.请参阅下面的我找到的答案.
为什么str(A())看似打电话A.__repr__()而不是dict.__str__()在下面的例子中?
class A(dict):
def __repr__(self):
return 'repr(A)'
def __str__(self):
return dict.__str__(self)
class B(dict):
def __str__(self):
return dict.__str__(self)
print 'call: repr(A) expect: repr(A) get:', repr(A()) # works
print 'call: str(A) expect: {} get:', str(A()) # does not work
print 'call: str(B) expect: {} get:', str(B()) # works
Run Code Online (Sandbox Code Playgroud)
输出:
call: repr(A) expect: repr(A) get: repr(A)
call: str(A) expect: {} get: repr(A)
call: str(B) expect: {} get: {}
Run Code Online (Sandbox Code Playgroud) 我刚遇到一个问题,其中类的构造函数需要分配内存.所以我高兴地写道char *mem = static_cast<char*>(malloc(100*sizeof(*mem)));.但后来我突然意识到,如果出现错误,我无法返回错误代码(我的代码中没有使用异常).我怎么解决这个问题?
我应该添加一个bool initialized成员然后在上课后然后立即检查,如:
myClass mc;
if (!mc.initialized) {
printf("Memory allocation failed in mc's constructor\n");
exit(1);
}
Run Code Online (Sandbox Code Playgroud)
谢谢,Boda Cydo.
>>> import string
>>> s = 'happy cat'
>>> string.find(s, 'cat')
6
Run Code Online (Sandbox Code Playgroud)
和
>>> s = 'happy cat'
>>> s.find('cat')
6
Run Code Online (Sandbox Code Playgroud)
在上面两段代码中,我有以下疑问.
谢谢,Vinay
我正在尝试制作我的第一个python应用程序.我想制作简单的电子邮件发件人表格 在qt designer中创建一个dialog.ui,然后从dialog.ui生成dialog.py并写入该函数
def SendEmail(self,efrom,eto,esubj,ebody):
msg = MIMEText( ebody.encode('UTF-8'),'html', 'UTF-8')
msg['Subject'] = esubj
msg['From'] = efrom
msg['To'] = eto
s = smtplib.SMTP()
s.connect("mail.driversoft.net", 25)
s.login("info@mysite.net", "1234567")
s.sendmail(efrom, [eto], msg.as_string())
s.quit()
Run Code Online (Sandbox Code Playgroud)
并尝试连接到插槽
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.SendEmail("info@mysite.net", "test@mysite.net", "subject", "bodytext"))
Run Code Online (Sandbox Code Playgroud)
当我尝试启动这个应用程序时,我没有在我的电子邮件恢复消息和控制台中看到表单
Traceback (most recent call last):
File "C:\Documents and Settings\a.ivanov\My Documents\Aptana Studio Workspace\test1\src\wxtest.py", line 61, in <module>
ui.setupUi(Dialog)
File "C:\Documents and Settings\a.ivanov\My Documents\Aptana Studio Workspace\test1\src\wxtest.py", line 33, in setupUi
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.SendEmail("info@mysqite.net", "test@mysite.net", "subject", "bodytext"))
TypeError: arguments did not match any overloaded call: …Run Code Online (Sandbox Code Playgroud) python ×4
d ×2
allocation ×1
bcc ×1
c++ ×1
class ×1
concurrency ×1
constructor ×1
dictionary ×1
email ×1
imap ×1
inheritance ×1
memory ×1
monitor ×1
return ×1