标签: thread-synchronization

线程同步

必须在线程之间共享数据.哪种方法最好同步?

锁是更好的方法还是Mutex?

namespace ConsoleApplication8
{
    class Data
    {
        public int X
        {
            set;
            get;
        }

        public int Y
        {
            get;
            set;
        }

        public int result;
    }

    class Program
    {
        static int a;
        private static object aLock = new object();

        static void Main(string[] args)
        {
            ParameterizedThreadStart aStart = new ParameterizedThreadStart(Addition);
            Thread aThread = new Thread(aStart);
            Data aData = new Data();
            aData.X = 10;
            aData.Y = 20;

            Thread aThread2 = new Thread(aStart);
            aThread2.Start();


            aThread.Start(aData);
            aThread.Join();
            aThread2.Join();
            Console.WriteLine("End of the program");
        }

        static …
Run Code Online (Sandbox Code Playgroud)

c# thread-synchronization

3
推荐指数
1
解决办法
439
查看次数

通过不同的线程同时读取和更改变量

我感兴趣的是一个线程正在等待while循环中变量的变化:

while (myFlag == false) {
    // do smth
}
Run Code Online (Sandbox Code Playgroud)

它重复无数次.

与此同时,另一个线程更改了此变量的值:

myFlag = true;
Run Code Online (Sandbox Code Playgroud)

如果此变量不是volatile,读者线程是否可以看到更改另一个线程中变量值的结果?一般来说,据我所知,它永远不会发生.还是我错了?然后在什么情况下,第一个线程可以看到变量的变化并退出循环?这可能不使用volatile关键字吗?在这种情况下,处理器缓存的大小是否起作用?

请解释并帮助我理解!先感谢您!!

java concurrency multithreading thread-safety thread-synchronization

3
推荐指数
1
解决办法
1192
查看次数

如何在Perl中控制多线程同步

我有[az,AZ] ASCII数字的数组,如下所示: my @alphabet = (65..90,97..122);

所以主线程功能是从字母表中检查每个字符,如果条件为真则返回字符串.

简单的例子:

    my @output = ();
    for my $ascii(@alphabet){
      thread->new(\sub{ return chr($ascii); });
    }
Run Code Online (Sandbox Code Playgroud)

我想在每个ASCII号上运行线程,然后以正确的顺序将线程函数中的字母放入数组中.

因此,在out case中,数组@output应该是动态的,并且[a..z,A-Z]在所有线程完成其工作后包含.

如何检查,是否已完成所有线程并保持订单?

perl multithreading thread-synchronization

3
推荐指数
1
解决办法
204
查看次数

如何启动两个CPU内核以同时运行指令?

例如,在X86中,两个CPU内核运行不同的软件线程。
同时,这两个线程需要同时在其CPU内核上运行。
是否有办法同步这2个CPU内核/线程,或类似的方法以使它们(几乎)同时(在指令级别)开始运行?

linux x86-64 linux-kernel microbenchmark thread-synchronization

3
推荐指数
1
解决办法
96
查看次数

Kotlin`..let`是线程安全的吗?

Kotlin ?.let线程安全吗?

假设a变量可以在不同的线程中更改。使用a?.let { /* */ }线程安全吗?如果等于,那么是否if (a != null) { block() }可能其中if不为null且block其中已经为null?

multithreading let thread-synchronization kotlin

3
推荐指数
1
解决办法
125
查看次数

方法同步与对象同步有什么不同?

可能重复:
synchronized block vs synchronized方法?

如果有人能够帮助我找到方法同步和对象同步之间的不同之处的真实例子,那就太好了.

方法同步示例

public class MyClassExample {
private int i;
public synchronized void increment(){
    i = i + 1;
}
}
Run Code Online (Sandbox Code Playgroud)

对象同步示例

public class MyClassExample {
private int i;
Object writeLock = new Object();
 public void increment(){
    synchronized(writeLock) {
        i = i + 1;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

java multithreading thread-safety thread-synchronization

2
推荐指数
1
解决办法
4922
查看次数

锁定不起作用

这是我的代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
    class Writer {
        public void Write(string xxx) { Console.Write(xxx); }

    }
    class Program
    {
        static Writer wrt;

        static void Main(string[] args)
        {
            wrt = new Writer();
            Thread trd = new Thread(new ThreadStart(delegate() {
                lock (wrt)
                {
                    Thread.Sleep(1000);
                    wrt.Write("1");
                }
            }));
            trd.Start();
            wrt.Write("0");
            Console.ReadLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

例外输出为"10",但输出为"01".为什么?

c# multithreading monitor thread-synchronization

2
推荐指数
1
解决办法
78
查看次数

暂停恢复线程:Java

当我运行此代码时,它显示以下输出:

One : 15 Two : 15 One : 14 Two : 14 Two : 13 One : 13 Two : 12 One : 12 One : 11 Two : 11 Thread 1 suspended Two : 10 Two : 9 Two : 8 Two : 7 Two : 6 Thread 1 resumed Thread 2 suspended Thread 2 resumed

输出不会持续到结束为One:1 Two:1是否未执行NewThread1类的myresume方法?这背后的原因是什么?

以下是NewThread1的代码:

class NewThread1 implements Runnable{
String name;
Thread t;
boolean suspendFlag;

NewThread1(String threadname){
    name = threadname;
    t = …
Run Code Online (Sandbox Code Playgroud)

java resume multithreading suspend thread-synchronization

2
推荐指数
1
解决办法
453
查看次数

同步方法,我可以用它来不是主线程吗?

我很少使用线程,我对这个类有疑问:

unit ExpectingThread;

interface

uses
    System.Classes;

type
    TExpectingThread = class(TThread)
    private
        _timeoutMs: Integer;
        _buff: string;
        _patterns: TArray<string>;
        _result: Integer;
        function Timeouted(startTime: Cardinal): Boolean;
        function ExpectedDetected: Boolean;

    protected
        procedure Execute; override;
    public
        constructor Create(patterns: TArray<string>; buff: string; timeoutMs: Integer);
        //this method is called from other NOT MAIN thread
        procedure BuffUpdate(text: string);
    end;

implementation

uses
    Winapi.Windows,
    System.RegularExpressions;

{ TExpectingThread }

constructor TExpectingThread.Create(patterns: TArray<string>; buff: string; timeoutMs: Integer);
begin
    _patterns := patterns;
    _timeoutMs := timeoutMs;
    _buff := buff;
end;

//this method is called …
Run Code Online (Sandbox Code Playgroud)

delphi thread-synchronization

2
推荐指数
1
解决办法
692
查看次数

线程安全是否意味着没有竞争条件?

ConcurrentHashMap是线程安全的,但是会发生竞争情况,因为据我了解,仅映射的一部分被锁定并且仅用于写操作,这意味着如果同时存在读取操作,则将存在竞争条件。

但是我也喜欢在这里阅读https://en.wikipedia.org/wiki/Thread_safety

Thread safe: Implementation is guaranteed to be free of race conditions when accessed by multiple threads simultaneously.
Run Code Online (Sandbox Code Playgroud)

我可以说ConcurrentHashMap是线程安全的,但没有完全同步吗?这里正确的术语是什么?

java multithreading thread-safety race-condition thread-synchronization

2
推荐指数
1
解决办法
92
查看次数