Sponsored Content
Top Forums Programming How to sleep and wake a thread??? Post 302582512 by gabam on Friday 16th of December 2011 08:23:16 AM
Old 12-16-2011
How to sleep and wake a thread???

Hi guys,
I am creating two posix threads. I have some queries, hopefully you will help me out with them
1) How can I put a thread to indefinite sleep, for indefinite time period. I am familiar with this
Code:
sleep(5);

for 5 second, how can I make it indefinite??
2) How can one thread wake another sleeping thread
3) Can one thread force another thread to go to sleep

Here is the program which is really bothering me, the idea is to have two threads, one filling the stack, and one emptying it. But I can't have one of my thread to wake up the other, after the stack is empty or full
See it for yourself!

Code:
 
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#define MAX_SIZE 10
int Counter;
struct Stack
{
 int home[MAX_SIZE];
 int top;
};
struct Stack s;
void InitStack(struct Stack *s)
{
 s->top = -1;
 Counter = 0;
}
void Push(struct Stack *s, int x)
{
 if(s->top == MAX_SIZE-1)
 {
  printf("Stack is full!\n");
  exit(0);
 }
 else
 s->top++;
 s->home[s->top] = x;
 Counter++;
 printf("Counter = %d\n", Counter);
}
void Pop(struct Stack *s)
{
 int y;
 if(s->top == -1)
 {
  printf("Stack empty!\n");
  exit(0);
 }
 else
 y = s->home[s->top];
 s->top--;
 printf("%d poped\n",y);
 Counter--;
 printf("Counter = %d\n", Counter);
}

void *threadA(void *);
void *threadB(void *);
int main()
{
 InitStack(&s); 
 int i, j, k;
 pthread_t tid[2];
 pthread_attr_t attr[2];
 for(i=1;i<=2;i++)
 {
  pthread_attr_init(&attr[i-1]);
 }
  {
  pthread_create(&tid[0],&attr[0],threadA,(void *)786);
  pthread_create(&tid[1],&attr[1],threadB,(void *)786);
  }
 { 
 pthread_join(tid[0],NULL);
 pthread_join(tid[1],NULL);
 }
return 0;
}

void *threadA(void *n)
{
 int x;
 x = (int)n;
 while(1) 
 {
 if(Counter != MAX_SIZE)
 {
 Push(&s,x);
 printf("x = %d\n", x);
 sleep(1);
 }
 else
 {
 printf("Going for a long sleep as Stack is full!\n");
 sleep(50);
 }
 }
}
void *threadB(void *n)
{
 int x;
 sleep(15);
 while(1)
 {
 if(Counter != 0)
 {
 Pop(&s);
 sleep(1);
 }
 else
 {
 printf("Going for a long sleep as Stack is empty!\n");
 sleep(50);
 }
 }
}


If you can explain your points with a very small program, that would be so so nice of you.
Desperately waiting for your wonderful replies!!!!

Thanks alot in advance!

Last edited by gabam; 12-16-2011 at 10:18 AM..
 

7 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Wake on Lan script

Im old to Unix but new to scripting I have a MacBook running osx that I want to use as an nfs client. The server will be a linux box with a wake on lan card. Here's the idea. Run a cron command on the mac every minute that checks if I am on my home wireless network (the linux box is wired to... (0 Replies)
Discussion started by: anon0mus
0 Replies

2. Shell Programming and Scripting

Wake on LAN script

m old to Unix but new to scripting I have a MacBook running osx that I want to use as an nfs client. The server will be a linux box with a wake on lan card. Here's the idea. Run a cron command on the mac every minute that checks if I am on my home wireless network (the linux box is wired to... (6 Replies)
Discussion started by: anon0mus
6 Replies

3. Programming

how to wake up a thread that blocking at epoll_wait?

I have two threads: one maintains a thread-safe message queue (handle this queue at the beginning of every loop) and deals with tcp connections, the other one posts message to the former one. the problem is, while the former one was blocking at epoll_wait, it's not sure that how long until the... (0 Replies)
Discussion started by: cometeor
0 Replies

4. UNIX for Advanced & Expert Users

wake up user space thread from kernel space ISR

Hello, I'm searching for a proper way to let the kernel space ISR(implemented in a kernel module) wake up a user space thread on a hardware interrupt. Except for sending a real-time signal, is it possible to use a semaphore? I've searched it on google, but it seems impossible to share a... (0 Replies)
Discussion started by: aaronwong
0 Replies

5. Shell Programming and Scripting

Wrapping 'sleep' with my 'resleep' function (Resettable sleep)

This is a very crude attempt in Bash at something that I needed but didn't seem to find in the 'sleep' command. However, I would like to be able to do it without the need for the temp file. Please go easy on me if this is already possible in some other way: How many times have you used the... (5 Replies)
Discussion started by: deckard
5 Replies

6. UNIX for Dummies Questions & Answers

Linux Device Driver: how can an ISR wake up a user-thread?

Hi all, Is it possible to do the following in Linux (kernel 2.6.x): - A user-space thread goes to "sleep". Using any call/mechanism - On a hardware generated interrupt, the Interrupt handler (ISR) "wakes" the sleeping user-thread. I have seen wait_event() and wake_up() but it appears... (1 Reply)
Discussion started by: agaurav
1 Replies

7. UNIX for Dummies Questions & Answers

Computer wake commands

I'm a OS X user (MacBook Pro, OS X Lion) and I need it to wake up on Mondays, Wednesdays, Thursdays and Saturdays at 9:00 AM on the rest of the days of the week at 7:00 I issue the following commands: sudo pmset repeat wake MWRS 09:00:00 for the former sudo pmset repeat wake TFU... (1 Reply)
Discussion started by: scrutinizerix
1 Replies
thr_suspend(3C) 					   Standard C Library Functions 					   thr_suspend(3C)

NAME
thr_suspend, thr_continue - suspend or continue thread execution SYNOPSIS
cc -mt [ flag... ] file...[ library... ] #include <thread.h> int thr_suspend(thread_t target_thread); int thr_continue(thread_t target_thread); DESCRIPTION
The thr_suspend() function immediately suspends the execution of the thread specified by target_thread. On successful return from thr_sus- pend(), the suspended thread is no longer executing. Once a thread is suspended, subsequent calls to thr_suspend() have no effect. The thr_continue() function resumes the execution of a suspended thread. Once a suspended thread is continued, subsequent calls to thr_con- tinue() have no effect. A suspended thread will not be awakened by any mechanism other than a call to thr_continue(). Signals and the effect of calls tomu- tex_unlock(3C), rw_unlock(3C), sema_post(3C), cond_signal(3C), and cond_broadcast(3C) remain pending until the execution of the thread is resumed by thr_continue(). RETURN VALUES
If successful, the thr_suspend() and thr_continue() functions return 0. Otherwise, a non-zero value is returned to indicate the error. ERRORS
The thr_suspend() and thr_continue() functions will fail if: ESRCH The target_thread cannot be found in the current process. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
thr_create(3C), thr_join(3C), attributes(5), standards(5) WARNINGS
The thr_suspend() function is extremely difficult to use safely because it suspends the target thread with no concern for the target thread's state. The target thread could be holding locks, waiting for a lock, or waiting on a condition variable when it is unconditionally suspended. The thread will not run until thr_continue() is applied, regardless of any calls to mutex_unlock(), cond_signal(), or cond_broadcast() by other threads. Its existence on a sleep queue can interfere with the waking up of other threads that are on the same sleep queue. The thr_suspend() and thr_continue() functions should be avoided. Mechanisms that involve the cooperation of the targeted thread, such as mutex locks and condition variables, should be employed instead. SunOS 5.10 22 Mar 2002 thr_suspend(3C)
All times are GMT -4. The time now is 06:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy