Sponsored Content
Homework and Emergencies Homework & Coursework Questions Bounded Buffer is hanging, tried all I can. (C++) Post 302733461 by jim mcnamara on Tuesday 20th of November 2012 02:06:28 PM
Old 11-20-2012
Quote:
Although, I'm still not clear why this should work if I have more than 5 threads of each going. That would leave the extras in limbo or something?)
The simplest model is with a mutex - like pthread_mutex_t
pseudo code
Code:
// DO NOT dawdle here just read and get the heck out of the mutex
trylock - if busy do something else or loop back so get back into the wait queue
// now you have the mutex
rc= read ()
unlock mutex
// now you do not have the mutex
check return code for < 0 handle error
return number of bytes read or recv() 'd

Do the exact same thing with write or send. Only keep the mutex during the read or write. Do nothing else when you own the mutex.

This model is fine for the situation where you have some processing to do with data, and you read/write and process using threads. I/O is always slower than other operations.

If you have almost no processing to do then threads are not going to gain you much if anything. Your app will always be I/O bound whether it is threaded or not. The idea with threads is to do data crunching or whatever in parallel. When parallelism gains you nothing, skip threading. So if you are misusing threading, then other threads are in fact superfluous.

To avoid hanging on pthread_join, make the threads not joinable, when they return or call
Code:
pthread_exit(NULL)

they simply go away.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Hanging commands

Hello, I just tried to run this command: /usr/lib/sendmail -d0.1 -bt < /dev/null | grep -i version Its doing what I want: writing out the sendmail version. But on some machines it writing the version and then exit to the prompt but on others its writing the version but then hangs, I need to... (0 Replies)
Discussion started by: jOOc
0 Replies

2. Solaris

hanging sockets!??!

hi all, i had a program which created a socket on port 7113, but for some reason the program was hunbg and I had to Ctrl+c it. I ran the program for a couple of times and now when I do a netstat -a I see that these sockets are lingering and that might be the reason why my program is not... (1 Reply)
Discussion started by: Naanu
1 Replies

3. UNIX for Dummies Questions & Answers

Hanging port?

Ok, this question my be different. I can ping our unix box, but when we I to access the webpage I cant. To access the webpage I type http://ipaddress:some port. How do I check if a port is hanging and how would I un hang it. Sorry if question doesnt make sense. (1 Reply)
Discussion started by: NycUnxer
1 Replies

4. Shell Programming and Scripting

Script is hanging

Hello, I have the following shell script and when i execute, it keeps hanging and nothing happens Please let me know. Requirement is to read data from file and pass it to the sql and create files as shown. code /******** #!/bin/sh while read user.dat do echo "user = $1 email =... (1 Reply)
Discussion started by: rakeshsr12
1 Replies

5. Programming

bounded buffer implementation

Hi Experts, I have a programming assignment that asks us to implement a pipegrep function. it basically has 5 stages and each stage has a thread and buffers are used between stages. am currently implementing stage 1 . In stage 1 am suppose to read directory and store the filenames in buffer1... (15 Replies)
Discussion started by: amejoish
15 Replies

6. Solaris

df command hanging

Hi Folks, When i execute the command df -kh in my system the o/p hangs.. The command runs fine but takes a lot of time before coming back to the # prompt. Can anyone please suggest the possible cause and solution?. (10 Replies)
Discussion started by: vivek.goel.piet
10 Replies

7. Shell Programming and Scripting

nslookup hanging

Hey folks. Long time lurker, first time poster. I'm a bit of a newbie at "coding" (obviously, scripting is a teensy bit different than coding) and I've run into a problem that I just can't seem to get around. I'm going through a list of servers to check their name, IP, reverse-NSLOOKUP name and... (2 Replies)
Discussion started by: Bearwhale
2 Replies

8. Shell Programming and Scripting

sed hanging

/bin/sed -n '$q;5633653,0 p' lfile lfile is a log file that is being updated several times a second. so, the command above figures out the line number it wants in the lfile and then proceeds to output all lines from that line number to the end of the file. the problem is, the end of the... (2 Replies)
Discussion started by: SkySmart
2 Replies

9. Shell Programming and Scripting

Sftp hanging

Hi All, I am transfering a file through sftp. But the script is hanging at exit occasionally. I am suspecting that sftp is hanging due to buffer size issue. If that is the case can any body suggest a solution. Please find the code. echo "cd /${CUST_ID}/inbound/${SAFET_ID}" >... (0 Replies)
Discussion started by: Girish19
0 Replies

10. Shell Programming and Scripting

XML text bounded with tag

Could you please give your inputs on the below issue: source.xml <?xml version="1.0" encoding="UTF-16"?> <P1 > <C1 type="i"><2></C1> <V1 type="string"><6.2></V1> <D1 type="string"> <D2><1.0></D2> <D2><2.0></D2> </D1> ...................... ...................... many more... (7 Replies)
Discussion started by: unme
7 Replies
pthread_mutex_lock(3T)													    pthread_mutex_lock(3T)

NAME
pthread_mutex_lock(), pthread_mutex_trylock() - lock or attempt to lock a mutex SYNOPSIS
PARAMETERS
mutex Pointer to the mutex to be locked. DESCRIPTION
The mutex object mutex is locked by calling the function. How the calling thread acquires the mutex is dependent upon the type attribute for the mutex. This operation returns with the mutex object referenced by mutex in the locked state with the calling thread as its owner. If the mutex type is deadlock detection is not provided. Attempting to relock the mutex causes deadlock. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, undefined behavior results. If the mutex type is the mutex maintains the concept of an owner. If a thread attempts to relock a mutex that it has already locked, an error shall be returned. If a thread attempts to unlock a mutex that it has not locked or a mutex that is unlocked, an error shall be returned. If the mutex type is then the mutex maintains the concept of an owner and a lock count. When a thread successfully acquires a mutex for the first time, the count field shall be set to one. Every time a thread relocks this mutex, the count field shall be incremented by one. Each time the thread unlocks the mutex, the count field shall be decremented by one. When the count field reaches zero, the mutex shall become available for other threads to acquire. If a thread attempts to unlock a mutex that it has not locked, an error shall be returned. If the mutex type is deadlock detection is not provided. Attempting to relock the mutex causes deadlock. A thread can unlock a mutex that it has not locked. Attempting to unlock an unlocked mutex results in undefined behavior. If the mutex type is attempting to recursively lock the mutex results in undefined behavior. Attempting to unlock the mutex if it was not locked by the calling thread results in undefined behavior. Attempting to unlocked the mutex if it is not locked results in undefined behavior. The function is identical to the function except that if the mutex object referenced by mutex cannot be acquired after one attempt, the call returns immediately with an error. If a signal is delivered to a thread waiting for a mutex, upon return from the signal handler, the thread shall resume waiting for the mutex as if it was not interrupted. RETURN VALUE
Upon successful completion, and return zero. Otherwise, an error number is returned to indicate the error (the variable is not set). ERRORS
If any of the following occur, the and functions return the corresponding error number: [EINVAL] mutex is a mutex and the caller's priority is higher than mutex's priority ceiling. If any of the following occur, the function returns the corresponding error number: [EBUSY] mutex is currently locked by another thread. If any of the following occur, the and functions return the corresponding error number: [EAGAIN] mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded. This error is not detected on HP-UX. [EINVAL] mutex is not an initialized mutex. [EFAULT] mutex parameter points to an illegal address. If any of the following occur, the function returns the corresponding error number: [EDEADLK] The current thread already owns the mutex. This error will be detected only for mutexes on HP-UX. WARNINGS
A recursive mutex can be locked more than once by the same thread without causing that thread to deadlock. Undefined behavior may result if the owner of a recursive mutex tries to lock the mutex too many times. AUTHOR
and was derived from the IEEE POSIX P1003.1c standard and X/Open SEE ALSO
pthread_mutex_init(3T), pthread_mutex_destroy(3T), pthread_mutex_unlock(3T). STANDARDS CONFORMANCE
Pthread Library pthread_mutex_lock(3T)
All times are GMT -4. The time now is 01:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy