public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
* how to get thread priorities to work
@ 2011-05-02 21:42 Burkhardt, Glenn
  2011-05-02 21:51 ` Lubashev, Igor
  0 siblings, 1 reply; 5+ messages in thread
From: Burkhardt, Glenn @ 2011-05-02 21:42 UTC (permalink / raw)
  To: pthreads-win32

I've been puzzling over this for way too long now.  I can't seem to get
thread priorities to work.  Any help will be appreciated.  

For the code that follows, I would expect that only one of the threads
would run, and the counter for the lower priority thread would never
increment.  But that's not the case, on both a WinXP and Win7 system
(built with MinGW 10/30/2010, gcc 4.5.2).  The counter values for the
two threads are close to being the same (e.g., 94686137, 99999999).

TIA...

#include <math.h>
#include <windows.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>

void sleep10(int *cnt) {
    double x;
    for (int i=0; i < 100000000; i++) {
        x = sin(1.0/(1.0+i));
        *cnt = i;
    }
}

void printError() {
    char *msg;
    int errorCode = GetLastError();
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
                  | FORMAT_MESSAGE_FROM_SYSTEM
                  | FORMAT_MESSAGE_IGNORE_INSERTS,
                  0, errorCode, 0, (LPTSTR)&msg, 0, 0);
    printf("error=%d: %s\n", errorCode, msg);
    LocalFree(msg);
}

void *thread(void *count) {
 
    printf("%p: %d\n", count, GetThreadPriority(GetCurrentThread()));

    sleep10(count);
 
    printf("%p exit\n", count);
    return 0;
}

int main() {
    struct sched_param spMax, spMin;
    pthread_attr_t attrMax, attrMin;
    pthread_t tMax, tMin;
    int minCount, maxCount;

    spMax.sched_priority = sched_get_priority_max(SCHED_OTHER);
    spMin.sched_priority = sched_get_priority_min(SCHED_OTHER);

    pthread_attr_init(&attrMax);
    pthread_attr_setschedparam(&attrMax, &spMax);
    pthread_attr_init(&attrMin);
    pthread_attr_setschedparam(&attrMin, &spMin);

    pthread_create(&tMax, &attrMax, thread, &maxCount);
    pthread_create(&tMin, &attrMin, thread, &minCount);

    printf("join %d\n", GetThreadPriority(GetCurrentThread()));
    pthread_join(tMax, 0);

    printf("%p: %d, %p: %d\n", &minCount, minCount, &maxCount,
maxCount);

    return 0;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: how to get thread priorities to work
  2011-05-02 21:42 how to get thread priorities to work Burkhardt, Glenn
@ 2011-05-02 21:51 ` Lubashev, Igor
  2011-05-02 22:00   ` Burkhardt, Glenn
  0 siblings, 1 reply; 5+ messages in thread
From: Lubashev, Igor @ 2011-05-02 21:51 UTC (permalink / raw)
  To: Burkhardt, Glenn, pthreads-win32

Without looking too much into this, are you trying this on a multi-core machine (either multiple cores or one hyperthreading core)?  

- Igor



-----Original Message-----
From: pthreads-win32-owner@sourceware.org [mailto:pthreads-win32-owner@sourceware.org] On Behalf Of Burkhardt, Glenn
Sent: Monday, May 02, 2011 5:43 PM
To: pthreads-win32@sourceware.org
Subject: how to get thread priorities to work

I've been puzzling over this for way too long now.  I can't seem to get
thread priorities to work.  Any help will be appreciated.  

For the code that follows, I would expect that only one of the threads
would run, and the counter for the lower priority thread would never
increment.  But that's not the case, on both a WinXP and Win7 system
(built with MinGW 10/30/2010, gcc 4.5.2).  The counter values for the
two threads are close to being the same (e.g., 94686137, 99999999).

TIA...

#include <math.h>
#include <windows.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>

void sleep10(int *cnt) {
    double x;
    for (int i=0; i < 100000000; i++) {
        x = sin(1.0/(1.0+i));
        *cnt = i;
    }
}

void printError() {
    char *msg;
    int errorCode = GetLastError();
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
                  | FORMAT_MESSAGE_FROM_SYSTEM
                  | FORMAT_MESSAGE_IGNORE_INSERTS,
                  0, errorCode, 0, (LPTSTR)&msg, 0, 0);
    printf("error=%d: %s\n", errorCode, msg);
    LocalFree(msg);
}

void *thread(void *count) {
 
    printf("%p: %d\n", count, GetThreadPriority(GetCurrentThread()));

    sleep10(count);
 
    printf("%p exit\n", count);
    return 0;
}

int main() {
    struct sched_param spMax, spMin;
    pthread_attr_t attrMax, attrMin;
    pthread_t tMax, tMin;
    int minCount, maxCount;

    spMax.sched_priority = sched_get_priority_max(SCHED_OTHER);
    spMin.sched_priority = sched_get_priority_min(SCHED_OTHER);

    pthread_attr_init(&attrMax);
    pthread_attr_setschedparam(&attrMax, &spMax);
    pthread_attr_init(&attrMin);
    pthread_attr_setschedparam(&attrMin, &spMin);

    pthread_create(&tMax, &attrMax, thread, &maxCount);
    pthread_create(&tMin, &attrMin, thread, &minCount);

    printf("join %d\n", GetThreadPriority(GetCurrentThread()));
    pthread_join(tMax, 0);

    printf("%p: %d, %p: %d\n", &minCount, minCount, &maxCount,
maxCount);

    return 0;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: how to get thread priorities to work
  2011-05-02 21:51 ` Lubashev, Igor
