• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

Pthread question

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Hi, I'm working on an assignment for my Operating Systems class in which we are to implement a solution to the Producer-Consumer Problem using pthreads. I am not asking for ways to solve this, rather I have a question about the pthreads & semaphores themselves. We are being asked to utilize binary semaphores as well as counting ones.

I understand why we need the counting semaphores, but why do we need to use binary ones when the pthreads are already going to be requesting mutex locks using the function pthread_mutex_lock()? It seems almost as if the request for the semaphore (sem_wait()) is superfluous. Perhaps I am not understanding the difference between mutex locks. Can someone help clarify?

Thanks in advance.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
pthread_mutex_lock() is a low-level atomic operation on a lock variable. sem_wait() explicitly blocks the thread until the semaphore is available. Use pthread_mutex_lock() for short critical sections. Use sem_wait() to guard long-latency events.
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Originally posted by: degibson
pthread_mutex_lock() is a low-level atomic operation on a lock variable. sem_wait() explicitly blocks the thread until the semaphore is available. Use pthread_mutex_lock() for short critical sections. Use sem_wait() to guard long-latency events.

Thanks for the reply. As I re-read the requirements it turns out we're aren't using the binary semaphore in addition to the pthread function. Don't I feel silly? :)
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: EvilManagedCare
Thanks for the reply. As I re-read the requirements it turns out we're aren't using the binary semaphore in addition to the pthread function. Don't I feel silly? :)

If you don't fully grasp this stuff right away, do not despair. Parallel and/or concurrent code is basically some of the hardest code you will ever have to write, and very few people are ever good at it.
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Originally posted by: degibson

If you don't fully grasp this stuff right away, do not despair. Parallel and/or concurrent code is basically some of the hardest code you will ever have to write, and very few people are ever good at it.

That's very nice to hear because I was, in fact, getting to the point of severe discouragement. This is the first C code I have produced after having some C++ a couple years back. On top of that I had to get used to using Linux and finding a nice IDE (which ended up being Code::Blocks) to use as I could not take any more command line gcc compiling. It seems getting the tools together and learning how to use them has been more time consuming than the actual project. Needless to say, after having come from all Windows and my familiarity with Java not C/C++, it has been a trip. But the up side is I no longer fear C++ the way I used to after having been confronted with it (in vivo desensitization ftw :) ).
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
I do not understand the obsession with IDEs. gvim+gdb/ddd+make has every feature I would want in an IDE.
 

iCyborg

Golden Member
Aug 8, 2008
1,353
62
91
Does it have intellisense? Can it show caller/callee graphs?
Stuff like these is the main reason I prefer Win devt with VS over Linux, especially if you're working on a big chunk of code, most of which you didn't write yourself.
 

Cogman

Lifer
Sep 19, 2000
10,286
145
106
Originally posted by: iCyborg
Does it have intellisense? Can it show caller/callee graphs?
Stuff like these is the main reason I prefer Win devt with VS over Linux, especially if you're working on a big chunk of code, most of which you didn't write yourself.

Anymore, most IDEs have some form of intellisense. And DDD has WAY more debugging features then I have seen available in MS Visual Studios. (GDB is the bomb when it comes to debugging). Profiling your code is definitely possible in many other IDE's as well. Codeblocks for one supports this.
 

iCyborg

Golden Member
Aug 8, 2008
1,353
62
91
The ones I worked with didn't move much from syntax highlighting, but truth be told, it's been a few years, and I didn't look much.
I agree that GDB rules and I like it a lot more than VS, although I've never had a situation where I found VS debugger inadequate. Never used ddd...
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: nkgreen
Are you in my CS 426 class?!! :Q

No.

Originally posted by: iCyborg
Does it have intellisense?

I had to google 'intellisense' to translate out of Microsoft-ese. Yes, vim has context-sensitive auto-completion.

Can it show caller/callee graphs?
Ah, I forgot to include doxygen in my tool set.

Stuff like these is the main reason I prefer Win devt with VS over Linux, especially if you're working on a big chunk of code, most of which you didn't write yourself.

Different strokes for different folks. In my case, I just get irritated with VS's flowery nature whenever I fire it up for some little project on Windows. I inevitably end up writing code in vim and just using VS for autodep/make.