Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pthread_mutex_t reading_mutex = PTHREAD_MUTEX_INITIALIZER;
- //############################################################
- int main() {
- alive_threads = number_of_threads;
- for (i = 0; i < thread_quantity; i++) {
- pthread_create(&thread_id[i], NULL, thread_function, NULL)
- created_threads++;
- }
- while (alive_threads) {
- usleep(1000);
- }
- }
- void clean(void *) {
- alive_threads--;
- }
- void *thread_function(void *data) {
- while (1) {
- pthread_cleanup_push(clean, NULL);
- pthread_mutex_lock(&reading_mutex)
- //some code...
- pthread_mutex_unlock(&reading_mutex);
- if (bytes_readed > 0) {
- //no, don't need that code
- pthread_cleanup_pop(1);
- pthread_cancel(pthread_self())
- return data;
- }
- /////////////////////////////////////####################### test.txt #################
- The pthread_cleanup_push() function pushes routine onto the top of the
- stack of clean-up handlers. When routine is later invoked, it will be
- given arg as its clean argument.
- The pthread_cleanup_pop() function removes the routine at the top of
- the stack of clean-up handlers, and optionally executes it if execute
- is nonzero.
- A cancellation clean-up handler is popped from the stack and executed
- in the following circumstances:
- 1. When a thread is canceled, all of the stacked clean-up handlers are
- popped and executed in the reverse of the order in which they were
- pushed onto the stack.
- 2. When a thread terminates by calling pthread_exit(3), all clean-up
- handlers are executed as described in the preceding point. (Clean-
- up handlers are not called if the thread terminates by performing a
- return from the thread start function.) clean
- 3. When a thread calls pthread_cleanup_pop() with a nonzero execute
- argument, the top-mst clean-up handler is popped and executed.
- POSIX.1 permits pthread_cleanup_push() and pthread_cleanup_pop() to be
- implemented as macros that expand to text containing '{' and '}',
- respectively. For this reason, the caller must ensure that call
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement