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

Author Topic: Thread Priority  (Read 8709 times)

0 Members and 1 Guest are viewing this topic.

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Thread Priority
« on: October 04, 2010, 01:17:58 am »
Hello, once again.
Sorry for creating so many threads in a short amount of time but since I'm on a learning phase, I really want  to solve my questions.

Is there a way to change the priority of a thread?
I noticed all the threads I create are being executed with "Lowest" priority.


On the image, the worker thread plays a music, but since it's running on lowest priority, whenever I do something outside of the application (Open browser, open a folder, open a winrar archive, etc...) the music starts slowing down and skipping.

Is there a way to change this? Or should I stop using SFML thread classes?
Thanks in advance.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Thread Priority
« Reply #1 on: October 04, 2010, 08:29:10 am »
SFML only provides basic threading features, if you need more you should use another, more specific threading library.
Laurent Gomila - SFML developer

Silvah

  • Guest
Thread Priority
« Reply #2 on: October 04, 2010, 08:43:52 am »
Quote from: "Laurent"
SFML only provides basic threading features, if you need more you should use another, more specific threading library.
Changing the thread priority is a basic feature, and as you see, it's pretty useful in a multimedia library, which SFML claims to be.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Thread Priority
« Reply #3 on: October 04, 2010, 09:31:00 am »
SFML is not a threading API, it only provides what it needs internally. If you need threading features please use a threading library, there are tons available.
Laurent Gomila - SFML developer

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Thread Priority
« Reply #4 on: October 04, 2010, 04:15:51 pm »
Ah I see. I understand.
Is there any library for threading that you recommend? Just wondering because asking can save hours of research.

Thanks for your help  :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Thread Priority
« Reply #5 on: October 04, 2010, 04:20:49 pm »
boost.thread is going to be part of the next C++ standard, so I guess this is the best choice. Your compiler may even already implement it as std::thread.
Laurent Gomila - SFML developer

Silvah

  • Guest
Thread Priority
« Reply #6 on: October 05, 2010, 09:19:53 am »
Quote from: "Laurent"
SFML is not a threading API, it only provides what it needs internally.
So, if it's really intended to be used solely by the library itself, it shouldn't be exposed to the user - all in all, you don't put the internals of your classes in the public section, do you?

Otherwise, if it's really meant to be available to the user, why you're refusing to implement this feature, even though it's literally several lines of code and some users apparently need it, as you see?

Yeah, SFML is not a threading library. And I don't even want it to be. It's a multimedia library, and therefore, if it provides threading support, it'd be nice if it let the user set the priority of the thread, because there are circumstances when one want to run threads with different than default priority in this kind of applications.

About boost.thread... Quite frankly, I think that many developers don't want to depend on boost. It's simply a big mess. Sure, there are other libraries, but still - if SFML let me to change that priority, I wouldn't have to depend upon other stuff (yay, smaller size of my proggy!).

Canadadry

  • Hero Member
  • *****
  • Posts: 1081
    • View Profile
Thread Priority
« Reply #7 on: October 05, 2010, 09:39:55 am »
Quote from: "Silvah"
Changing the thread priority is a basic feature, and as you see, it's pretty useful in a multimedia library, which SFML claims to be.


Sorry, but I disagree. This is an advance feature. In my opinion the basic is to provide thread with start and stop function. Only the people who want to manage finely their threads, want a feature like that, the rest of us just want to launch a thread function.

Look at the clock class. There is no pause function which seems to be a basic function. I guess, this function is absent for the same reason and I approve it that's why I found SFML really simple to use : few function but the most important.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Thread Priority
« Reply #8 on: October 05, 2010, 09:59:18 am »
Quote
So, if it's really intended to be used solely by the library itself, it shouldn't be exposed to the user - all in all, you don't put the internals of your classes in the public section, do you?

Otherwise, if it's really meant to be available to the user, why you're refusing to implement this feature, even though it's literally several lines of code and some users apparently need it, as you see?

