Sponsored Content
Top Forums Programming help me out with my threaded c++ mudbase - c++, pthread_cond_wait Post 302133748 by sonicx on Saturday 25th of August 2007 05:47:49 AM
Old 08-25-2007
thanks very much for your replies porter. i did not know about cleanup_push/pop yet, and after reading up a little on the functions i will surely try to integrate them into my source. i found some nice example code
here.

as for the signals - something i planned to use, quite in the manner you described, but did not know how yet. again, i did not know that sigwait function, thanks for that tip too - i shall integrate that into my main thread to catch signals send to the process there, and will try to put all normal threads into a blocking mode by setting up signal masks for them (got that right?).

what is named signals there in my code is actually more of a small event system to controll my thread wrapper class objects. other threads (the object which hold them) are supposed to use the signal function of the thread object to control (pause,go,kill) the thread pointed to by Thread->thread. i wanted to have a few threads which are there all the time, but only do work if i unpause them.
the input processing for example, the plan is to have my processing functions reside within a paused thread which i can unpause incase the socket listener receives some input on a clients socket. later i intend to add more parallelism by for example having the input processing thread spawn multiple workers which each process one clients input or a users command, with a queue and so on.

so to get back to the point, as you said
Quote:
(b) a thread cannot unblock itself, if it's it's blocked - its blocked, another thread has to send the pthead_cond_signal.
that would be the main thread in my case, which calls the signal function of the object which he also used to spawn that new thread.so the call to cond_signal would come from the main thread, but through the same object which "holds" the thread, thread id, associated mutex and condition and so on. but not from within the waiting thread itself - but within the to-be-controlled thread i do the cond_wait - thats right, isnt it?
i noticed not having something like
Code:
while(Thread->state == PAUSEME)
cond_wait(Thread->condition...)

which i will also give a try.

thanks again for your short but informative replies. hopefully ill be back to post a message of success.


---
nice pthreads tutorial for starters:
POSIX Threads Programming
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Threaded Discussions for Webpages

Dear All, I run a website for a non-profit. Does anyone know where I can get free or cheap software to run threaded discussions for our website? Our website is obviously running off a unix platform. Thanks (4 Replies)
Discussion started by: evertk
4 Replies

2. Programming

Threaded 'find' utility

I need to modify my version of find in unix and get it to create and use two POSIX threads to carry out concurrent finding operations. How do i get about doing this>? If anyone could help me it would be much appreciated. Thanx Mariuca (1 Reply)
Discussion started by: mariuca
1 Replies

3. Programming

threaded merge sort help

I am working on a merge sort of two files of integers, and am fuzzy on some of the logic\syntax. I need two threads, each of which will open a file, read its contents into an array, and then sort the array using qsort. One thread will operate on file f1.dat(10000 numbers) and leave its sorted... (0 Replies)
Discussion started by: AusTex
0 Replies

4. AIX

multi threaded program is hanging

I have a Multithreaded program which is hanging on AIX. OS Version: AIX 5.2 and thread library version : 5.2.0.75 We Initiate the process with 50 threads..when we are disconnecting from the process it hangs.There is lots of other stuff involved here.I am just sending the piece of the problem with... (0 Replies)
Discussion started by: hikrishn
0 Replies

5. Programming

Conditional wait using pthread_cond_wait() details

Please tell me about internal functionality of pthread_cond_wait(). How it works. Whether it actually put the thread into sleep and do the context switch or use spin locking. (1 Reply)
Discussion started by: mansoorulhaq
1 Replies

6. Shell Programming and Scripting

In need of multi threaded perl assistance

I need to write a perl script to execute external programs and grab the output and return code. Each program should be killed if it has not completed within X seconds. Imagine that the script goes something like this : @commands = &get_commands(); foreach $cmd (@commands) { $pid =... (4 Replies)
Discussion started by: SandmanCL
4 Replies

7. UNIX for Advanced & Expert Users

Multi-threaded encryption @ Fedora 11

Hello, are any of the encryption programs capable of true multi-threading ? Friend of mine tells me that he's been running some testing on Fedora 11 and that the kernel doesn't support multi-threading at that level. I've been looking into TrueCrypt, encfs and both calm to support... (0 Replies)
Discussion started by: TehOne
0 Replies

8. Linux

Multi-threaded encryption @ Fedora 11

Hello, are any of the encryption programs capable of true multi-threading ? Friend of mine tells me that he's been running some testing on Fedora 11 and that the kernel doesn't support multi-threading at that level. I've been looking into TrueCrypt, encfs and both calm to support... (1 Reply)
Discussion started by: TehOne
1 Replies

9. Programming

multi-threaded memory leak

Hello All : I write a .c program to test the exactually resource the memory leak as follows: 1 #include <stdio.h> 2 #define NUM 100000 3 void *Thread_Run(void * arg){ 4 //TODO 5 //pthread_datch(pthread_self()); 6 int socket= (int)arg; 7 ... (1 Reply)
Discussion started by: aobai
1 Replies

10. Shell Programming and Scripting

Need threaded python script

I have a single threaded bash script that I am using to create secgroup rules in openstack. The process to add the rules is taking forever. Any of you python gurus know how to convert this bash script into a thread python script? Thanks in advanced. create-secgroup-rules.sh: #!/bin/bash cat ... (2 Replies)
Discussion started by: stovie1000
2 Replies
pthread_cond_wait(3C)					   Standard C Library Functions 				     pthread_cond_wait(3C)

NAME
pthread_cond_wait, pthread_cond_timedwait, pthread_cond_reltimedwait_np - wait on a condition SYNOPSIS
cc -mt [ flag... ] file... -lpthread [ library... ] #include <pthread.h> int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex); int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime); int pthread_cond_reltimedwait_np(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *reltime); DESCRIPTION
The pthread_cond_wait(), pthread_cond_timedwait(), and pthread_cond_reltimedwait_np() functions are used to block on a condition variable. They are called with mutex locked by the calling thread or undefined behavior will result. These functions atomically release mutex and cause the calling thread to block on the condition variable cond. Atomically here means ``atomically with respect to access by another thread to the mutex and then the condition variable." That is, if another thread is able to acquire the mutex after the about-to-block thread has released it, then a subsequent call to pthread_cond_signal() or pthread_cond_broad- cast() in that thread behaves as if it were issued after the about-to-block thread has blocked. Upon successful return, the mutex has been locked and is owned by the calling thread. If mutex is a robust mutex where an owner terminated while holding the lock and the state is recoverable, the mutex is acquired even though the function returns an error value. When using condition variables there is always a boolean predicate, an invariant, associated with each condition wait that must be true before the thread should proceed. Spurious wakeups from the pthread_cond_wait(), pthread_cond_timedwait(), or pthread_cond_reltimed- wait_np() functions could occur. Since the return from pthread_cond_wait(), pthread_cond_timedwait(), or pthread_cond_reltimedwait_np() does not imply anything about the value of this predicate, the predicate should always be reevaluated. The order in which blocked threads are awakened by pthread_cond_signal() or pthread_cond_broadcast() is determined by the scheduling pol- icy. See pthreads(5). The effect of using more than one mutex for concurrent pthread_cond_wait(), pthread_cond_timedwait(), or pthread_cond_reltimedwait_np() operations on the same condition variable will result in undefined behavior. A condition wait (whether timed or not) is a cancellation point. When the cancelability enable state of a thread is set to PTHREAD_CAN- CEL_DEFERRED, a side effect of acting upon a cancellation request while in a condition wait is that the mutex is reacquired before calling the first cancellation cleanup handler. A thread that has been unblocked because it has been canceled while blocked in a call to pthread_cond_wait() or pthread_cond_timedwait() does not consume any condition signal that may be directed concurrently at the condition variable if there are other threads blocked on the condition variable. The pthread_cond_timedwait() function is the same as pthread_cond_wait() except that an error is returned if the absolute time specified by abstime passes (that is, system time equals or exceeds abstime) before the condition cond is signaled or broadcast, or if the absolute time specified by abstime has already been passed at the time of the call. The abstime argument is of type struct timespec, defined in time.h(3HEAD). When such time-outs occur, pthread_cond_timedwait() will nonetheless release and reacquire the mutex referenced by mutex. The function pthread_cond_timedwait() is also a cancellation point. The pthread_cond_reltimedwait_np() function is a non-standard extension provided by the Solaris version of POSIX threads as indicated by the ``_np'' (non-portable) suffix. The pthread_cond_reltimedwait_np() function is the same as pthread_cond_timedwait() except that the rel- time argument specifies a non-negative time relative to the current system time rather than an absolute time. The reltime argument is of type struct timespec, defined in time.h(3HEAD). An error value is returned if the relative time passes (that is, system time equals or exceeds the starting system time plus the relative time) before the condition cond is signaled or broadcast. When such timeouts occur, pthread_cond_reltimedwait_np() releases and reacquires the mutex referenced by mutex. The pthread_cond_reltimedwait_np() function is also a cancellation point. If a signal is delivered to a thread waiting for a condition variable, upon return from the signal handler the thread resumes waiting for the condition variable as if it was not interrupted, or it returns 0 due to spurious wakeup. RETURN VALUES
Except in the case of ETIMEDOUT, EOWNERDEAD, or ENOTRECOVERABLE, all of these error checks act as if they were performed immediately at the beginning of processing for the function and cause an error return, in effect, prior to modifying the state of the mutex specified by mutex or the condition variable specified by cond. Upon successful completion, 0 is returned. Otherwise, an error value is returned to indicate the error. ERRORS
These functions will fail if: EPERM The mutex type is PTHREAD_MUTEX_ERRORCHECK or the mutex is a robust mutex, and the current thread does not own the mutex. The pthread_cond_timedwait() function will fail if: ETIMEDOUT The absolute time specified by abstime to pthread_cond_timedwait() has passed. The pthread_cond_reltimedwait_np() function will fail if: EINVAL The value specified by reltime is invalid. ETIMEDOUT The relative time specified by reltime to pthread_cond_reltimedwait_np() has passed. These functions may fail if: EINVAL The value specified by cond, mutex, abstime, or reltime is invalid. EINVAL Different mutexes were supplied for concurrent operations on the same condition variable. If the mutex specified by mutex is a robust mutex (initialized with the robustness attribute PTHREAD_MUTEX_ROBUST), the pthread_cond_wait(), pthread_cond_timedwait(), and pthread_cond_reltimedwait_np() functions will, under the specified conditions, return the following error values. For complete information, see the pthread_mutex_lock(3C) and pthread_mutexattr_setrobust(3C) manual pages. EOWNERDEAD The last owner of this mutex died while holding the mutex, leaving the state it was protecting possibly inconsistent. The mutex is now owned by the caller. ENOTRECOVERABLE The mutex was protecting state that has now been left irrecoverable. The mutex has not been acquired. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
pthread_cond_signal(3C), pthread_cond_broadcast(3C), pthread_mutex_lock(3C), pthread_mutexattr_getrobust(3C), time.h(3HEAD), attributes(5), condition(5), pthreads(5), standards(5) SunOS 5.11 11 Nov 2008 pthread_cond_wait(3C)
All times are GMT -4. The time now is 03:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy