spandana
spandana

Reputation: 755

setting events in Objective C

I have this following code in cpp. I need to port it into Objective C. I am new to Objective C.

How to set the events and start threads in Objective C.

RS232Timer::RS232Timer()
{
 m_hThreadEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
 m_hThreadControlEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
 m_bThreadActive = false;
 m_bRunThread = false;
}

void RS232Timer::startThread()
{
 m_bRunThread = true;
 (void)ResetEvent(m_hThreadEvent);
 (void)ResetEvent(m_hThreadControlEvent);
 (void)AfxBeginThread(timeoutThread,(void*)this); //timeoutThread is another function
 if((waitForSingleObject(m_hThreadControlEvent,1500)!=WAIT_OBJECT_O)||!m_bThreadActive)
 {
  assert(FALSE);
  m_bThread = false;
 }
}

Upvotes: 0

Views: 758

Answers (2)

user1566276
user1566276

Reputation:

Hope this might help you:

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

[object runSomeLongOperation:^{
    // your own code here.
    dispatch_semaphore_signal(semaphore);
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);

Upvotes: 2

sergio
sergio

Reputation: 69047

This is not only a matter of porting from C++ to ObjC, but also from win32 to Cocoa...

I would suggest you to use NSThread and NSCondition.

Extremely useful is the Threading Programming Guide, specifically when it comes to synchronization.

No easy "statement-to-statement" porting is possible, I hope you understand.

Upvotes: 2

Related Questions