我想知道C++ 11 std::this_thread::yield()和C++之间有什么区别std::this_thread::sleep_for()?以及如何决定使用什么?谢谢.
我声明我读过线程,但我从未使用过.所以我问你:)
我有两个线程:A和B,其中A管理GUI和B管理逻辑.
我会先说A.
然后在A绘制GUI时,我会暂停它,等待B到达X点进入run方法.
当B达到X点进入运行方法时,我暂停B,然后恢复A.
A并B共享一些变量来管理GUI,逻辑......
我可以做吗?如果有,怎么样?:)
我想我已经读过,字段上的final关键字保证如果线程1实例化包含该字段的对象,那么如果线程2具有对该对象的引用,则线程2将始终看到该字段的初始化值(假设它是正确建造).它还在JLS中说过
[Thread 2]还将看到那些最终字段引用的任何对象或数组的版本,这些字段至少与最终字段一样是最新的.(JLS第17.5节)
这意味着如果我有A级
class A {
private final B b = new B();
private int aNotFinal = 2;
...
Run Code Online (Sandbox Code Playgroud)
和B级
class B {
private final int bFinal = 1;
private int bNotFinal = 2;
...
Run Code Online (Sandbox Code Playgroud)
然后aNotFinal不保证在线程2获得对类A的引用时被初始化,但字段bNotFinal是,因为B是由JLS中指定的最终字段引用的对象.
我有这个权利吗?
编辑:
如果我们有两个线程同时在类C的同一个实例上执行getA(),那么可能发生这种情况的情况就是这样
class C {
private A a;
public A getA(){
if (a == null){
// Thread 1 comes in here because a is null. Thread B doesn't come in
// here because by the time it gets here, object c …Run Code Online (Sandbox Code Playgroud) 我知道还有其他帖子说你可以创建一个控件然后检查InvokeRequired属性以查看当前线程是否是主线程.
问题是你无法知道该控件本身是否是在主线程上创建的.
我使用以下代码来判断一个线程是否是主线程(启动该进程的线程):
if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA ||
Thread.CurrentThread.ManagedThreadId != 1 ||
Thread.CurrentThread.IsBackground || Thread.CurrentThread.IsThreadPoolThread)
{
// not the main thread
}
Run Code Online (Sandbox Code Playgroud)
有谁知道更好的方法?看起来这种方式可能在运行时的未来版本中容易出错或中断.
我试图绕过线程,我知道我可以使用a Handler来发送消息/ runnables MessageQueue,然后由它接收Looper并发送回处理程序进行处理.
如果我发布到一个处理器在我的活动,是Activity,Handler,MessageQueue并且Looper在UI线程上所有正在运行的?如果没有,有人可以解释这一切是如何结合在一起的吗?:)
今天我面对constructServiceUrl()的是org.jasig.cas.client.util.CommonUtils班上的方法.我觉得他很奇怪:
final StringBuffer buffer = new StringBuffer();
synchronized (buffer)
{
if (!serverName.startsWith("https://") && !serverName.startsWith("http://"))
{
buffer.append(request.isSecure() ? "https://" : "http://");
}
buffer.append(serverName);
buffer.append(request.getRequestURI());
if (CommonUtils.isNotBlank(request.getQueryString()))
{
final int location = request.getQueryString().indexOf(
artifactParameterName + "=");
if (location == 0)
{
final String returnValue = encode ? response.encodeURL(buffer.toString()) : buffer.toString();
if (LOG.isDebugEnabled())
{
LOG.debug("serviceUrl generated: " + returnValue);
}
return returnValue;
}
buffer.append("?");
if (location == -1)
{
buffer.append(request.getQueryString());
}
else if (location > 0)
{
final int …Run Code Online (Sandbox Code Playgroud) 在Ruby中对类变量执行写入/读取不是线程安全的.对实例变量执行写入/读取似乎是线程安全的.也就是说,对类或元类对象的实例变量执行写/读是否是线程安全的?
在线程安全方面,这三个(人为的)示例之间有什么区别?
例1: 相互排斥
class BestUser # (singleton class)
@@instance_lock = Mutex.new
# Memoize instance
def self.instance
@@instance_lock.synchronize do
@@instance ||= best
end
end
end
Run Code Online (Sandbox Code Playgroud)
例2: 实例变量存储
class BestUser # (singleton class)
# Memoize instance
def self.instance
@instance ||= best
end
end
Run Code Online (Sandbox Code Playgroud)
例3: 在METACLASS上安装可变存储器
class BestUser # (singleton class)
# Memoize instance
class << self
def instance
@instance ||= best
end
end
end
Run Code Online (Sandbox Code Playgroud) ruby multithreading metaprogramming thread-safety ruby-1.9.2
当我们谈论原子变量时,例如C++ 11 atomic<>,它是否可以免费锁定?或者锁定是不同的东西?如果我使用原子变量管理队列,它会比无锁队列慢吗?
我正在尝试预取训练数据以隐藏I/O延迟.我想编写自定义Python代码,从磁盘加载数据并预处理数据(例如,通过添加上下文窗口).换句话说,一个线程进行数据预处理,另一个线程进行训练.这在TensorFlow中可行吗?
更新:我有一个基于@ mrry的例子的工作示例.
import numpy as np
import tensorflow as tf
import threading
BATCH_SIZE = 5
TRAINING_ITERS = 4100
feature_input = tf.placeholder(tf.float32, shape=[128])
label_input = tf.placeholder(tf.float32, shape=[128])
q = tf.FIFOQueue(200, [tf.float32, tf.float32], shapes=[[128], [128]])
enqueue_op = q.enqueue([label_input, feature_input])
label_batch, feature_batch = q.dequeue_many(BATCH_SIZE)
c = tf.reshape(feature_batch, [BATCH_SIZE, 128]) + tf.reshape(label_batch, [BATCH_SIZE, 128])
sess = tf.Session()
def load_and_enqueue(sess, enqueue_op, coord):
with open('dummy_data/features.bin') as feature_file, open('dummy_data/labels.bin') as label_file:
while not coord.should_stop():
feature_array = np.fromfile(feature_file, np.float32, 128)
if feature_array.shape[0] == 0:
print('reach end of …Run Code Online (Sandbox Code Playgroud) 如果我们在具有两个处理器的机器上有两个正在运行的线程,并且Thread.yield()在其中一个线程中调用,那么是否有理由不做任何事情(调度程序实际上会忽略请求),因为我们有足够的处理器来服务正在运行的线程?
java concurrency multithreading thread-synchronization java-threads
multithreading ×10
java ×4
c++ ×2
c++11 ×2
concurrency ×2
.net ×1
android ×1
atomic ×1
c# ×1
java-threads ×1
latency ×1
prefetch ×1
python ×1
ruby ×1
ruby-1.9.2 ×1
tensorflow ×1
x86-64 ×1