首页 \ 问答 \ 锁定自由数组元素交换(Lock Free Array Element Swapping)

锁定自由数组元素交换(Lock Free Array Element Swapping)

在多线程环境中,为了进行线程安全的数组元素交换,我们将执行同步锁定。

// a is char array.
synchronized(a) {
    char tmp = a[1];
    a[1] = a[0];
    a[0] = tmp;
}

在上面的情况下我们是否可以使用以下API,以便我们可以进行无锁数组元素交换? 如果有,怎么样?

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.html#compareAndSet%28T,%20V,%20V%29


In multi-thread environment, in order to have thread safe array element swapping, we will perform synchronized locking.

// a is char array.
synchronized(a) {
    char tmp = a[1];
    a[1] = a[0];
    a[0] = tmp;
}

Is it possible that we can make use of the following API in the above situation, so that we can have a lock free array element swapping? If yes, how?

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.html#compareAndSet%28T,%20V,%20V%29


原文:https://stackoverflow.com/questions/1231913
更新时间:2023-06-23 09:06

最满意答案

是的,这是正确的方式,您在NSLog上获得相同的结果,只需点击页面并显示源代码即可。 你的url index.php是纯文本而不是由服务器解释,这就是你没有“Hello,PHP!”的原因。 结果是。

请参阅php hello world tutorial: http//www.utexas.edu/learn/php/example1.shtml

结果: http//www.utexas.edu/learn/php/ex1.php

改变你的代码:

NSString *urlstr = [[NSString alloc] 
      initWithFormat:@"http://www.utexas.edu/learn/php/ex1.php"];
NSURL *url = [[NSURL alloc] initWithString:urlstr];

yes it's the right way, you have the same result on your NSLog as rigth click on your page and show source code. Your url index.php is in plain text and not interpreted by the server, is why you haven't "Hello, PHP!" as a result.

see php hello world tutorial : http://www.utexas.edu/learn/php/example1.shtml

the result: http://www.utexas.edu/learn/php/ex1.php

change your code :

NSString *urlstr = [[NSString alloc] 
      initWithFormat:@"http://www.utexas.edu/learn/php/ex1.php"];
NSURL *url = [[NSURL alloc] initWithString:urlstr];

相关问答

更多
  • 您需要NULL终止缓冲区。 使缓冲区大小为6个字符,并将其初始化为零。 现在只填充前5个位置,单独留下最后一个位置。 char buffer[6] = {0}; // <-- Zero initializes the array for(int x = 0; x < 5; x++){ buffer[x] = in.get(); cout << x << " " << buffer[x] << endl; } cout << buffer << endl; return buffer; 或 ...
  • 我认为NSPredicate本身并没有办法。 您似乎要求的是排序结果。 您可以通过在从谓词中返回结果后对结果进行排序来完成此操作。 在这种情况下,由于排序顺序不仅仅是按字母顺序排列,因此您应该使用NSArray的-sortedArrayUsingComparator:方法。 像这样的东西(没有经过测试,键入了我的头顶)。 NSArray *sortedStrings = [filtered sortedArrayUsingComparator:^NSComparisonResult(id obj1, id ...
  • 为了让您的输出成为数据的十六进制编码,请按照此处找到的方法进行操作。 我稍微修改它们作为扩展方法: public static string ToHexString(this byte[] bytes) { return bytes == null ? string.Empty : BitConverter.ToString(bytes).Replace("-", string.Empty); } public static byte[] FromHexSt ...
  • NSDateFormatter的格式不正确。 您需要匹配预期输入的格式: [dateFormatter setDateFormat:@"MMMM d, yyyy"]; The NSDateFormatter has the incorrect format. You need to match the format of the expected input: [dateFormatter setDateFormat:@"MMMM d, yyyy"];
  • 我从来没有使用过这个功能,但是对于“UILexicon”的快速网络搜索让我着迷于Apple的文档; 阅读和下面的链接填写图片很快。 应用程序扩展程序设计指南快速解释了哪些词典适用于: 每个自定义键盘(独立于其RequestsOpenAccess键的值)都可以通过UILexicon类访问基本的自动UILexicon词典。 利用这个课程以及您自己设计的词汇,在用户输入文本时提供建议和自动更正。 单击UILexicon链接将我带到该类的参考文档,这说明它是Apple提供的术语对的只读列表。 它的每个条目都是一个U ...
  • 您从getIPAddresss()获得的地址是接口的地址,而不是多播组的地址。 这里是你的例子中如何使用地址,你能发现问题吗? // construct an IGMP join request structure mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str); mc_req.imr_interface.s_addr = htonl(INADDR_ANY) 您将多播地址设置为包含接口地址的mc_addr_str中的地址。 这看起来不正确吗? ;) ...
  • 那么在将它添加到urlString 之前 ,serverAdress中有什么。 看起来好像serverAdress包含'//server.dyndns.org/webservices/service1.asmx/getHourlySales2?tarih2=20.01.2011%2016:00&tarih1=19.01.2011%2016:00&salesType=Hepsi' So what's in serverAdress before you add it to urlString. Looks a ...
  • 是的,这是正确的方式,您在NSLog上获得相同的结果,只需点击页面并显示源代码即可。 你的url index.php是纯文本而不是由服务器解释,这就是你没有“Hello,PHP!”的原因。 结果是。 请参阅php hello world tutorial: http : //www.utexas.edu/learn/php/example1.shtml 结果: http : //www.utexas.edu/learn/php/ex1.php 改变你的代码: NSString *urlstr = [[NSS ...
  • 正如sln所指出的那样(他解决了这个问题),你不能匹配C中的任何东西,因为你必须打开多线选项(使用m),并且你在regexpal中匹配,因为它是打开的。 关于你的正则表达式,它可以用^(Page\s*\d+\s*:?\s*)$ 。 问号意味着前面的字符不必在这里, \s匹配任何空格类型字符(空格,制表符等)。 关于您的选择问题,正则表达式中的括号是捕获变量的内容。 所以,如果你这样做(Page( \d+|\d+))你将有两个不同的变量。 你想要的是(Page(?: \d+|\d+)) ,因为(?: ) :) ...
  • 我找到了一种方法...之后返回字符串 [self.dataTask resume]; I found a way... return the string after [self.dataTask resume];

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。