?
Solved

Semaphores, code question

Posted on 2004-10-13
4
Medium Priority
?
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
meidanze earned 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In a recent article here at Experts Exchange (http://www.experts-exchange.com/articles/18880/PaperPort-14-in-Windows-10-A-First-Look.html), I discussed my nine-month sandbox testing of the Windows 10 Technical Preview, specifically with respect to r…
#Citrix #POC #XenDesktop #vCenter #VMware #ESX
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Suggested Courses

764 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