Cocoa multiple threads, locks don't work
Posted
by Igor
on Stack Overflow
See other posts from Stack Overflow
or by Igor
Published on 2010-04-04T22:37:21Z
Indexed on
2010/04/05
4:43 UTC
Read the original article
Hit count: 308
cocoa
|multithreading
I have a threadMethod which shows in console robotMotorsStatus every 0.5 sec. But when I try to change robotMotorsStatus in changeRobotStatus method I receive an exception. Where I need to put locks in that program.
#import "AppController.h"
@implementation AppController
extern char *robotMotorsStatus;
- (IBAction)runThread:(id)sender
{
[self performSelectorInBackground:@selector(threadMethod) withObject:nil];
}
- (void)threadMethod
{
char string_to_send[]="QFF001100\r"; //String prepared to the port sending (first inintialization)
string_to_send[7] = robotMotorsStatus[0];
string_to_send[8] = robotMotorsStatus[1];
while(1){
[theLock lock];
usleep(500000);
NSLog (@"Robot status %s", robotMotorsStatus);
[theLock unlock];
}
}
- (IBAction)changeRobotStatus:(id)sender
{
robotMotorsStatus[0]='1';
}
© Stack Overflow or respective owner