Two threads work together to handle Bubble Sorting Linux dual-thread processing

Source: Internet
Author: User

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <pthread. h>

Int arr [] = {7,348 };
Int g_ I = 0;
Pthread_t thread [2];
Pthread_mutex_t mutex_1, mutex_2; // mutex lock

Void print_arry () // print the Array
{
Int I = 0;
For (; I <10; I ++)
{
Printf ("% d \ t", arr [I]);
}
Printf ("\ n ");
}
Void swap_elem (int * a, int * B) // value in the exchange address
{
Int temp;
Temp = *;
* A = * B;
* B = temp;
}
Void * entrance_1 (void * Arg) // processing function of thread 1
{
Printf ("1 \ n ");
Int J = 0;
For (; g_ I <10; g_ I ++)
{
Pthread_mutex_lock (& mutex_2 );
// Printf ("1g_ I = % d \ n", g_ I );
Printf ("thread 1 executes background sorting. \ n ");
For (j = 0; j <10-g_i-1; j ++)

{
If (ARR [J]> arr [J + 1])
{
Swap_elem (& arr [J], & arr [J + 1]);
}
}
Pthread_mutex_unlock (& mutex_1 );
}

Return (void *) 1 );
}
Void * entrance_2 (void * Arg) // processing function of thread 2
{
Printf ("2 \ n ");
Int J = 0;
For (; g_ I <10; g_ I ++)
{
Pthread_mutex_lock (& mutex_1 );

// Printf ("2g_ I = % d \ n", g_ I );
Printf ("thread 2 executes background sorting. \ n ");
For (j = 0; j <10-g_i-1; j ++)
{
If (ARR [J]> arr [J + 1])
{
Swap_elem (& arr [J], & arr [J + 1]);
}
}
Pthread_mutex_unlock (& mutex_2 );
}
Return (void *) 2 );
}

Void create_two_thread () // create two threads
{

Memset (thread, 0, sizeof (thread ));
If (pthread_create (& Thread [0], null, entrance_1, null) = 0)
{
Printf ("thread 1 created successfully \ n ");
} Else {

Printf ("failed to create thread 1 \ n ");
Exit (exit_failure );
}
If (pthread_create (& Thread [1], null, entrance_2, null) = 0)
{
Printf ("thread 2 created successfully \ n ");
} Else {

Printf ("creating thread 2 failed \ n ");
Exit (exit_failure );
}

}
Void do_and_wait () // 2 thread execution and waiting
{
Void * RET [2];
If (thread [0]! = 0)
{
Pthread_join (thread [0], & RET [0]);
Printf ("\ n thread 1 execution ends and exits \ n ");
} Else {
Printf ("thread 1 creation failed \ n ");
Exit (exit_failure );
}

If (thread [1]! = 0)
{

Pthread_join (thread [1], & RET [1]);
Printf ("\ n thread 2 execution ends and exits \ n ");

} Else {
Printf ("thread 2 creation failed \ n ");
Exit (exit_failure );
}
Printf ("Return Value of thread 1: % d \ n", (INT) RET [0]);
Printf ("Return Value of thread 2: % d \ n", (INT) RET [1]);
Pthread_mutex_destroy (& mutex_1 );
Pthread_mutex_destroy (& mutex_2 );
}

Int main ()
{
Printf ("main function, create two threads to jointly implement Bubble Sorting \ n ");
Pthread_mutex_init (& mutex_1, null );
Pthread_mutex_init (& mutex_2, null );
Print_arry ();
Create_two_thread ();
Do_and_wait ();
Printf ("Sorted. \ n ");
Print_arry ();
Return 0;
}
The running result is:

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.