@ 2011-05-02 22:00   ` Burkhardt, Glenn
  2011-05-02 22:02     ` Lubashev, Igor
  0 siblings, 1 reply; 5+ messages in thread
From: Burkhardt, Glenn @ 2011-05-02 22:00 UTC (permalink / raw)
  To: Lubashev, Igor, pthreads-win32

Dual core processor. 

-----Original Message-----
From: Lubashev, Igor [mailto:ilubashe@akamai.com] 
Sent: Monday, May 02, 2011 5:51 PM
To: Burkhardt, Glenn; pthreads-win32@sourceware.org
Subject: RE: how to get thread priorities to work

Without looking too much into this, are you trying this on a multi-core
machine (either multiple cores or one hyperthreading core)?  

- Igor



-----Original Message-----
From: pthreads-win32-owner@sourceware.org
[mailto:pthreads-win32-owner@sourceware.org] On Behalf Of Burkhardt,
Glenn
Sent: Monday, May 02, 2011 5:43 PM
To: pthreads-win32@sourceware.org
Subject: how to get thread priorities to work

I've been puzzling over this for way too long now.  I can't seem to get
thread priorities to work.  Any help will be appreciated.  

For the code that follows, I would expect that only one of the threads
would run, and the counter for the lower priority thread would never
increment.  But that's not the case, on both a WinXP and Win7 system
(built with MinGW 10/30/2010, gcc 4.5.2).  The counter values for the
two threads are close to being the same (e.g., 94686137, 99999999).

TIA...

#include <math.h>
#include <windows.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>

void sleep10(int *cnt) {
    double x;
    for (int i=0; i < 100000000; i++) {
        x = sin(1.0/(1.0+i));
        *cnt = i;
    }
}

void printError() {
    char *msg;
    int errorCode = GetLastError();
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
                  | FORMAT_MESSAGE_FROM_SYSTEM
                  | FORMAT_MESSAGE_IGNORE_INSERTS,
                  0, errorCode, 0, (LPTSTR)&msg, 0, 0);
    printf("error=%d: %s\n", errorCode, msg);
    LocalFree(msg);
}

void *thread(void *count) {
 
    printf("%p: %d\n", count, GetThreadPriority(GetCurrentThread()));

    sleep10(count);
 
    printf("%p exit\n", count);
    return 0;
}

