C pointer principle (89)-LINUX application (2)-Thread Pool

Source: Internet
Author: User

Knoppix @ Microknoppix:/mnt-system/lx/test $ gcc-D_REENTRANT-lpthread-o testcondout. c
Knoppix @ Microknoppix:/mnt-system/lx/test $./testcondout

Please input an integer :( <= 1000) 26
Time out
Time out
1/1 finished, push 1.0000000000
1/2 finished, push 0.5000000000
1/3 finished, push 0.3333333333
1/4 finished, push 0.2500000000
1/5 finished, push 0.2000000000
Time out
1/6 finished, push 0.1666666667
1/7 finished, push 0.1428571429
1/8 finished, push 0.1250000000
1/9 finished, push 0.1111111111
1/10 finished, push 0.1000000000
Time out
1/11 finished, push 0.0909090909
1/12 finished, push 0.0833333333
1/13 finished, push 0.0769230769
1/14 finished, push 0.0714285714
1/15 finished, push 0.0666666667
1/1: 1.0000000000 added result 1.0000000000
1/2: 0.5000000000 added result 1.5000000000
1/3: 0.3333333333 added result 1.8333333333
1/4: 0.2500000000 added result 2.0833333333
1/5: 0.2000000000 added result 2.2833333333
1/6: 0.1666666667 added result 2.4500000000
1/7: 0.1428571429 added result 2.5928571429
1/8: 0.1250000000 added result 2.7178571429
1/9: 0.1111111111 added result 2.8289682540
1/10: 0.1000000000 added result 2.9289682540
1/11: 0.0909090909 added result 3.0198773449
1/12: 0.0833333333 added result 3.1032106782
1/13: 0.0769230769 added result 3.1801337551
1/14: 0.0714285714 added result 3.2515623266
1/15: 0.0666666667 added result 3.3182289932
* ***** 1/1: 1.0000000000 computed result 1.0000000000
* ***** 1/2: 0.5000000000 computed result 0.5000000000
* ***** 1/3: 0.3333333333 computed result 0.8333333333
* ***** 1/4: 0.2500000000 computed result 0.5833333333
* ***** 1/5: 0.2000000000 computed result 0.7833333333
* ***** 1/6: 0.1666666667 computed result 0.6166666667
* ***** 1/7: 0.1428571429 computed result 0.7595238095
* ***** 1/8: 0.1250000000 computed result 0.6345238095
* ***** 1/9: 0.1111111111 computed result 0.7456349206
* ***** 1/10: 0.1000000000 computed result 0.6456349206
* ***** 1/11: 0.0909090909 computed result 0.7365440115
* ***** 1/12: 0.0833333333 computed result 0.6532106782
* ***** 1/13: 0.0769230769 computed result 0.7301337551
* ***** 1/14: 0.0714285714 computed result 0.6587051837
* ***** 1/15: 0.0666666667 computed result 0.7253718504
=================== Compute finish! ============= Result: 0.7253718504
1/16 finished, push 0.0625000000
1/17 finished, push 0.0588235294
1/18 finished, push 0.0555555556
1/19 finished, push 0.0526315789
1/20 finished, push 0.0500000000
1/16: 0.0625000000 added result 3.3807289932
1/17: 0.0588235294 added result 3.4395525226
1/18: 0.0555555556 added result 3.4951080782
1/19: 0.0526315789 added result 3.5477396571
1/20: 0.0500000000 added result 3.5977396571
1/21 finished, push 0.0476190476
1/22 finished, push 0.0454545455
1/23 finished, push 0.0434782609
1/24 finished, push 0.0416666667
1/25 finished, push 0.0400000000
Time out
* ***** 1/16: 0.0625000000 computed result 0.6628718504
* ***** 1/17: 0.0588235294 computed result 0.7216953798
* ***** 1/18: 0.0555555556 computed result 0.6661398242
* ***** 1/19: 0.0526315789 computed result 0.7187714032
* ***** 1/20: 0.0500000000 computed result 0.6687714032
* ***** 1/21: 0.0476190476 computed result 0.7163904508
* ***** 1/22: 0.0454545455 computed result 0.6709359053
* ***** 1/23: 0.0434782609 computed result 0.7144141662
* ***** 1/24: 0.0416666667 computed result 0.6727474995
* ***** 1/25: 0.0400000000 computed result 0.7127474995
=================== Compute finish! ============= Result: 0.7127474995
1/21: 0.0476190476 added result 3.6453587048
1/22: 0.0454545455 added result 3.6908132502
1/23: 0.0434782609 added result 3.7342915111
1/24: 0.0416666667 added result 3.7759581778
1/25: 0.0400000000 added result 3.8159581778
1/26 finished, push 0.0384615385
Time out
Time out
* ***** 1/26: 0.0384615385 computed result 0.6742859611
=================== Compute finish! ============= Result: 0.6742859611
1/26: 0.0384615385 added result 3.8544197162
====================== Add finish! ============= Result: 3.8544197162

All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/

Two threads complete the accumulate and accumulate operations (one uses the timeout waiting condition signal and the other uses the waiting condition signal). n threads complete the calculation of each operator point.

# Include
# Include
# Include
# Include
# Include
# Define MAXS 1000
# Define MAXTDS 5 // thread pool size


Double myjg [MAXS + 1]; // storage location of computing results
Int max;
Pthread_mutex_t eventlock; // mutex lock
Pthread_cond_t myevent; // condition variable
Pthread_t threads [MAXTDS + 2]; // thread pool, completing 1/n computing
Int isend = 0;


Int done;

Void * mycomp (void * x) {// calculate the result of 1/I, and put the result in an array.
Int I = 0;
Int rc;
While (1 ){
Pthread_mutex_lock (& eventlock );
If (isend ){
Pthread_mutex_unlock (& eventlock );
Break;
}
I = myjg [0]; // myjg [0] stores the I that has been computed by the thread.
If (I I ++;
Myjg [0] = I;
}
If (I = max) {// the last number
Myjg [I] = (1/(double) I );
Isend = 1;
Printf ("1/% d finished, push %. 10f \ n", I, myjg [I]);
Fflush (stdout );
Pthread_mutex_unlock (& eventlock );
Sleep (3 );
Rc = pthread_cond_signal (& myevent); // broadcast signal. multiple tasks are not blocked, and multiple tasks compete for the ownership of the mutex lock. You can also use pthread_cond_signal (& event); to send signals, so that only one thread is not blocked and other threads are blocked.
If (rc ){
Perror ("pthread_cond_broadcast ");
Fflush (stdout );
}
Sleep (2 );
Break;
}

// Start Calculation
Myjg [I] = (1/(double) I );
Printf ("1/% d finished, push %. 10f \ n", I, myjg [I]);
Fflush (stdout );
Pthread_mutex_unlock (& eventlock );
If (! (I % MAXTDS )){
Sleep (3 );
Pthread_cond_broadcast (& myevent); // broadcast signal. multiple tasks are not blocked. multiple tasks compete for the ownership of the mutex lock. You can also use pthread_cond_signal (& event); to send signals, so that only one thread is not blocked and other threads are blocked.
Sleep (3 );
}
}
Pthread_exit (NULL );
}



