Linux创建线程与多线程互斥及同步

创建线程

main.cpp

#include 
#include 
#include 
void* threadfn( void *arg )
{
    pthread_detach(pthread_self());
    for(int i = 0;i<10;i++)
    {
    printf("i=%d\n",i);
    sleep(1);
    }
    printf( "This is a thread and arg = %d.\n", *(int*)arg);
    *(int*)arg = 0;
    return arg;
}
int main( int argc, char *argv[] )
{
    pthread_t th_id;
    int ret;
    int param = 7;
    int *thread_ret = NULL;
    ret = pthread_create( &th_id, NULL, threadfn, ¶m );
    if( ret != 0 ){
        printf( "error!\n");
        return -1;
    }
    printf( "the main process.\n" );
    pthread_join( th_id, (void**)&thread_ret );
    //pthread_detach(th_id);
    for(int i = 0;i<10;i++)
    {
        printf("main i=%d\n",i);
    sleep(1);   
    }
    printf( "thread_ret = %d.\n", *thread_ret );
    return 0;
}

Makefile:

overpass:main.o 
    g++ -g -o  overpass main.o  -lpthread
main.o:main.cpp
    g++ -g -c main.cpp -o main.o
clean:
    rm -f *.o overpass

多线程互斥

main.cpp

#include 
#include 

pthread_mutex_t mutex;      //互斥量声明
int counter = 0;    //互斥量保护的全局变量

void *thread_function(void *dummyPtr)
{
   printf("Thread number %ld\n", pthread_self());
   pthread_mutex_lock( &mutex);
   counter++;
   pthread_mutex_unlock( &mutex);
}

int main(int argc, char *argv[])
{
   pthread_t thread_id[10];
   int i, j;

   pthread_mutex_init(&mutex, NULL);
   for(i=0; i < 10; i++)
   {
      pthread_create( &thread_id[i], NULL, thread_function, NULL );
   }
   for(j=0; j < 10; j++)
   {
      pthread_join( thread_id[j], NULL); 
   }                                             
   printf("Final counter value: %d\n", counter);
   return 0;
}

makefile:

overpass:main.o 
    g++ -g -o  overpass main.o  -lpthread
main.o:main.cpp
    g++ -g -c main.cpp -o main.o
clean:
    rm -f *.o overpass

线程同步

main.cpp

#include 
#include 
#include 
#include 
#include 

pthread_mutex_t count_lock;
pthread_cond_t count_nz_event;
unsigned count = 0;

void *decrement_count(void *dummyPtr) 
{
    for(int i = 0;i<10;i++)
    {
        pthread_mutex_lock (&count_lock);
        while(count==0)
        pthread_cond_wait( &count_nz_event, &count_lock);

        count=count -1;
        printf("decrement thread number:%ld,count:%u\n",pthread_self(),count);
        pthread_mutex_unlock (&count_lock);
        sleep(1);
    }
}
void *increment_count(void *dummyPtr)
{
    for(int i = 0;i<10;i++)
    {
        pthread_mutex_lock(&count_lock);
        count++;
        if(count-1==0)
        pthread_cond_signal(&count_nz_event);
        printf("increment thread number:%ld,count:%u\n",pthread_self(),count);
        pthread_mutex_unlock(&count_lock);
        sleep(1);
    }
}


int main(int argc, char* argv[])
{
    pthread_mutex_init(&count_lock, NULL);
    pthread_cond_init(&count_nz_event, NULL);
    pthread_t p1, p2;
    pthread_create(&p1, NULL, decrement_count, NULL);
    pthread_create(&p2, NULL, increment_count, NULL);
    pthread_join( p1, NULL); 
    pthread_join( p2, NULL); 
    printf("main thread number:%ld,count:%u\n",pthread_self(),count);
}

makefile:

overpass:main.o 
    g++ -g -o  overpass main.o  -lpthread
main.o:main.cpp
    g++ -g -c main.cpp -o main.o
clean:
    rm -f *.o overpass

你可能感兴趣的:(Linux创建线程与多线程互斥及同步)