この質問のあいまいなタイトルで申し訳ありません。私が抱えている問題を説明するための簡潔な文が思いつきませんでした。
基本的に、複数のスレッドを開始し、そのコンテキスト内で使用できるスレッド固有の値を持つオブジェクトを渡したいと考えています。これがそれを行うコードです
while (count < no_threads) {
_thread_bytes = _file_length / no_threads;
_thread_offset = count * _thread_bytes;
NSDictionary *args = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:_thread_offset],@"thread_offset",
[NSNumber numberWithInt:_thread_bytes], @"thread_bytes",
[NSNumber numberWithInt:count], @"count", nil];
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(processServerThread:) object:args];
[thread start];
count++;
}
thread_bytes
各スレッドは、正しいおよびを使用してセレクター メソッドを呼び出し、ソケットにthread_offset
アクセスすることもできGCDAsyncSocket
ます。その結果、thread_bytes
およびを使用する多数のデリゲート メソッド呼び出しが発生しますthread_offset
。これがセレクターメソッドです。
- (void)processServerThread:(id)sender
{
NSError *err;
dispatch_queue_t iQueue = dispatch_queue_create("main_server_queue", NULL);
GCDAsyncSocket *socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:iQueue];
if (![socket connectToHost:@"example.com" onPort:8080 error:&err]) {
NSLog(@"Error: couldn't connect to file server....");
return;
}
NSData *message =[[self requestMessageWithOffset:[NSNumber numberWithInt:_thread_offset]
andBytes:[NSNumber numberWithInt:_thread_bytes]]
dataUsingEncoding:NSUTF8StringEncoding];
[socket writeData:message withTimeout:SOCKET_TIMEOUT tag:TAG_FILE_REQUEST_WRITE];
}
処理中の N 個のスレッドがある場合、リクエスト メッセージを送信すると_thread_offset
、たとえばオフセットが 0、2、4、8 の場合、1 つのスレッドが 0、他のすべてのスレッドが 8 と表示されますそのコンテキスト内の一意のオフセット。
また、スレッドが終了したときのコールバック メソッドはありますか?