Void * myprint1 (void * xx) {// read the array, accumulate the calculation result, and finally complete 1/1 + 1/2 + 1/3 + ...... + 1/n calculation, timeout wait
Int maxi;
Int curj = 1;
Double jg = 0;
Int rc;
Struct timeval now; // use microseconds
Struct timespec timeout; // use nanoseconds





While (curj <= max)
{
// Obtain the current time
// Myhaspl
Gettimeofday (& now );
// Preparation Interval
Timeout. TV _sec = now. TV _sec + 1;
Timeout. TV _nsec = now. TV _usec * 1000;
Maxi = 0;

Pthread_mutex_lock (& eventlock); // used for the mutex of condition variables. Condition variables are semaphores used to send event signals.
Rc = pthread_cond_timedwait (& myevent, & eventlock, & timeout); // wait for the occurrence of the Conditional Variable myevent. If the timeout occurs, the system returns and does not wait. The condition variable must be associated with a mutex lock eventlock. The condition variable does not provide a lock and must have a mutex lock eventlock.
// Mutex lock eventlock should be locked before wait is called, and the mutex eventlock will be unlocked During the wait period. The thread is suspended until the condition variable myevent receives a signal.


If (rc = 0) {// myhaspl
Maxi = myjg [0];
Fflush (stdout );
Pthread_mutex_unlock (& eventlock );
For (; curj <= maxi; curj ++)
{// Myhaspl
Jg + = myjg [curj];
Printf ("1/% d: %. 10f added result %. 10f \ n", curj, myjg [curj], jg );
Fflush (stdout );
}


}
Else if (rc = ETIMEDOUT) {// TIMEOUT
Printf ("time out \ n ");
Fflush (stdout );
Pthread_mutex_unlock (& eventlock );
Continue;
}
Else {// myhaspl
Perror ("pthread_cond_wait ");
Fflush (stdout );
Pthread_mutex_unlock (& eventlock );
Continue;
}
}
Printf ("======================= add finish! ============== Result: %. 10f \ n ", jg); // output the accumulate result.
Fflush (stdout );
Pthread_exit (NULL );
}

Void * myprint2 (void * xx) {// read the array and complete the calculation of the calculation result 1/1 + 1/2-1/3 + 1/4-1/5 ......
Int maxi = 0;
Int curi = 1;
Double jg = 0;
Int fh = 1;
Int rc;
While (curi <= max)
{
Maxi = 0;
Sleep (2 );
Pthread_mutex_lock (& eventlock); // used for the mutex of condition variables. Condition variables are semaphores used to send event signals.
Rc = pthread_cond_wait (& myevent, & eventlock); // wait for the occurrence of the condition variable myevent. The condition variable must be associated with a mutex lock eventlock. The condition variable does not provide a lock and must have a mutex lock eventlock.
// Mutex lock eventlock should be locked before wait is called, and the mutex eventlock will be unlocked During the wait period. The thread is suspended until the condition variable myevent receives a signal.
// Myhaspl
If (rc = 0 ){
Maxi = myjg [0];
Fflush (stdout );
Pthread_mutex_unlock (& eventlock );
While (curi <= maxi ){
Jg + = fh * myjg [curi];
Printf ("******** 1/% d: %. 10f computed result %. 10f \ n", curi, myjg [curi], jg );
Fflush (stdout );
Fh =-fh;
Curi ++;
}
Printf ("===================== compute finish! ============ Result: %. 10f \ n ", jg); // output the accumulate result
Fflush (stdout );
}
Else {// error
Perror ("pthread_cond_wait ");
Fflush (stdout );
Pthread_mutex_unlock (& eventlock );
Continue;
}
}


Pthread_exit (NULL );
}

Int main (){
// Calculate 1 + 1/2 + 1/3 +... and 1 + 1/2-1/3 + 1/4-1/5 ......


Pthread_mutex_init (& eventlock, NULL );
Pthread_cond_init (& myevent, NULL );
Int I = 0;

Printf ("please input an integer :( <=% d)", MAXS );
While (scanf ("% d", & max), max> MAXS) {// n maximum value
Printf ("please input an integer :( <=% d)", MAXS );
};
// Myhaspl
Myjg [0] = 0;

Pthread_create (& (threads [I]), NULL, myprint1, (void *) & I );
Sleep (1 );
I ++;
Pthread_create (& (threads [I]), NULL, myprint2, (void *) & I );
Sleep (1 );
I ++;
For (; I <= MAXTDS; I ++ ){
Pthread_create (& (threads [I]), NULL, mycomp, (void *) & I );
Sleep (1 );
}
Sleep (MAXTDS * 2*(I/10 + 1); // wait ......
Pthread_mutex_destroy (& eventlock );
Return (0 );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.