本文将介绍Objective-C的multithreading库的详细情况,特别是关于objective-c的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及
本文将介绍Objective-C的multithreading库的详细情况,特别是关于objective -c的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于.NET中的multithreading处理、angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])、Archiving Objective-C Objects with NSCoding、c++11 Enable multithreading to use std::thread: Op的知识。
本文目录一览:- Objective-C的multithreading库(objective -c)
- .NET中的multithreading处理
- angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])
- Archiving Objective-C Objects with NSCoding
- c++11 Enable multithreading to use std::thread: Op
Objective-C的multithreading库(objective -c)
不包括cocoa(和它的NSThread ),你会推荐什么multithreading库?
应用程序的引擎必须运行在多个平台上(Windows,Linux,MacOS,iPhone),并且是multithreading的。 抽象该库以针对平台特定的MT库进行编译是可能的,但是会产生额外的开销和复杂度。
ASIFormDataRequest为空的POST请求
我如何追踪室内位置?
如何在Windows机器上将ipa文件添加到iTunes中?
Objective C编辑器为windows
使用Cygwin为WindowsXP构buildiPhone
NSOperation工作正常。 作为额外的奖励,你可以免费得到一个线程池,并且可以在操作之间建立一个依赖链。
那么C图书馆里的东西呢? 例如libapr: http ://apr.apache.org/docs/apr/1.3/group__apr__os__thread.html
还是glib? http://library.gnome.org/devel/glib/stable/glib-Threads.html
问候
我将从最近开源的Grand Central开始: http : //libdispatch.macosforge.org/
总结
以上是小编为你收集整理的Objective-C的multithreading库全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
.NET中的multithreading处理
我已经有了一些想法,但是如果可能的话,我想听听大家的不同意见和select。
我有一个使用Exchange Web服务连接到Exchange并下载电子邮件的Windows控制台应用程序。 目标是采取每个单独的消息对象,提取元数据,parsing附件等。该应用程序正在检查收件箱每60秒。 连接到收件箱并获取消息对象没有问题。 这很好。
这里是我接受你的input的地方:当我收到一个消息对象时,我立即想要处理这个消息,并完成上面解释的所有繁忙的工作。 我正在考虑一些不同的方法:
将电子邮件对象排列在一个表中并逐个处理它们。
将电子邮件对象传递给本地Windows服务以完成繁忙的工作。
我不认为db排队会是一个好方法,因为有时需要处理多个电子邮件对象。 在处理具有5个附件的高优先级电子邮件之前处理具有30个附件的低优先级电子邮件是不公平的。 换句话说,电子邮件中较低的电子邮件不需要排队等待处理。 这就像在商店排队,在你前面的一个骷髅头的单一登记册扫描100件物品。 这不公平。 我的电子邮件对象的概念相同。
在控制台应用程序中回应问题(Linux)
如何在GUI中显示Perl控制台输出?
Shell命令以目录模式查找文件
从stdin读取非阻塞
在Windows上加速文本输出,用于控制台
我对Windows服务方法有些不确定。 不过,我非常有信心,我可以安装服务监听,等待指令处理新的电子邮件。 如果我有5个独立的电子邮件对象,我可以对Windows服务进行5个独立的调用并进行处理而不发生冲突吗?
我接受build议或替代方法。 但是,解决scheme必须使用.NET技术堆栈来呈现。
我如何find当前的虚拟terminal
psql控制台不接受本地化
控制台应用程序在Visual Studio中打开后立即closures
如何在“top”linux命令中执行控制台input?
如何以编程方式将控制台字体设置为Raster Font?
一种选择是在控制台应用程序中进行处理。 你看起来像一个生产者(获取电子邮件的线程)和多个消费者的标准生产者 – 消费者问题。 这很容易用BlockingCollection来处理。
我假设你的消息类型(你从邮件服务器得到的)被称为MailMessage 。
所以你在类的作用域创建一个BlockingCollection<MailMessage> 。 我还会假设你有一个计时器,每60秒计时一次,收集消息并排入队列:
private BlockingCollection<MailMessage> MailMessageQueue = new BlockingCollection<MailMessage>(); // Timer is created as a one-shot and re-initialized at each tick. // This prevents the timer proc from being re-entered if it takes // longer than 60 seconds to run. System.Threading.Timer ProducerTimer = new System.Threading.Timer( TimerProc,null,TimeSpan.FromSeconds(60),TimeSpan.FromMilliseconds(-1)); void TimerProc(object state) { var newMessages = GetMessagesFromserver(); foreach (var msg in newMessages) { MailMessageQueue.Add(msg); } ProducerTimer.Change(TimeSpan.FromSeconds(60),TimeSpan.FromMilliseconds(-1)); }
您的消费者线程只是读取队列:
void MessageProcessor() { foreach (var msg in MailMessageQueue.GetConsumingEnumerable()) { ProcessMessage(); } }
计时器将使生产者每分钟运行一次。 要启动消费者(比如说你想要两个):
var t1 = Task.Factory.StartNew(MessageProcessor,TaskCreationoptions.LongRunning); var t2 = Task.Factory.StartNew(MessageProcessor,TaskCreationoptions.LongRunning);
所以你将有两个线程处理消息。
有更多的处理线程比你有可用的cpu核心是没有意义的。 生产者线程可能不需要大量的cpu资源,所以你不必专门给它一个线程。 只要它正在做它的事情,它只会简单地减缓消息处理。
我在上面的描述中略过了一些细节,特别是取消了线程。 当你想停止程序,但让消费者完成处理消息,只需要杀死生产者定时器,并设置队列完成添加:
MailMessageQueue.CompleteAdding();
消费者将清空队列并退出。 您当然想等待任务完成(请参阅Task.Wait )。
如果您希望在不清空队列的情况下杀死消费者,则需要查看“ 取消” 。
BlockingCollection的默认后备存储是一个ConcurrentQueue ,它是一个严格的FIFO。 如果你想优先考虑的事情,你需要提出一个实现IProducerConsumerCollection接口的并发优先级队列。 .NET没有这样的事情(或者甚至是一个优先级队列类),但是一个简单的使用锁来阻止并发访问的二进制堆就足够了。 你不是在说很难打这个东西。
当然,你需要一些方法来优先消息。 可能按附件数量排序,以便更快地处理没有附件的邮件。 另一个选择是拥有两个单独的队列:一个用于带有0或1个附件的邮件,另一个用于拥有大量附件的队列。 你可以让你的一个消费者专用于0或1队列,这样容易的消息总是有很好的机会被首先处理,其他消费者从0或1队列中取出,除非它是空的,然后从另一个队列中取出。 这会让你的消费者变得更复杂一些,但不是那么重要。
如果选择将消息处理移动到单独的程序中,则需要一些方法将生产者的数据保存到使用者。 有很多可能的方式来做到这一点,但我只是没有看到它的优势。
我在这里是一个新手,但似乎最初的方法可能是有一个单独的高优先级队列。 每当有工作人员可以获得新消息时,可以这样做:
If DateTime.Now - lowPriorityQueue.Peek.AddedTime < maxWaitTime Then ProcessMessage(lowPriorityQueue.Dequeue()) Else If highPriorityQueue.Count > 0 Then ProcessMessage(highPriorityQueue.Dequeue()) Else ProcessMessage(lowPriorityQueue.Dequeue()) End If
在单个线程中,虽然仍然可以让一条消息阻塞其他消息,但可以更快地处理更高优先级的消息。
根据大多数消息处理的速度,如果队列变得太大或太旧,应用程序可能会在新线程上创建新的工作人员。
请告诉我,尽管我完全脱离了这里。
angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])
Can’t resolve all parameters for AuthenticationService: ([object Object],?,[object Object])
我已经检查了几乎每个主题,并尝试了多种方法来解决它,但仍然无法在第二天击败它.
我试图像这样在appService中注入第一个authService但是得到了同样的错误
@Inject(forwardRef(() => AuthenticationService)) public authService: AuthenticationService
我检查了所有DI和服务内部的导入顺序,在我看来一切都是正确的
如果有人可以帮我处理它,我很感激.
Angular 4.0.0
AuthService
import { Injectable } from '@angular/core'; import {Http,Headers,Response} from '@angular/http'; import 'rxjs/add/operator/toPromise'; import {Observable} from 'rxjs/Rx'; import {AppServices} from "../../app.services"; import {Router} from "@angular/router"; @Injectable() export class AuthenticationService { public token: any; constructor( private http: Http,private appService: AppServices,private router: Router ) { this.token = localStorage.getItem('token'); } login(username: string,password: string): Observable<boolean> { let headers = new Headers(); let body = null; headers.append("Authorization",("Basic " + btoa(username + ':' + password))); return this.http.post(this.appService.api + '/login',body,{headers: headers}) .map((response: Response) => { let token = response.json() && response.json().token; if (token) { this.token = token; localStorage.setItem('Conform_token',token); return true; } else { return false; } }); } logout(): void { this.token = null; localStorage.removeItem('Conform_token'); this.router.navigate(['/login']); } }
应用服务
import {Injectable} from '@angular/core'; import {Headers,Http,RequestOptions} from '@angular/http'; import {Router} from "@angular/router"; import {AuthenticationService} from "./auth/auth.service"; import 'rxjs/add/operator/toPromise'; import {Observable} from 'rxjs/Rx'; @Injectable() export class AppServices { api = '//endpoint/'; public options: any; constructor( private http: Http,private router: Router,public authService: AuthenticationService // doesn't work // @Inject(forwardRef(() => AuthenticationService)) public authService: AuthenticationService // doesn't work either ) { let head = new Headers({ 'Authorization': 'Bearer ' + this.authService.token,"Content-Type": "application/json; charset=utf8" }); this.options = new RequestOptions({headers: head}); } // ==================== // data services // ==================== getData(): Promise<any> { return this.http .get(this.api + "/data",this.options) .toPromise() .then(response => response.json() as Array<Object>) .catch((err)=>{this.handleError(err);}) }
应用模块
import { browserModule } from '@angular/platform-browser'; import { browserAnimationsModule } from '@angular/platform-browser/animations'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import {BaseRequestOptions,HttpModule} from '@angular/http'; import { MaterialModule} from '@angular/material'; import {FlexLayoutModule} from "@angular/flex-layout"; import 'hammerjs'; import { routing,appRoutingProviders } from './app.routing'; import { AppServices } from './app.services'; import {AuthGuard} from "./auth/auth.guard"; import {AuthenticationService} from "./auth/auth.service"; import {AppComponent} from './app.component'; import {AuthComponent} from './auth/auth.component'; import {NotFoundComponent} from './404/not-found.component'; import { HomeComponent } from './home/home.component'; @NgModule({ declarations: [ AppComponent,AuthComponent,NotFoundComponent,HomeComponent ],imports: [ browserModule,browserAnimationsModule,FormsModule,HttpModule,routing,MaterialModule,FlexLayoutModule ],providers: [AppServices,AuthGuard,AuthenticationService],bootstrap: [AppComponent] }) export class AppModule { }
解决方法
你可以使用
export class AuthenticationService { public token: any; appService: AppServices; constructor( private http: Http,// private appService: AppServices,injector:Injector; private router: Router ) { setTimeout(() => this.appService = injector.get(AppServices)); this.token = localStorage.getItem('token'); }
另见DI with cyclic dependency with custom HTTP and ConfigService
要避免使用setTimeout,您还可以从AppService的构造函数中设置AuthenticationService.appService(或者相反)
Archiving Objective-C Objects with NSCoding
For the seasoned Cocoa developer,this is a piece of cake. For newer developers,this can be a real pain,especially if you don't kNow what you're looking for. I get this question a decent amount,so I figured I'd put a quick guide together.
The Problem
You can't put just any object in a plist. This mainly gets people when they want to put something into NSUserDefaults and get an error (because NSUserDefaults archives to a plist under the hood).
Plists only support the core types: Nsstring
, NSNumber
,monospace">NSDate,monospace">NSData,monospace">NSArray,NSDictionary
(and their CF buddies thanks to the toll-free bridge). The key here isNSData
. You can convert any object to NSData
with the NSCoding
protocol.
The Solution
There are two things you have to do: implement NSCoding and then use the archiver and unarchiver.
Implementing NSCoding
Say you have an object that looks like this:
@interface Note : NSObject { Nsstring *title; Nsstring *author; BOOL published; } @property (nonatomic, copy) Nsstring *title; @property (nonatomic, copy) Nsstring *author; @property (nonatomic) BOOL published; @end
#import "Note.h" @implementation Note @synthesize title; @synthesize author; @synthesize published; - (void)dealloc { [title release]; [author release]; [super dealloc]; } @end
Pretty simple,right?
Now,all you have to do to implement NSCoding is the following:
NSObject <NSCoding> { Nsstring *title; Nsstring *author; dealloc { [title release]; [author release]; [super dealloc]; } - (id)initWithCoder:(NSCoder *)decoder { if (self = [super init]) { self.title = [decoder decodeObjectForKey:@"title"]; self.author = [decoder decodeObjectForKey:@"author"]; self.published = [decoder decodeBoolForKey:@"published"]; } return self; } - (encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:title forKey:@"time"]; [encoder encodeObject:author forKey:@"author"]; [encoder encodeBool:published forKey:@"published"]; } @end
Pretty simple. All I did was add the <NSCoding>
protocol delectation in the header file and initWithCoder:
and encodeWithCoder:
in the implementation. You use these methods to tell NSCoder
how to encode your object into data. Notice how two of the variables are objects and one was a BOOL
. You have to use different methods for non-objects. The NSCoder documentation has the full list.
Remember,that you can use NSCoder
to archive your object however whatever you want. It doesn't have to just be all of the instance variables in your object,although that's what you'll do 90% of the time.
Using the Archiver and Unarchiver
This part is also really easy. Let's say you have an array of notes that you want to put into NSUserDefaults
,here's the code:
// Given `notes` contains an array of Note objects NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notes]; [[NSUserDefaults standardUserDefaults] setobject:data forKey:@"notes"];
Unarchiving is just as easy:
NSData *notesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"notes"]; NSArray *notes = [NSKeyedUnarchiver unarchiveObjectWithData:notesData];
c++11 Enable multithreading to use std::thread: Op
gcc4.6 以后对于 ld 自动加上了 as-needed 选项。所以编译选项应该变成:
g++ -Wl,--no-as-needed -std=c++11 -pthread a.cpp
这样就没有问题了!
关于Objective-C的multithreading库和objective -c的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于.NET中的multithreading处理、angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])、Archiving Objective-C Objects with NSCoding、c++11 Enable multithreading to use std::thread: Op的相关信息,请在本站寻找。
本文标签: