Hi all
How i can create thread in SUSPENDED mode, and resume when i want.
I have win code with CreateThread (parameter CREATE_SUSPEND), and Resume Thread, but on POSIX with pthread i cant do it.
Please help me.
Best regards
// Kolesar
Hi all
How i can create thread in SUSPENDED mode, and resume when i want.
I have win code with CreateThread (parameter CREATE_SUSPEND), and Resume Thread, but on POSIX with pthread i cant do it.
Please help me.
Best regards
// Kolesar
pthread_suspend()
pthread_continue()
?? will these not work?
pthread_suspend() is not portable,
You may try pthread_cond_wait() or pthread_cond_timedwait()
Hi all
thanks for answers
I try with pthread_cond_*, but i have callback function and i have a big problem.
void auto_event::set()
{
#if _WIN32
assert(m_evt);
if (!::SetEvent(m_evt))
{
// error out
}
#elif _LINUX
if(m_count == 0)
{
m_count = 1;
if(::pthread_cond_signal(&m_cond) != 0)
{
// error out
}
}
#endif
}
void auto_event::wait()
{
#if _WIN32
assert(m_evt);
if (::WaitForSingleObject(m_evt, INFINITE) == WAIT_FAILED)
{
// error out
}
#elif _LINUX
if(m_count == 0)
{
if(::pthread_cond_wait(&m_cond, &m_mutex) != 0)
{
// error out
}
}
m_count = 0;
#endif
}
/// for callback function
int auto_event::wait(auto_event::auto_event_impl* other)
{
int result=-1;
#if _WIN32
assert(m_evt);
assert(other->m_evt);
const HANDLE objs[2] = { m_evt, other->m_evt };
DWORD wmo = ::WaitForMultipleObjects(2, objs, FALSE, INFINITE);
switch (wmo)
{
case WAIT_FAILED:
// error out
case WAIT_IO_COMPLETION:
// error out
case WAIT_TIMEOUT:
// error out
case WAIT_ABANDONED_0:
case WAIT_ABANDONED_0 + 1:
// error out
case WAIT_OBJECT_0:
case WAIT_OBJECT_0 + 1:
result = static_cast<int>(wmo - WAIT_OBJECT_0);
break;
default:
// error out
}
return result;
#elif _LINUX
int rc = 0;
while (rc == 0)
rc = pthread_cond_wait(&m_cond, &m_mutex);
if (rc == 0)
{
return 0;
}
rc = 0;
while (rc == 0)
rc = pthread_cond_wait(&other->m_cond, &other->m_mutex);
if (rc == 0)
{
return 1;
}
#endif
}
void auto_event::lock() const
{
if(::pthread_mutex_lock((pthread_mutex_t*)&m_mutex) !=0)
{
// error out
}
}
void auto_event::unlock() const
{
if(::pthread_mutex_unlock((pthread_mutex_t*)&m_mutex) !=0)
{
// error out
}
}
void auto_event::lock(auto_event::auto_event_impl* other) const
{
if(::pthread_mutex_lock((pthread_mutex_t*)&other->m_mutex) !=0)
{
// error out
}
}
void auto_event::unlock(auto_event::auto_event_impl* other) const
{
if(::pthread_mutex_unlock((pthread_mutex_t*)&other->m_mutex) !=0)
{
// error out
}
}
I don't know how to correct write
auto_event::wait(auto_event::auto_event_impl* other)
function.
Please help.
BR
// Kolesar
Hi again 
I try with semaphore. Program is working, but very very slowly :S
void auto_event::set()
{
#if _WIN32
assert(m_evt);
if (!::SetEvent(m_evt))
{
// error out
}
#elif _LINUX
int ret=0;
if ((ret=sem_post(&(this->sem))) != 0) {
printf("sem_post return error:[%d]\n", ret);
}
#endif
}
void auto_event::wait()
{
#if _WIN32
assert(m_evt);
if (::WaitForSingleObject(m_evt, INFINITE) == WAIT_FAILED)
{
// error out
}
#elif _LINUX
if((ret=sem_wait(&(this->sem))) != 0)
printf("error\n");
#endif
}
/// for callback function
int auto_event::wait(auto_event::auto_event_impl* other)
{
int result=-1;
#if _WIN32
assert(m_evt);
assert(other->m_evt);
const HANDLE objs[2] = { m_evt, other->m_evt };
DWORD wmo = ::WaitForMultipleObjects(2, objs, FALSE, INFINITE);
switch (wmo)
{
case WAIT_FAILED:
// error out
case WAIT_IO_COMPLETION:
// error out
case WAIT_TIMEOUT:
// error out
case WAIT_ABANDONED_0:
case WAIT_ABANDONED_0 + 1:
// error out
case WAIT_OBJECT_0:
case WAIT_OBJECT_0 + 1:
result = static_cast<int>(wmo - WAIT_OBJECT_0);
break;
default:
// error out
}
return result;
#elif _LINUX
int milliseconds = 2001;
struct timespec timeout;
timeout.tv_sec = time(NULL) + milliseconds / 1000;
timeout.tv_nsec = (milliseconds % 1000) * 1000000;
if(sem_trywait(&(this->m_sem)) == 0)
result = 0;
if(errno == EINVAL || errno == EINTR)
{ /*error*/}
if(sem_trywait(&(other->m_sem)) == 0)
result = 1;
if(errno == EINVAL || errno == EINTR)
{ /*error*/}
#endif
}
Please help me.
BR
// Kolesar
This code cannot compile by itself. Do you have the rest? Or could you make a reduced, complete program that displays the same problem?
I do not know what your timeout code is supposed to do there. You're just creating a variable and setting values in it but not actually using the variable to do anything.