Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Mutexes  (Read 6406 times)

0 Members and 1 Guest are viewing this topic.

henk23

  • Newbie
  • *
  • Posts: 1
    • View Profile
Mutexes
« on: July 07, 2009, 12:16:03 am »
Hi!

Is it possible to use the mutexes like this:

Code: [Select]

void thread_1()
{
  while(1)
  {
    wait_for_important_data();
    process_important_data();
  }
}

void thread_2()
{
  while(1)
  {
    provide_important_data();
    sleep(0.07 seconds);
}


the provided data is a picture from my webcam at about 15 frames per second.

the process is displaying the picture as an openGL-texture.

it's part of a big project, so there will be no other way of doing it.

i have tried it like this but it doesn't work:

Code: [Select]

void thread();
{
  while(1)
  {
    provide_data();
    mutex.unlock();
  }
}

void main();
{
  thread.launch();
  while(1)
  {
    mutex.lock();
    use_data();
  }
}


what can i do? :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Mutexes
« Reply #1 on: July 07, 2009, 07:50:51 am »
You can't lock the mutex in a thread and unlock it in another one. I think you should read more about mutexes, you don't seem to understand how they work ;)

To give you an answer, a correct version would look like this:
Code: [Select]
void thread();
{
  while(1)
  {
    mutex.lock();
    provide_data();
    mutex.unlock();
  }
}

void main();
{
  thread.launch();
  while(1)
  {
    mutex.lock();
    use_data();
    mutex.unlock();
  }
}

But then your threads will never run in parallel, because when one is busy the other will always be waiting. You'll have to use the mutex more locally.

We won't be able to help you more unless you show more code.
Laurent Gomila - SFML developer

 

anything