ResumeThread on POSIX


 
Thread Tools Search this Thread
Top Forums Programming ResumeThread on POSIX
# 1  
Old 02-16-2009
ResumeThread on POSIX

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
# 2  
Old 02-18-2009
pthread_suspend()
pthread_continue()

?? will these not work?
# 3  
Old 02-18-2009
pthread_suspend() is not portable,

You may try pthread_cond_wait() or pthread_cond_timedwait()
# 4  
Old 02-23-2009
Hi all

thanks for answers

I try with pthread_cond_*, but i have callback function and i have a big problem.

Code:
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
Code:
auto_event::wait(auto_event::auto_event_impl* other)

function.
Please help.

BR
// Kolesar
# 5  
Old 02-24-2009
Hi again Smilie

I try with semaphore. Program is working, but very very slowly :S

Code:
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
# 6  
Old 02-25-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Change value for POSIX

Hi, I have a VM with following configration . 3.10.0-693.1.1.el7.x86_64 #1 SMP Thu Aug 3 08:15:31 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux My current POSIX is :-- Your environment variables take up 2011 bytes POSIX upper limit on argument length (this system): 2093093 POSIX smallest... (15 Replies)
Discussion started by: Abhayman
15 Replies

2. OS X (Apple)

POSIX compliance...

Thanks to all you guys about posix compliance I have learnt an enormous amount over the last few days. I have written a program that is an Egg Timer with simple animation. I now realise how sophisticated 'bash' is compared to full posix compliance. The code below has passed all of the tests from... (11 Replies)
Discussion started by: wisecracker
11 Replies

3. Programming

POSIX Thread Help

I want to create a program that creates 2 child process, and each of them creates 2 threads, and each thread prints its thread id. I0ve allread done that the outuput isn't the outuput i want. When a run the following comand "$./a.out | sort -u | wc -l" I have the folowing output 2 $: It should... (3 Replies)
Discussion started by: pharaoh
3 Replies

4. UNIX for Advanced & Expert Users

System V or POSIX

Hi , I am using UNIX network programming Vol1 (by R Stevens) book to learn about IPC. I would be using HP-UX,Solaris and Linux at my work. I have sections for POSIX and for System V in that book. I am quite confused in indentifying those OSs as POSIX or SYstem V. Can anyone please... (1 Reply)
Discussion started by: kumaran_5555
1 Replies

5. What is on Your Mind?

Linux posix

Hi everybody, i couldn't think of any better place to ask this question. Does LINUX totally confirm with ALL of the POSIX standards??. If not which areas does it diverge?? my apologies if this questions seems sooo stupid to some of you.. thanks (0 Replies)
Discussion started by: abhiram7
0 Replies

6. UNIX for Advanced & Expert Users

Posix threads

Hi, consider the code below: #include <stdio.h> . . struct myStruct { char *message ; int id; }; . . . void *thread_function( void *ptr ); nt main() { pthread_t thread1, thread2 ,thread3 ; struct myStruct nico1; (2 Replies)
Discussion started by: Behnaz
2 Replies

7. Programming

Posix

HI, When i am configuring php in SUN Solaris. I am getting the below error. configure: error: Your system seems to lack POSIX threads. Do i need to install POSIX? If so can somebody let me know where can i download POSIX for Solaris 8? Thanks, (2 Replies)
Discussion started by: Krrishv
2 Replies

8. Programming

POSIX threads

Hello ! Let's supose I have a main function in C , and two POSIX threads. I give you an example down : int main() { int something; char else; void *FirstThread(); void *SecondThread(); .. <start those two pthreads ..> return 0;} void *FirstThread() { ... } void *SecondThread()... (2 Replies)
Discussion started by: !_30
2 Replies

9. UNIX for Dummies Questions & Answers

how to read POSIX?

how to read POSIX? poe six or not? (3 Replies)
Discussion started by: robin.zhu
3 Replies

10. UNIX for Dummies Questions & Answers

Posix and linux

What is posix? What is the relation between Posix, Unix and linux? (1 Reply)
Discussion started by: darbarilal
1 Replies
Login or Register to Ask a Question