by Gavin Y » Tue, 15 Nov 2005 21:19:37
his is a multi-part message in MIME format.
My god, what is wrong for myself???? I have make the same mistake for the second time!!!!!!!!!!!!!!!
Below is the final version for my code:
========================================================================
include <pthread.h>
#include <stdio.h>
void* dowork(void* idp)
{
int i;
int *my_id = (int*) idp;
printf("Thread %d is starting up\n", *my_id);
sleep(2);
while(1)
{
printf("Thread %d is doing work. \n", *my_id);
}
}
int main (int argc, char* argv[])
{
int i;
pthread_t threads[2];
int thread_ids[2] = {0, 1};
pthread_attr_t attr_high, attr_low;
struct sched_param param_high, param_low;
pthread_attr_init(&attr_high);
pthread_attr_init(&attr_low);
pthread_attr_setinheritsched(&attr_high, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setinheritsched(&attr_low, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setdetachstate(&attr_high, PTHREAD_CREATE_JOINABLE);
pthread_attr_setdetachstate(&attr_low, PTHREAD_CREATE_JOINABLE);
/* set thread priority 42 and scheduling policy SCHED_RR for high thread */
param_high.sched_priority = 42;
pthread_attr_setschedparam(&attr_high, ¶m_high);
pthread_attr_setschedpolicy(&attr_high, SCHED_RR);
pthread_create(&threads[0], &attr_high, dowork, &thread_ids[0]);
/* set thread priority 41 and scheduling policy SCHED_RR for low thread */
param_low.sched_priority = 41;
pthread_attr_setschedparam(&attr_low, ¶m_low);
pthread_attr_setschedpolicy(&attr_low, SCHED_RR);
pthread_create(&threads[1], &attr_low, dowork, &thread_ids[1]);
for (i = 0; i < 2; i++)
{
pthread_join(threads[i], NULL);
}
printf ("Main(): Waited on 2 threads. Done.\n");
/* Clean up and exit */
pthread_attr_destroy(&attr_high);
pthread_attr_destroy(&attr_low);
pthread_exit(NULL);
}
"Gavin Yu" < XXXX@XXXXX.COM > wrote in message news:dl9u73$ XXXX@XXXXX.COM ...
There are too many typo errors in my former mail, I re-copy as below:
==================================================================
#include <pthread.h>
#include <stdio.h>
void* dowork(void* idp)
{
int i;
int *my_id = (int*) idp;
printf("Thread %d is starting up\n", *my_id);
sleep(2);
while(1)
{
printf("Thread %d is doing work. \n", *my_id);
}
}
int main (int argc, char* argv[])
{
int i;
pthread_t threads[2];
int thread_ids[2] = {0, 1};
pthread_attr_t attr_high, attr_low;
struct sched_param param_high, param_low;
pthread_attr_init(&attr_high);
pthread_attr_init(&attr_low);
pthread_attr_setinheritsched(&attr_high, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setinheritsched(&attr_low, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setdetachstate(&attr_high, PTHREAD_CREATE_JOINABLE);
pthread_attr_setdetachstate(&attr_low, PTHREAD_CREATE_JOINABLE);
/* set thread priority 42 and scheduling policy SCHED_RR for high thread */
param_high.sched_priority = 42;