Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+中的线程同步+/仅使用原子操作的CLI 我目前试图在C++中使用.St::TnRe::.NET中的互锁类,只使用原子操作实现线程同步。我对多线程处理没有太多经验,我正在尝试使用无锁线程同步。目前,我已经创建了一个名为settings的类,其中包含需要在线程之间共享的静态变量。然后我在其中创建了两个静态函数,一个用于设置静态数据成员,另一个用于读取它们。目前,这就是我的一个静态同步函数的样子我知道如果两个以上的线程同时进入这个函数,它们可能会永远卡在while循环中,但是只有两个线程需要这个函数,一个GUI线程和一个主线程,它们将读取设置并将工作分派给工作线程 //object is handle to instance of settings class that also contains non-static //members that will contain each threads copy of the data. void Settings::SetStaticVariables(Settings ^object) { int returnvalue; //canchange variable is a static integer of the class "Settings" returnvalue = Threading::Interlocked::Increment(Settings::canchange); if(returnvalue > 1) { while(Settings::canchange > 1) { //perhaps eventually I will find an alternative to telling the thread //to sleep for a defined amount of time, maybe when I learn how to use events //for now this will do, speed is not very important for this data sync as //it does not occure often Threading::Thread::Sleep(50); } } //data synchronization of static members here //decrement allowing waiting threads to exit while loop Threading::Interlocked::Decrement(Settings::canchange); };_.net_Multithreading_Synchronization_C++ Cli_Atomic - Fatal编程技术网 1) { while(Settings::canchange > 1) { //perhaps eventually I will find an alternative to telling the thread //to sleep for a defined amount of time, maybe when I learn how to use events //for now this will do, speed is not very important for this data sync as //it does not occure often Threading::Thread::Sleep(50); } } //data synchronization of static members here //decrement allowing waiting threads to exit while loop Threading::Interlocked::Decrement(Settings::canchange); };,.net,multithreading,synchronization,c++-cli,atomic,.net,Multithreading,Synchronization,C++ Cli,Atomic" /> 1) { while(Settings::canchange > 1) { //perhaps eventually I will find an alternative to telling the thread //to sleep for a defined amount of time, maybe when I learn how to use events //for now this will do, speed is not very important for this data sync as //it does not occure often Threading::Thread::Sleep(50); } } //data synchronization of static members here //decrement allowing waiting threads to exit while loop Threading::Interlocked::Decrement(Settings::canchange); };,.net,multithreading,synchronization,c++-cli,atomic,.net,Multithreading,Synchronization,C++ Cli,Atomic" />

C+中的线程同步+/仅使用原子操作的CLI 我目前试图在C++中使用.St::TnRe::.NET中的互锁类,只使用原子操作实现线程同步。我对多线程处理没有太多经验,我正在尝试使用无锁线程同步。目前,我已经创建了一个名为settings的类,其中包含需要在线程之间共享的静态变量。然后我在其中创建了两个静态函数,一个用于设置静态数据成员,另一个用于读取它们。目前,这就是我的一个静态同步函数的样子我知道如果两个以上的线程同时进入这个函数,它们可能会永远卡在while循环中,但是只有两个线程需要这个函数,一个GUI线程和一个主线程,它们将读取设置并将工作分派给工作线程 //object is handle to instance of settings class that also contains non-static //members that will contain each threads copy of the data. void Settings::SetStaticVariables(Settings ^object) { int returnvalue; //canchange variable is a static integer of the class "Settings" returnvalue = Threading::Interlocked::Increment(Settings::canchange); if(returnvalue > 1) { while(Settings::canchange > 1) { //perhaps eventually I will find an alternative to telling the thread //to sleep for a defined amount of time, maybe when I learn how to use events //for now this will do, speed is not very important for this data sync as //it does not occure often Threading::Thread::Sleep(50); } } //data synchronization of static members here //decrement allowing waiting threads to exit while loop Threading::Interlocked::Decrement(Settings::canchange); };

C+中的线程同步+/仅使用原子操作的CLI 我目前试图在C++中使用.St::TnRe::.NET中的互锁类,只使用原子操作实现线程同步。我对多线程处理没有太多经验,我正在尝试使用无锁线程同步。目前,我已经创建了一个名为settings的类,其中包含需要在线程之间共享的静态变量。然后我在其中创建了两个静态函数,一个用于设置静态数据成员,另一个用于读取它们。目前,这就是我的一个静态同步函数的样子我知道如果两个以上的线程同时进入这个函数,它们可能会永远卡在while循环中,但是只有两个线程需要这个函数,一个GUI线程和一个主线程,它们将读取设置并将工作分派给工作线程 //object is handle to instance of settings class that also contains non-static //members that will contain each threads copy of the data. void Settings::SetStaticVariables(Settings ^object) { int returnvalue; //canchange variable is a static integer of the class "Settings" returnvalue = Threading::Interlocked::Increment(Settings::canchange); if(returnvalue > 1) { while(Settings::canchange > 1) { //perhaps eventually I will find an alternative to telling the thread //to sleep for a defined amount of time, maybe when I learn how to use events //for now this will do, speed is not very important for this data sync as //it does not occure often Threading::Thread::Sleep(50); } } //data synchronization of static members here //decrement allowing waiting threads to exit while loop Threading::Interlocked::Decrement(Settings::canchange); };,.net,multithreading,synchronization,c++-cli,atomic,.net,Multithreading,Synchronization,C++ Cli,Atomic,我的问题是,您是否发现有任何缺陷不能满足我的期望,或者同步的整个想法是否有缺陷?如果您能够确定多线程只适用于两个线程,那么您对多线程处理有很好的眼光。不要让人们阻止你学习无锁技术 您可以使用InterlockedExchange而不是increment,这将消除>=2个线程可能出现的死锁。然而,该算法根本不是无锁编程,而是实现自己的锁。当然,这意味着您将遇到许多性能和正确性问题,这些问题已经由库锁定类(Monitor和friends)解决了 下面是它的外观 “我对多线程处理一点经验都没有,我正在