I wish I could get rid of all the stuff in sfml-system -- thread, clock, etc. This has nothing to do with multimedia programming, but unfortunately it's one of the things that you can't avoid, you always need these basic features in this kind of apps.
That's why I provide minimal support for it, but nothing more: SFML should stay focused on multimedia, and not start to become a bloated general purpose system library.
Like I said, I prefer people to use a dedicated library when they have more specific needs, I don't want to reinvent the wheel here.

Quote
About boost.thread... Quite frankly, I think that many developers don't want to depend on boost. It's simply a big mess

boost.thread is the next C++ standard, and is already implemented in recent versions of the compilers. I won't fight against that and provide my own threading library.
Laurent Gomila - SFML developer

Silvah

  • Guest
Thread Priority
« Reply #9 on: October 05, 2010, 10:18:16 am »
Quote from: "Laurent"
boost.thread is the next C++ standard, and is already implemented in recent versions of the compilers. I won't fight against that and provide my own threading library.
Not exactly true - surely standard library won't bother with portability, as it's often tied to the specific compiler and operating system. Since boost.thread bothers, it depends upon other parts of boost, what makes it just plain bloated. I do care about compile times, what about you?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Thread Priority
« Reply #10 on: October 05, 2010, 10:33:57 am »
Yeah, I was just talking about the public API, not the private details. Of course using the standard library will be more convenient, as you will have no external dependency. But I don't think that boost.thread depends on many other parts, maybe just some generic configuration headers.

Regarding the compile times, I don't think there's a huge difference, the things that really take a noticeable time to compile are the metaprogramming-based stuff ; boost.thread is not concerned.
Laurent Gomila - SFML developer

Spellbreaker

  • Newbie
  • *
  • Posts: 33
    • ICQ Messenger - 287156171
    • MSN Messenger - spellbreaker@live.de
    • Yahoo Instant Messenger - Spellbreaker1979
    • View Profile
    • http://www.apeironstudios.com
Thread Priority
« Reply #11 on: October 05, 2010, 11:21:16 am »
I'm using boost.thread myself for advanced stuff, it really doesn't matter in compile-time. You can believe me :)

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Thread Priority
« Reply #12 on: October 06, 2010, 05:20:59 am »
Boost.Thread solved my problem as it sets the thread priority to normal by default.

But, imagine I wanted to change it to "High" priority. Not that I'm going to do so, but I'm just curious. I've been reading boost.thread documentation but couldn't find a way to do so, so I did a bit of research and found the function "SetThreadPriority()" but I'm not sure how to use it.

I've tried this:
Code: [Select]
[...]
    thread th1(threadfunction1,n);
    SetThreadPriority(th1, HIGH_PRIORITY_CLASS);
[...]

But I get the error: 'SetThreadPriority' : cannot convert parameter 1 from 'boost::thread' to 'HANDLE'.

What am I doing wrong? Should I be doing this, even? Or is there another way to change the priority of a boost.thread rather than using this function?

I know this isn't related to the topic or to SFML but I just wanted to solve this.

Thanks to everyone for your help


Quote from: "Laurent"

I wish I could get rid of all the stuff in sfml-system -- thread, clock, etc. This has nothing to do with multimedia programming, but unfortunately it's one of the things that you can't avoid, you always need these basic features in this kind of apps.

I found those tools to be very useful, makes the librabry a all in one and it definitely helps those who're just starting, like me hehe.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Thread Priority
« Reply #13 on: October 06, 2010, 08:09:41 am »
SetThreadPriority is a function of the low-level Windows API, it can't be used directly with a boost thread (but you can pass thread.native_handle() or something like this).

Apparently there's no function in the boost.thread API to change a thread's priority, you will have to use the specific OS functions like SetThreadPriority. Anyway, Google gives you more links than you need to solve this problem.

By the way, how did you create your SFML thread? Its priority shouldn't be lowest, it should be normal.
Laurent Gomila - SFML developer

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Thread Priority
« Reply #14 on: October 06, 2010, 02:59:44 pm »
I did it like this:
Code: [Select]

using namespace sf;
using namespace std;

void thread_tester(void *UserData)
{
cout << "Thread 1 is here." << endl;
/*
Generate random numbers
*/
}


int main()
{
Thread th1(&thread_tester);
th1.Launch();
cout << "Main is here." << endl;
return 0;
}

Used MS Visual Studio 2008 to compile