线程

代码
   
     
#include < unistd.h >
#include
< pthread.h >
#include
< stdio.h >

void * thr_fn1( void * arg)
{
printf(
" thread 1 returning\n " );
return (( void * ) 1 );
}

void * thr_fn2( void * arg)
{
printf(
" thread 2 returning\n " );
return (( void * ) 2 );
}

int main( void )
{
int err;
pthread_t tid1,tid2;
void * tret;

err
= pthread_create( & tid1,NULL,thr_fn1,NULL);
err
= pthread_create( & tid2,NULL,thr_fn2,NULL);
err
= pthread_join(tid1, & tret);
printf(
" thread 1 exti code %d\n " ,( int )tret);
err
= pthread_join(tid2, & tret);
printf(
" thread 2 exit code %d\n " ,( int )tret);
return 0 ;
}

 

代码
   
     
#include < unistd.h >
#include
< stdio.h >
#include
< pthread.h >
#include
< signal.h >
#include
< stdlib.h >

int quitflag;
sigset_t mask;
pthread_mutex_t
lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t wait
= PTHREAD_COND_INITIALIZER;

void * thr_fn( void * arg)
{
int err,signo;
while ( 1 )
{
err
= sigwait( & mask, & signo);
switch (signo)
{
case SIGINT:
printf(
" \nSIGINT\n " );
break ;
case SIGQUIT:
pthread_mutex_lock(
& lock );
quitflag
= 1 ;
pthread_mutex_unlock(
& lock );
pthread_cond_signal(
& wait);
exit(
0 );
default :
printf(
" unexpected signal %d\n " ,signo);
exit(
1 );
}
}
}

int main( void )
{
int err;
sigset_t oldmask;
pthread_t tid;
sigemptyset(
& mask);
sigaddset(
& mask,SIGINT);
sigaddset(
& mask,SIGQUIT);
if ((err = pthread_sigmask(SIG_BLOCK, & mask, & oldmask)) != 0 )
{
printf(
" SIG_BLOCK error\n " );
return 1 ;
}

err
= pthread_create( & tid,NULL,thr_fn, 0 );
pthread_mutex_lock(
& lock );
while (quitflag == 0 )
pthread_cond_wait(
& wait, & lock );
pthread_mutex_unlock(
& lock );
quitflag
= 0 ;
if (sigprocmask(SIG_SETMASK, & oldmask,NULL) < 0 )
{
printf(
" SIG_SETMASK error\n " );
}
}

 

代码
   
     
#include < unistd.h >
#include
< stdio.h >
#include
< pthread.h >

void cleanup( void * arg)
{
printf(
" cleanup:%s\n " ,( char * )arg);
}

void * thr_fn1( void * arg)
{
printf(
" thread 1 start\n " );
pthread_cleanup_push(cleanup,(
void * ) " thread 1 first handler " );
pthread_cleanup_push(cleanup,(
void * ) " thread 1 second handler " );
printf(
" thread 1 push complete\n " );
if (arg)
return (( void * ) 1 );
pthread_cleanup_pop(
0 );
pthread_cleanup_pop(
0 );
return (( void * ) 1 );
}

void * thr_fn2( void * arg)
{
printf(
" thread 2 start\n " );
pthread_cleanup_push(cleanup,(
void * ) " thread 2 first handler " );
pthread_cleanup_push(cleanup,(
void * ) " thread 2 second handler " );
printf(
" thread 2 push complete\n " );
if (arg)
pthread_exit((
void * ) 2 );
pthread_cleanup_pop(
0 );
pthread_cleanup_pop(
0 );
pthread_exit((
void * ) 2 );
}

int main( void )
{
int err;
pthread_t tid1,tid2;
void * tret;

err
= pthread_create( & tid1,NULL,thr_fn1,( void * ) 1 );
err
= pthread_create( & tid2,NULL,thr_fn2,( void * ) 2 );
err
= pthread_join(tid1, & tret);
printf(
" thread 1 exit code %d\n " ,( int )tret);
err
= pthread_join(tid2, & tret);
printf(
" thread 2 exit code %d\n " ,( int )tret);
return 0 ;
}

 

代码
   
     
#include < unistd.h >
#include
< stdio.h >
#include
< stdlib.h >
#include
< pthread.h >

pthread_mutex_t lock1
= PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t lock2
= PTHREAD_MUTEX_INITIALIZER;

void prepare( void )
{
printf(
" preparing locks...\n " );
pthread_mutex_lock(
& lock1);
pthread_mutex_lock(
& lock2);
}

void parent( void )
{
printf(
" parent unlock locks...\n " );
pthread_mutex_unlock(
& lock1);
pthread_mutex_unlock(
& lock2);
}

void child( void )
{
printf(
" child unlocking locks...\n " );
pthread_mutex_unlock(
& lock1);
pthread_mutex_unlock(
& lock2);
}

void * thr_fn( void * arg)
{
printf(
" thread started...\n " );
pause();
return 0 ;
}

int main( void )
{
int err;
pid_t pid;
pthread_t tid;

err
= pthread_atfork(prepare,parent,child);
pthread_create(
& tid,NULL,thr_fn, 0 );

sleep(
2 );
printf(
" parent about to fork...\n " );
pid
= fork();
if (pid == 0 )
printf(
" child return form fork\n " );
else
printf(
" parent returned from fork\n " );
return 0 ;
}

 

你可能感兴趣的:(线程)