Sponsored Content
Full Discussion: ResumeThread on POSIX
Top Forums Programming ResumeThread on POSIX Post 302290451 by Kolesar on Monday 23rd of February 2009 10:06:41 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
PTHREAD_COND(3) 					     Library Functions Manual						   PTHREAD_COND(3)

NAME
pthread_cond_init, pthread_cond_destroy, pthread_cond_signal, pthread_cond_broadcast, pthread_cond_wait, pthread_cond_timedwait - opera- tions on conditions SYNOPSIS
#include <pthread.h> pthread_cond_t cond = PTHREAD_COND_INITIALIZER; int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr); int pthread_cond_signal(pthread_cond_t *cond); int pthread_cond_broadcast(pthread_cond_t *cond); int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime); int pthread_cond_destroy(pthread_cond_t *cond); DESCRIPTION
A condition (short for ``condition variable'') is a synchronization device that allows threads to suspend execution and relinquish the pro- cessors until some predicate on shared data is satisfied. The basic operations on conditions are: signal the condition (when the predicate becomes true), and wait for the condition, suspending the thread execution until another thread signals the condition. A condition variable must always be associated with a mutex, to avoid the race condition where a thread prepares to wait on a condition variable and another thread signals the condition just before the first thread actually waits on it. pthread_cond_init initializes the condition variable cond, using the condition attributes specified in cond_attr, or default attributes if cond_attr is NULL. The LinuxThreads implementation supports no attributes for conditions, hence the cond_attr parameter is actually ignored. Variables of type pthread_cond_t can also be initialized statically, using the constant PTHREAD_COND_INITIALIZER. pthread_cond_signal restarts one of the threads that are waiting on the condition variable cond. If no threads are waiting on cond, nothing happens. If several threads are waiting on cond, exactly one is restarted, but it is not specified which. pthread_cond_broadcast restarts all the threads that are waiting on the condition variable cond. Nothing happens if no threads are waiting on cond. pthread_cond_wait atomically unlocks the mutex (as per pthread_unlock_mutex) and waits for the condition variable cond to be signaled. The thread execution is suspended and does not consume any CPU time until the condition variable is signaled. The mutex must be locked by the calling thread on entrance to pthread_cond_wait. Before returning to the calling thread, pthread_cond_wait re-acquires mutex (as per pthread_lock_mutex). Unlocking the mutex and suspending on the condition variable is done atomically. Thus, if all threads always acquire the mutex before sig- naling the condition, this guarantees that the condition cannot be signaled (and thus ignored) between the time a thread locks the mutex and the time it waits on the condition variable. pthread_cond_timedwait atomically unlocks mutex and waits on cond, as pthread_cond_wait does, but it also bounds the duration of the wait. If cond has not been signaled within the amount of time specified by abstime, the mutex mutex is re-acquired and pthread_cond_timedwait returns the error ETIMEDOUT. The abstime parameter specifies an absolute time, with the same origin as time(2) and gettimeofday(2): an abstime of 0 corresponds to 00:00:00 GMT, January 1, 1970. pthread_cond_destroy destroys a condition variable, freeing the resources it might hold. No threads must be waiting on the condition vari- able on entrance to pthread_cond_destroy. In the LinuxThreads implementation, no resources are associated with condition variables, thus pthread_cond_destroy actually does nothing except checking that the condition has no waiting threads. CANCELLATION
pthread_cond_wait and pthread_cond_timedwait are cancellation points. If a thread is cancelled while suspended in one of these functions, the thread immediately resumes execution, then locks again the mutex argument to pthread_cond_wait and pthread_cond_timedwait, and finally executes the cancellation. Consequently, cleanup handlers are assured that mutex is locked when they are called. ASYNC-SIGNAL SAFETY The condition functions are not async-signal safe, and should not be called from a signal handler. In particular, calling pthread_cond_sig- nal or pthread_cond_broadcast from a signal handler may deadlock the calling thread. RETURN VALUE
All condition variable functions return 0 on success and a non-zero error code on error. ERRORS
pthread_cond_init, pthread_cond_signal, pthread_cond_broadcast, and pthread_cond_wait never return an error code. The pthread_cond_timedwait function returns the following error codes on error: ETIMEDOUT the condition variable was not signaled until the timeout specified by abstime EINTR pthread_cond_timedwait was interrupted by a signal The pthread_cond_destroy function returns the following error code on error: EBUSY some threads are currently waiting on cond. AUTHOR
Xavier Leroy <Xavier.Leroy@inria.fr> SEE ALSO
pthread_condattr_init(3), pthread_mutex_lock(3), pthread_mutex_unlock(3), gettimeofday(2), nanosleep(2). EXAMPLE
Consider two shared variables x and y, protected by the mutex mut, and a condition variable cond that is to be signaled whenever x becomes greater than y. int x,y; pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; Waiting until x is greater than y is performed as follows: pthread_mutex_lock(&mut); while (x <= y) { pthread_cond_wait(&cond, &mut); } /* operate on x and y */ pthread_mutex_unlock(&mut); Modifications on x and y that may cause x to become greater than y should signal the condition if needed: pthread_mutex_lock(&mut); /* modify x and y */ if (x > y) pthread_cond_broadcast(&cond); pthread_mutex_unlock(&mut); If it can be proved that at most one waiting thread needs to be waken up (for instance, if there are only two threads communicating through x and y), pthread_cond_signal can be used as a slightly more efficient alternative to pthread_cond_broadcast. In doubt, use pthread_cond_broadcast. To wait for x to becomes greater than y with a timeout of 5 seconds, do: struct timeval now; struct timespec timeout; int retcode; pthread_mutex_lock(&mut); gettimeofday(&now); timeout.tv_sec = now.tv_sec + 5; timeout.tv_nsec = now.tv_usec * 1000; retcode = 0; while (x <= y && retcode != ETIMEDOUT) { retcode = pthread_cond_timedwait(&cond, &mut, &timeout); } if (retcode == ETIMEDOUT) { /* timeout occurred */ } else { /* operate on x and y */ } pthread_mutex_unlock(&mut); LinuxThreads PTHREAD_COND(3)
All times are GMT -4. The time now is 05:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy