Solved

Semaphores, code question

Posted on 2004-10-13
4
318 Views
Last Modified: 2008-01-09
Can anyone show me where I would add semaphores to this to ensure it always will print ready, then set, then go?

#include <iostream.h>
#include <pthread.h>
using namespace std;

void *ready(void *u)
{
cout << "ready" << endl;
}

void *set(void *u)
{
cout << "set" << endl;
}
      
void *go(void *u)
{
cout << "go" << endl;
}    

main()
{
pthread_t tr, ts, tg;

pthread_create(&tr,NULL,ready,NULL);
pthread_create(&ts,NULL,set,NULL);
pthread_create(&tg,NULL,go,NULL);

pthread_join(tr,NULL);
pthread_join(ts,NULL);
pthread_join(tg,NULL);
}
0
Comment
Question by:killer455
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
meidanze earned 75 total points
ID: 12308925
Here are my comments,
Sorry i did not write you the correct syntax of the command but i do'nt have infront of me
a unix machine to check it.

#include <iostream.h>
#include <pthread.h>
using namespace std;

void *ready(void *u)
{
//wait to semaphore to become 3 , run first (using semop)
cout << "ready" << endl;
//decrease semaphore in 1 (using semop)
}

void *set(void *u)
{
//wait to semaphore to become 2,, run second (using semop)
cout << "set" << endl;
//decrease semaphore in 1 (using semop)
}
     
void *go(void *u)
{
//wait to semaphore to become 1, run first (using semop)
cout << "go" << endl;
}    

main()
{


pthread_t tr, ts, tg;

// Create the semaphore  (using semget)
//Init the semaphore to 3(using semctrl)
pthread_create(&tr,NULL,ready,NULL);
pthread_create(&ts,NULL,set,NULL);
pthread_create(&tg,NULL,go,NULL);

pthread_join(tr,NULL);
pthread_join(ts,NULL);
pthread_join(tg,NULL);

//Delete the semaphore (using semctrl)
}
0
 
LVL 1

Expert Comment

by:jaguarul
ID: 12311728
I don't know the exact type signature of your functions, but are you sure they should return a void pointer? (you have void* in front of each function). Then add a "return NULL;" in each of your functions, otherwise you are in for some stack trouble ;)
0
 
LVL 1

Expert Comment

by:meidanze
ID: 12317534
The signature of the function MUST be void * because the signature of pthread_create
include the void *function(void  *0) argument.
no need to return NULL.
0
 
LVL 1

Expert Comment

by:jaguarul
ID: 12324277
You may be right, but then the function signature is strange: void * f() means that f returns a void pointer, right? So in principle, you should respect what you declare. In practice, it may work on some architectures and compilers, because in this case the return value might be stored in a register instead of the stack, so no problem arises if you don't set that return value. At least in one example I saw on the internet, the thread function returned something (actually NULL). I'm not saying it will not work in some cases...

In addition, void *function(void  *0)  means a pointer to a function that returns void (not void *) and takes a void pointer as parameter.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vmware  OVA template question 33 112
SymUIAgent.app persists asking to restart - ?? 4 182
Window 7 local DNS 9 87
Windows pro to home 2 81
Sometimes a user will call me frantically, explaining that something has gone wrong and they have tried everything (read - they have messed it up more and now need someone to clean up) and it still does no good, can I help them?!  Usually the standa…
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
A short film showing how OnPage and Connectwise integration works.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now