PDA

View Full Version : Hướng Dẫn Tạo Thread bằng Linux API



AlexF
21-04-2011, 10:26 PM
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *Day_la_Thread_Ma_Ban_CanDung( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}

int main()
{
pthread_t thread1, thread2;
char *message1 = "Sonnh89: Day la Thread 1";
char *message2 = "Sonnh89: Day la Thread 2";
int iret1, iret2;

iret1 = pthread_create( &thread1, NULL, Day_la_Thread_Ma_Ban_CanDung, (void*) message1);
iret2 = pthread_create( &thread2, NULL, Day_la_Thread_Ma_Ban_CanDung, (void*) message2);

pthread_join( thread1, NULL);//day la ham wait cho thread thuc hien xong moi chay tiep
pthread_join( thread2, NULL);

printf("GIa Tri Tra ve: %d\n",iret1);
printf("GIa Tri Tra ve: %d\n",iret2);
return 0;
}



lưu lại với tên file là sonnh89.c
biên dịch: gcc -pthread sonnh89.c
./a.out
để biên dịch


VD về dùng mutex đồng bộ giữa các thread


#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;

void *Day_La_Ham_Can_Thuc_Hien_Cua_Thread(void *ptr)
{
char *message;
message = (char *) ptr;
int i = 0;
while(i <=10)
{
pthread_mutex_lock( &mutex1 );
counter++;
printf("%s Gia Tri Bien Dem: %d\n",message ,counter);
pthread_mutex_unlock( &mutex1 );
i++;
}
}

int main()
{
int rc1, rc2;
pthread_t thread1, thread2;
char *message1 = "Day la Thread 1";
char *message2 = "Day la Thread 2";
/* Create independent threads each of which will execute Day_La_Ham_Can_Thuc_Hien_Cua_Thread */

if( (rc1=pthread_create( &thread1, NULL, Day_La_Ham_Can_Thuc_Hien_Cua_Thread, (void*) message1)) )
{
printf("Thread creation failed: %d\n", rc1);
}

if( (rc2=pthread_create( &thread2, NULL, Day_La_Ham_Can_Thuc_Hien_Cua_Thread, (void*) message2)) )
{
printf("Thread creation failed: %d\n", rc2);
}

/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */

pthread_join( thread1, NULL);
pthread_join( thread2, NULL);

return 0;
}




#include <stdio.h>
#include <pthread.h>

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;
#define NTHREADS 10

void *thread_function(void *dummyPtr)
{
printf("Day La Thread So %ld\n", pthread_self());
pthread_mutex_lock( &mutex1 );
counter++;
pthread_mutex_unlock( &mutex1 );
}

int main()
{
pthread_t thread_id[NTHREADS];
int i, j;

for(i=0; i < NTHREADS; i++)
{
pthread_create( &thread_id[i], NULL, thread_function, NULL );
}

for(j=0; j < NTHREADS; j++)
{
pthread_join( thread_id[j], NULL); //ham pthread_join la cho den khi nao thread hoan thanh xong thi chay tiep
}

/* Now that all threads are complete I can print the final result. */
/* Without the join I could be printing a value before all the threads */
/* have been completed. */

printf("Final counter value: %d\n", counter);
}




#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER;
int count = 0;
#define COUNT_DONE 10
#define COUNT_HALT1 3
#define COUNT_HALT2 6

// Write numbers 1-3 and 8-10 as permitted by functionCount2()
void *functionCount1(void* asd)
{
for(;;)
{
// Lock mutex and then wait for signal to relase mutex
pthread_mutex_lock( &count_mutex );

// Wait while functionCount2() operates on count
// mutex unlocked if condition varialbe in functionCount2() signaled.
pthread_cond_wait( &condition_var, &count_mutex );
count++;
printf("Counter value functionCount1: %d\n",count);

pthread_mutex_unlock( &count_mutex );

if(count >= COUNT_DONE)
return(NULL);
}
}

// Write numbers 4-7
void *functionCount2(void* asd)
{
for(;;)
{
pthread_mutex_lock( &count_mutex );

if( count < COUNT_HALT1 || count > COUNT_HALT2 )
{
// Condition of if statement has been met.
// Signal to free waiting thread by freeing the mutex.
// Note: functionCount1() is now permitted to modify "count".
pthread_cond_signal( &condition_var );
}
else
{
count++;
printf("Counter value functionCount2: %d\n",count);
}

pthread_mutex_unlock( &count_mutex );

if(count >= COUNT_DONE)
return(NULL);
}

}

main()
{
pthread_t thread1, thread2;

pthread_create( &thread1, NULL, functionCount1, NULL);
pthread_create( &thread2, NULL, functionCount2, NULL);

pthread_join( thread1, NULL);
pthread_join( thread2, NULL);

printf("Final count: %d\n",count);

exit(0);
}