Unix threads and Sleep as a block process

Hi everyone ... thanks for taking your time to look at this ...

I am trying to create a process that creates a needed number of threads per single proces. Also, within this main process I have a 2 global vars that are accessed within threads as well as main process. Those 2 vars I am controling using the POSIX semaphores. I am shure that those semaphores are working and positioned right.

My problem is that I have to use the sleep within my main process to let those threads kick in to read / change the global vars. Since sleep blocks the entire process my program kind of run VERY slow.

My main loop looks like

while(time)
{
   sem_wait(&currentime)
   currenttime++
   sem_post(&currentime)
   while(true)
  {
     create my thread etc etc
     sleep(1)
  }

}

I was wondering if anyone is out there that is aware of any system call that would not block the entire process that I could use instead of sleep ....

Again ... thanks a loot for looking at this.
L.
ledeniAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yuzhCommented:
I think you can create a milisecond timer to WAST miliseconds, and let the threads to kick off.

have a look at the following page, to see if it make sense:
http://oldlook.experts-exchange.com/Programming/Programming_Platforms/Linux_Programming/Q_20577083.html
0
yuzhCommented:
Hi ledeni,

    I think if you put (move/link) this question to UNIX programming, you might get better
response.
    http://oldlook.experts-exchange.com/Programming/Programming_Platforms/Unix_Programming/

    You can ask the CS (post a 0 point question in CS TA), and the Mods will do it for you:
    http://oldlook.experts-exchange.com/Community_Support/
   
0
sunnycoderCommented:
try pthread_yield()

man pthread_yield

I assumed that you are using the pthreads library
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

ChiefEngineerCommented:
I guess you need the threads to initialize in a certain order to set the global variables correctly and that a random order initialization is what you want to avoid.

If that's the case I suggest you to use another semaphore or mutex to synchronize the threads creation within the main loop (because in fact you have a kind of resource access coordination issue here) so you block your main loop until the new thread releas the lock and then you have no timer waste.

Just modify your code so it looks like this:


my thread ( )
{
   // Initialization part of the thread
    ...
    release mutex for initializing thread

    // Rest of the thread code
    ...
}

...

while(time)
{
   sem_wait(&currentime)
   currenttime++
   sem_post(&currentime)
 
  while(true)
  {
     lock mutex for initializing thread
     create my thread etc etc
     // I suggest to add a timer for this operation so you don't hang in case of problems
     // with thread's initialization
     wait mutex for initializing thread
  }
}

Hope it helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ledeniAuthor Commented:
Sorry I should have posted earlier ... but I got that solved with thr_yield() ... but certanly your effort will be awarded !!

Thanks for looking at this anyhow
0
sunnycoderCommented:
ledeni,

I had suggested using pthread_yield more than 2 weeks ago !!! I assumed that you were using pthreads which was wrong ... so you had to use thr_yield in place of pthread_yield ... I feel that you awarded the question wrongly
0
ledeniAuthor Commented:
Hey Pasha

fare is fare ... you can move or split those points between the both guys .. I appreciate the sunny's response though that it was not quite the right reposnse .. since I used the thr_yield() and I also appreciate the other guy response for giving in his time .. .I will leave it to you to distribute those points ...

thx
L .
0
ledeniAuthor Commented:
for a future reference ,let me know if there is anyhow possible for me to reasign points or split them my self bw different people .
thanks
L.
0
sunnycoderCommented:
>I appreciate the sunny's response though that it was not quite the right reposnse
My idea was correct .... making the threads yield
I had clearly mentioned in my post

>>try pthread_yield()

>>man pthread_yield

>>I assumed that you are using the pthreads library

you never returned clarifying that you were not using pthreads nor was any such thing specified in your original question !! All that was needed was using the corresponding thread yielding function of the library/pltform that you  were using.

>for a future reference ,let me know if there is anyhow possible for me to reasign points or split them my self bw different
>people .
you cannot reassign points ... for help with splitting,
http://www.experts-exchange.com/help/closing.jsp
0
YensidModCommented:
I split the points per asker request.

Yensidmod
EE Moderator
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.