Professional Documents
Culture Documents
Nghien Cứu Về Thread
Nghien Cứu Về Thread
==============================================================================
C Code for fork() creation test
==============================================================================
==============================================================================
C Code for pthread_create() test
==============================================================================
#include <pthread.h>
#define NTHREADS 50000
void *do_nothing(void *null)
{
int i;
i=0;
pthread_exit(NULL);
}
main()
{
int rc, i, j, detachstate;
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_attr_destroy(&attr);
pthread_exit(NULL);
/*Hy suy ngh khi chy chng trnh ny v on xem bin : myglobal s c gi tr l bao
nhiu v ti sao.y l mt li nn chng ta phi control n v chng ta s dng Mutexs cho vn
ny */
2,MUTEX:
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int myglobal;
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;
void *thread_function(void *arg)
{
int i,j;
for ( i=0; i<20; i++ ) {
pthread_mutex_lock(&mymutex);
j=myglobal;
j=j+1;
printf(".");
fflush(stdout);
sleep(1);
myglobal=j;
pthread_mutex_unlock(&mymutex);
}
return NULL;
}
int main(void)
{
pthread_t mythread;
int i;
if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {
printf("error creating thread.");
abort();
}
for ( i=0; i<20; i++) {
pthread_mutex_lock(&mymutex);
myglobal=myglobal+1;
pthread_mutex_unlock(&mymutex);
printf("o");
fflush(stdout);
sleep(1);
}
if ( pthread_join ( mythread, NULL ) ) {
printf("error joining thread.");
abort();
}
printf("\nmyglobal equals %d\n",myglobal);
exit(0);
}
Nu chng ta so snh on m ny vi on m trong chng trnh thread2.c, chng ta s thy
c thm nhng li gi hm pthread_mutex_lock() v pthread_mutex_unlock(). Nhng li gi hm
ny rt cn thit trong lp trnh vi thread. Chng cung cp cch thc loi tr ln nhau (mutual
exclusion) c ngha l khi mt thread s dng ti nguyn th thread khc khng c s dng
v phi ch cho n khi thread kia gii phng ti nguyn. Khng c hai thread no c th cng
c mt mutex b kha mt thi im.
chnh l cch thc mutex lm vic. Nu thread a c gng kha mt mutex trong khi thread
b kha cng mutex ri th thread a s ri vo trng thi ng (sleep). Ngay khi thread b
gii phng mutex (thng qua pthread_mutex_unlock()), thread a s c th kha mutex (ni mt
cch khc th khi gi tr ca hm pthread_mutex_lock() s c tr v vi mutex b lock).
Ngc li, nu thread c c gng kha mutex trong khi thread a ang nm gi n th thread c
cng s b ri vo trng thi ng tm thi. Tt c thread m cng ri vo trng thi ng t lnh
gi pthread_mutex_lock() v mt kha mutex b lock sn ri s c a vo hng i truy
nhp vo mutex.
pthread_mutex_lock() v pthread_mutex_unlock() thng c s dng bo v cu trc d
liu. N m bo rng ch c mt thread ti mt thi im c th truy nhp vo cu trc d liu
bng vic kha v m kha. Bn cng c th on ra rng POSIX threads s cp mt kha m
khng a thread vo trng thi ng khi n c gng kha mt mutex khng b kha.
Th nhng ti sao chng ta li phi a cc thread vo trng thi sleep? R rng n khng phi
l u im chnh ca thread khi m ta mun tn dng kh nng lm vic c lp v ng thi
ca cc thread? ng vy. Tuy nhin th cc chng trnh s dng thread vn thng cn phi
s dng mutex.
Nu bn nhn vo hm thread_function(), bn s nhn ra rng mutex b kha vo lc u vng
lp v gii phng khi kt thc. Trong v d ny th mymutex c dng bo v gi tr ca
myglobal. Nu nhn cn thn hn, s thy rng on m tng gi tr ca myglobal trong
thread_function s copy gi tr myglobal v mt bin local v ghi tr li t local vo myglobal sau
khi tng (on m sleep mt giy c th b i. Chng c l do g thread_function phi sleep
khi s dng mutex. N c thm vo c th nhn r hn s khc bit).
3, CONDITION VARIABLE:
Chng ta bit cch thc dng mutex ng b ha gia cc thread, trnh xung
t gia cc thread khi cng truy cp ti nguyn. Tuy nhin th khng phi mutex
4,SEMAPHORE:
/*on code v semaphore vi chc nng tng gim semaphore*/
/* Includes */
#include <unistd.h>
/* Symbolic Constants */
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h>
/* Errors */
#include <stdio.h>
/* Input/Output */
#include <stdlib.h>
/* General Utilities */
#include <pthread.h>
/* POSIX Threads */
#include <string.h>
/* String handling */
#include <semaphore.h> /* Semaphore */
/* prototype for thread routine */
void handler ( void *ptr );
/* global vars */
/* semaphores are declared global so they can be accessed
in main() and in thread routine,
here, the semaphore is used as a mutex */
sem_t mutex;
int counter; /* shared variable */
int main()
{
int i[2];
pthread_t thread_a;
pthread_t thread_b;
i[0] = 0; /* argument to threads */
i[1] = 1;
sem_init(&mutex, 0, 1);
semaphore */
local */
sem_wait(&mutex);
/* down semaphore */
/* START CRITICAL REGION */
printf("Thread %d: Now in critical region...\n", x);
printf("Thread %d: Counter Value: %d\n", x, counter);
printf("Thread %d: Incrementing Counter...\n", x);
counter++;
printf("Thread %d: New Counter Value: %d\n", x, counter);
printf("Thread %d: Exiting critical region...\n", x);
/* END CRITICAL REGION */
sem_post(&mutex);
/* up semaphore */
pthread_exit(0); /* exit thread */
}