我的问题是,您是否发现有任何缺陷不能满足我的期望,或者同步的整个想法是否有缺陷?

如果您能够确定多线程只适用于两个线程,那么您对多线程处理有很好的眼光。不要让人们阻止你学习无锁技术

您可以使用
InterlockedExchange
而不是increment,这将消除>=2个线程可能出现的死锁。然而,该算法根本不是无锁编程,而是实现自己的锁。当然,这意味着您将遇到许多性能和正确性问题,这些问题已经由库锁定类(
Monitor
和friends)解决了

下面是它的外观


“我对多线程处理一点经验都没有,我正在尝试使用无锁线程同步。”这很可能会导致糟糕的结果。编写正确的多线程代码是很困难的,而实现无锁则更为困难。您是否尝试过使用锁并对其进行分析,以了解锁是否是实际的性能热点?我没有尝试过使用任何锁,但由于这段代码的性能一点都不重要,因此我可以尝试使用锁。如果你认为这是最好的路线,那么我绝对可以用它们。我只是觉得获得无锁同步的经验会很好,因为我在一篇关于多线程的文章中读到,最好尽可能少地使用锁。此外,我最终会(尝试)编写速度确实重要的代码,但现在,我想我可以熟悉锁了。@James:+1+如果可以的话@杰克:一般来说,避免锁确实是一个很好的做法,但更重要的是关于避免无锁同步的规则——特别是,“永远不要编写无锁同步代码,除非你是一位有几十年经验的专家,并且你有一支由多线程程序员和高级数学家组成的优秀团队随时准备检查你的工作。“正确:首先要熟悉使用锁进行线程同步的基本原理。一旦您熟悉了基本原理,原子就可以用于非常基本的无锁编程,尽管在您非常熟悉编写同步代码之前,我不会尝试对它们做任何太复杂的事情(我的意思一点也不是居高临下;我写了很多线程错误,我坚信坚持简单、直接、常用的模式,这些模式很容易推理。)我确信,我现在会使用锁。在做了更多的研究后,我发现编译器和处理器有时会重新排序操作,这可能会导致同步部分中的数据在离开后发生更改。也许最终我会尝试另一个程序,只为测试数据同步,而不需要锁,但现在,我会使用它们。希望当我上大学学习计算机科学时,我会对并行性有更好的理解。谢谢你们的建议。谢谢你们的建议,但我不会尝试不使用锁,直到我比现在更好地理解了使用锁和多线程。另外,如果你们可以的话,有多精确ly是“无锁”多线程,与我实现自己的锁不同(我想我甚至在问LOL这个问题之前应该更好地理解它)J.JAK:对于一个真正的无锁编程的例子,考虑Win32,它对于多生产者单一用户线程安全队列是有用的。实际上,我使用C++的API实现了一个C++中的相同的数据结构,唯一的不同在于,由于C++模板的魔力,我的类型是安全的。无锁编程中的一个关键角色。我知道已经有一段时间了,但当我用一个线程进行更改时,memorybarrier是否也会刷新处理器缓存,或者我是否需要使用易失性写入和读取来保持缓存一致性?或者这是由处理器自动维护的?内存屏障会阻止处理器重新排序rea当系统总线实现缓存一致性协议时,实际上可能不需要刷新缓存——大部分影响实际上是在CPU管道引擎上,包括无序执行和推测性执行。Volatile完全是另一回事,它阻止编译器重新排列或删除缓存或生成指令以推测性地执行内存访问。(除了在VC++2008和更高版本中,volatile还添加了内存障碍。)那么,我真的对volatile有任何用处吗?我从几篇文章中发现,在大多数情况下,volatile对多线程处理是毫无用处的,并在这里和那里回答了一些问题。我应该使用它,以防我遇到一个由单个线程共享和频繁更改的变量,以确保它不会保留在寄存器中,还是我应该完成它你应该忽略这个词吗
// try to acquire lock
while (0 != Interlocked::Exchange(Settings::canchange, 1))
{
   Thread::Sleep(50);
}

MemoryBarrierAcquire();

// update shared variables

// flush cached writes
MemoryBarrierRelease();
// unlock
Settings::canchange = 0;