我无法理解这段代码......我期待类似于线程的东西,我会得到一个输出随机的"nooo"和"yaaaay"互相穿插,因为它们都是异步打印,而是我发现主线程似乎阻止第一次调用coroutine.resume(),从而防止下一个被启动,直到第一个产生.
如果这是预期的操作协同程序,它们有什么用处,我将如何实现我希望的目标?我是否必须为这些协同程序实现我自己的调度程序才能异步操作?因为这看起来很混乱,我也可以使用函数!
co1 = coroutine.create(function ()
local i = 1
while i < 200 do
print("nooo")
i = i + 1
end
coroutine.yield()
end)
co2 = coroutine.create(function ()
local i = 1
while i < 200 do
print("yaaaay")
i = i + 1
end
coroutine.yield()
end)
coroutine.resume(co1)
coroutine.resume(co2)
Run Code Online (Sandbox Code Playgroud) 我正试图在jruby中使用Java Opencl,但遇到了一个我无法解决的问题,即使有很多谷歌搜索.
require 'java'
require 'JOCL-0.1.7.jar'
platforms = org.jocl.cl_platform_id.new
puts platforms.class
org.jocl.CL.clGetPlatformIDs(1, platforms, nil)
Run Code Online (Sandbox Code Playgroud)
当我使用以下命令运行此代码时:jruby test.rb当取消注释最后一行时,我收到以下错误:
#<Class:0x10191777e>
TypeError: cannot convert instance of class org.jruby.java.proxies.ConcreteJavaP
roxy to class [Lorg.jocl.cl_platform_id;
LukeTest at test.rb:29
(root) at test.rb:4
Run Code Online (Sandbox Code Playgroud)
只是想知道是否有人知道如何解决这个问题?
编辑:好吧所以我想通过使平台成为一个数组我已经解决了这个问题的第一部分:
platforms = org.jocl.cl_platform_id[1].new
Run Code Online (Sandbox Code Playgroud)
但是在添加下几行时会导致此错误:
context_properties = org.jocl.cl_context_properties.new()
context_properties.addProperty(org.jocl.CL::CL_CONTEXT_PLATFORM, platforms[0])
Run Code Online (Sandbox Code Playgroud)
CodegenUtils.java:98:in `human': java.lang.NullPointerException
from CodegenUtils.java:152:in `prettyParams'
from CallableSelector.java:462:in `argumentError'
from CallableSelector.java:436:in `argTypesDoNotMatch'
from RubyToJavaInvoker.java:248:in `findCallableArityTwo'
from InstanceMethodInvoker.java:66:in `call'
from CachingCallSite.java:332:in `cacheAndCall'
from CachingCallSite.java:203:in `call'
from test.rb:36:in `module__0$RUBY$LukeTest'
from test.rb:-1:in `module__0$RUBY$LukeTest'
from test.rb:4:in `__file__' …Run Code Online (Sandbox Code Playgroud) 我已经有太多麻烦让这段代码正常工作!!!! 当我逐步调试它时它运行正常,但是当正常运行它只是崩溃.最初我使用QThread来更新ImagePreview像素图,但经过一整天的崩溃和痛苦,我改变了路线.现在它可以工作,在上面使用调试器的情况下,但是否则我很难过.请帮我!这段代码出了什么问题?我可以使用另一种方法吗?我正在尝试使用从网址下载的图片不断更新图像预览.
import sys
import io
import urllib2
from PySide import QtCore, QtGui, QtNetwork
import time
class QDownloadBuffer(QtCore.QBuffer):
downloadFinished = QtCore.Signal()
def __init__(self):
super(QDownloadBuffer, self).__init__()
self.open(QtCore.QBuffer.ReadWrite)
self.url = QtCore.QUrl("http://www.google.com.au/images/srpr/logo3w.png")
self.manager = QtNetwork.QNetworkAccessManager()
self.request = QtNetwork.QNetworkRequest(self.url)
self.manager.finished.connect(self.onFinished)
def startDownload(self):
print("Starting Download --")
self.reply = self.manager.get(self.request)
self.reply.error[QtNetwork.QNetworkReply.NetworkError].connect(self.onError)
def onFinished(self):
print("Download Finished -- ")
print(self.write(self.reply.readAll()))
self.reply.close()
self.downloadFinished.emit()
def onError(self):
print("oh no there is an error -- ")
print(self.reply.error())
class ImagePreview(QtGui.QWidget):
def __init__(self, parent=None):
super(ImagePreview, self).__init__(parent)
self.setMinimumSize(50, 50)
self.text = None
self.pixmap = None …Run Code Online (Sandbox Code Playgroud) 我创建了这个小程序,用概率和比率来计算pi.为了让它运行得更快我决定用pthreads多线程一次.不幸的是,即使经过大量的搜索,我也无法解决我的问题,当我运行threadFunc函数时,使用一个线程,无论是使用pthread,还是通常从calculate_pi_mt函数调用,性能都很高比我在双核机器上使用两个线程运行它时更好(至少两次或者不是三倍).我试过禁用优化无济于事.据我所知,当线程运行时,它使用局部变量,而不是在最后我使用互斥锁来创建命中总和...
首先是否有任何创建代码的技巧可以在这里运行得更好?(即风格),因为我只是通过尝试这些东西来学习.
其次,这些明显的性能问题是否有任何原因?当线程数设置为1运行时,我的一个cpus最大值为100%.设置为2时,第二个cpu上升到大约80%-90%,但显然这样做的所有额外工作都无济于事!可能是使用rand()函数?
struct arguments {
int n_threads;
int rays;
int hits_in;
pthread_mutex_t *mutex;
};
void *threadFunc(void *arg)
{
struct arguments* args=(struct arguments*)arg;
int n = 0;
int local_hits_in = 0;
double x;
double y;
double r;
while (n < args->rays)
{
n++;
x = ((double)rand())/((double)RAND_MAX);
y = ((double)rand())/((double)RAND_MAX);
r = (double)sqrt(pow(x, 2) + pow(y, 2));
if (r < 1.0){
local_hits_in++;
}
}
pthread_mutex_lock(args->mutex);
args->hits_in += local_hits_in;
pthread_mutex_unlock(args->mutex);
return NULL;
}
double calculate_pi_mt(int rays, int threads){
double answer;
int c; …Run Code Online (Sandbox Code Playgroud) 我一直在尝试实现一个可以从任何类型的切片中随机选择一个元素的函数(比如python的random.choice函数)
func RandomChoice(a []interface{}, r *rand.Rand) interface{} {
i := r.Int()%len(a)
return a[i]
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试将一个类型为[] float32的片段传入第一个参数时,会发生以下错误:
cannot use my_array (type []float32) as type []interface {} in function argument
这是界面{}的有趣滥用吗?有没有更好的方法来做到这一点?