int main() {
    struct sched_param spMax, spMin;
    pthread_attr_t attrMax, attrMin;
    pthread_t tMax, tMin;
    int minCount, maxCount;

    spMax.sched_priority = sched_get_priority_max(SCHED_OTHER);
    spMin.sched_priority = sched_get_priority_min(SCHED_OTHER);

    pthread_attr_init(&attrMax);
    pthread_attr_setschedparam(&attrMax, &spMax);
    pthread_attr_init(&attrMin);
    pthread_attr_setschedparam(&attrMin, &spMin);

    pthread_create(&tMax, &attrMax, thread, &maxCount);
    pthread_create(&tMin, &attrMin, thread, &minCount);

    printf("join %d\n", GetThreadPriority(GetCurrentThread()));
    pthread_join(tMax, 0);

    printf("%p: %d, %p: %d\n", &minCount, minCount, &maxCount,
maxCount);

    return 0;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: how to get thread priorities to work
  2011-05-02 22:00   ` Burkhardt, Glenn
@ 2011-05-02 22:02     ` Lubashev, Igor
  2011-05-02 22:05       ` Burkhardt, Glenn
  0 siblings, 1 reply; 5+ messages in thread
From: Lubashev, Igor @ 2011-05-02 22:02 UTC (permalink / raw)
  To: Burkhardt, Glenn, pthreads-win32

Hope this answers the question of why the two threads are running at about the same speed.

-----Original Message-----
From: Burkhardt, Glenn [mailto:Glenn.Burkhardt@goodrich.com] 
Sent: Monday, May 02, 2011 6:00 PM
To: Lubashev, Igor; pthreads-win32@sourceware.org
Subject: RE: how to get thread priorities to work

Dual core processor. 

-----Original Message-----
From: Lubashev, Igor [mailto:ilubashe@akamai.com] 
Sent: Monday, May 02, 2011 5:51 PM
To: Burkhardt, Glenn; pthreads-win32@sourceware.org
Subject: RE: how to get thread priorities to work

Without looking too much into this, are you trying this on a multi-core
machine (either multiple cores or one hyperthreading core)?  

- Igor



-----Original Message-----
From: pthreads-win32-owner@sourceware.org
[mailto:pthreads-win32-owner@sourceware.org] On Behalf Of Burkhardt,
Glenn
Sent: Monday, May 02, 2011 5:43 PM
To: pthreads-win32@sourceware.org
Subject: how to get thread priorities to work

I've been puzzling over this for way too long now.  I can't seem to get
thread priorities to work.  Any help will be appreciated.  

For the code that follows, I would expect that only one of the threads
would run, and the counter for the lower priority thread would never
increment.  But that's not the case, on both a WinXP and Win7 system
(built with MinGW 10/30/2010, gcc 4.5.2).  The counter values for the
two threads are close to being the same (e.g., 94686137, 99999999).

TIA...

#include <math.h>
#include <windows.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>

void sleep10(int *cnt) {
    double x;
    for (int i=0; i < 100000000; i++) {
        x = sin(1.0/(1.0+i));
        *cnt = i;
    }
}

void printError() {
    char *msg;
    int errorCode = GetLastError();
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
                  | FORMAT_MESSAGE_FROM_SYSTEM
                  | FORMAT_MESSAGE_IGNORE_INSERTS,
                  0, errorCode, 0, (LPTSTR)&msg, 0, 0);
    printf("error=%d: %s\n", errorCode, msg);
    LocalFree(msg);
}

void *thread(void *count) {
 
    printf("%p: %d\n", count, GetThreadPriority(GetCurrentThread()));

    sleep10(count);
 
    printf("%p exit\n", count);
    return 0;
}

int main() {
    struct sched_param spMax, spMin;
    pthread_attr_t attrMax, attrMin;
    pthread_t tMax, tMin;
    int minCount, maxCount;

    spMax.sched_priority = sched_get_priority_max(SCHED_OTHER);
    spMin.sched_priority = sched_get_priority_min(SCHED_OTHER);

    pthread_attr_init(&attrMax);
    pthread_attr_setschedparam(&attrMax, &spMax);
    pthread_attr_init(&attrMin);
    pthread_attr_setschedparam(&attrMin, &spMin);

    pthread_create(&tMax, &attrMax, thread, &maxCount);
    pthread_create(&tMin, &attrMin, thread, &minCount);

    printf("join %d\n", GetThreadPriority(GetCurrentThread()));
    pthread_join(tMax, 0);

    printf("%p: %d, %p: %d\n", &minCount, minCount, &maxCount,
maxCount);

    return 0;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: how to get thread priorities to work
  2011-05-02 22:02     ` Lubashev, Igor
@ 2011-05-02 22:05       ` Burkhardt, Glenn
  0 siblings, 0 replies; 5+ messages in thread
From: Burkhardt, Glenn @ 2011-05-02 22:05 UTC (permalink / raw)
  To: Lubashev, Igor, pthreads-win32

Yes, of course.  It was one of those "duh!" moments.  Thanks. 

-----Original Message-----
From: Lubashev, Igor [mailto:ilubashe@akamai.com] 
Sent: Monday, May 02, 2011 6:02 PM
To: Burkhardt, Glenn; pthreads-win32@sourceware.org
Subject: RE: how to get thread priorities to work

Hope this answers the question of why the two threads are running at
about the same speed.

-----Original Message-----
From: Burkhardt, Glenn [mailto:Glenn.Burkhardt@goodrich.com]
Sent: Monday, May 02, 2011 6:00 PM
To: Lubashev, Igor; pthreads-win32@sourceware.org
Subject: RE: how to get thread priorities to work

Dual core processor. 

-----Original Message-----
From: Lubashev, Igor [mailto:ilubashe@akamai.com]
Sent: Monday, May 02, 2011 5:51 PM
To: Burkhardt, Glenn; pthreads-win32@sourceware.org
Subject: RE: how to get thread priorities to work

Without looking too much into this, are you trying this on a multi-core
machine (either multiple cores or one hyperthreading core)?  

- Igor



-----Original Message-----
From: pthreads-win32-owner@sourceware.org
[mailto:pthreads-win32-owner@sourceware.org] On Behalf Of Burkhardt,
Glenn
Sent: Monday, May 02, 2011 5:43 PM
To: pthreads-win32@sourceware.org
Subject: how to get thread priorities to work

I've been puzzling over this for way too long now.  I can't seem to get
thread priorities to work.  Any help will be appreciated.  

For the code that follows, I would expect that only one of the threads
would run, and the counter for the lower priority thread would never
increment.  But that's not the case, on both a WinXP and Win7 system
(built with MinGW 10/30/2010, gcc 4.5.2).  The counter values for the
two threads are close to being the same (e.g., 94686137, 99999999).

TIA...

#include <math.h>
#include <windows.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>

void sleep10(int *cnt) {
    double x;
    for (int i=0; i < 100000000; i++) {
        x = sin(1.0/(1.0+i));
        *cnt = i;
    }
}

void printError() {
    char *msg;
    int errorCode = GetLastError();
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
                  | FORMAT_MESSAGE_FROM_SYSTEM
                  | FORMAT_MESSAGE_IGNORE_INSERTS,
                  0, errorCode, 0, (LPTSTR)&msg, 0, 0);
    printf("error=%d: %s\n", errorCode, msg);
    LocalFree(msg);
}

void *thread(void *count) {
 
    printf("%p: %d\n", count, GetThreadPriority(GetCurrentThread()));

    sleep10(count);
 
    printf("%p exit\n", count);
    return 0;
}

int main() {
    struct sched_param spMax, spMin;
    pthread_attr_t attrMax, attrMin;
    pthread_t tMax, tMin;
    int minCount, maxCount;

    spMax.sched_priority = sched_get_priority_max(SCHED_OTHER);
    spMin.sched_priority = sched_get_priority_min(SCHED_OTHER);

    pthread_attr_init(&attrMax);
    pthread_attr_setschedparam(&attrMax, &spMax);
    pthread_attr_init(&attrMin);
    pthread_attr_setschedparam(&attrMin, &spMin);

    pthread_create(&tMax, &attrMax, thread, &maxCount);
    pthread_create(&tMin, &attrMin, thread, &minCount);

    printf("join %d\n", GetThreadPriority(GetCurrentThread()));
    pthread_join(tMax, 0);

    printf("%p: %d, %p: %d\n", &minCount, minCount, &maxCount,
maxCount);

    return 0;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-05-02 22:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-02 21:42 how to get thread priorities to work Burkhardt, Glenn
2011-05-02 21:51 ` Lubashev, Igor
2011-05-02 22:00   ` Burkhardt, Glenn
2011-05-02 22:02     ` Lubashev, Igor
2011-05-02 22:05       ` Burkhardt, Glenn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).