歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

linux線程與線程安全

linux線程與線程安全
0.在linux中創建一個線程
main.cpp
#include <stdio.h>
#include <pthread.h>

void *thread_function(void *dummyPtr)
{
    printf("Thread number %ld\n", pthread_self());
}

int main(int argc, char *argv[])
{
   pthread_t thread_id;
   pthread_create( &thread_id, NULL, thread_function, NULL );
   
   pthread_join( thread_id, NULL); 
   
   printf("Final counter value: %d\n", counter);
   return 0;
}

Makefile
引用了thead庫
linuxthead:main.o 
	g++ -g -o  linuxthread main.o -L. -lpthread -lrt
main.o:main.cpp
	g++ -g -c main.cpp -o main.o
clean:
	rm -f *.o linuxthead

pthread_create()
創建一個線程默認的狀態是joinable, 如果一個線程結束運行但沒有被join,則它的狀態類似於進程中的Zombie Process,即還有一部分資源沒有被回收(退出狀態碼),所以創建線程者應該調用pthread_join來等待線程運行結束,並可得到線程的退出代碼,回收其資源(類似於wait,waitpid)但是調用pthread_join(pthread_id)後,如果該線程沒有運行結束,調用者會被阻塞,在有些情況下我們並不希望如此,比如在Web服務器中當主線程為每個新來的鏈接創建一個子線程進行處理的時候,主線程並不希望因為調用pthread_join而阻塞(因為還要繼續處理之後到來的鏈接),這時可以在子線程中加入代碼pthread_detach(pthread_self())或者父線程調用pthread_detach(thread_id)(非阻塞,可立即返回)
1.多線程互斥
pthread_mutex_t mutex;	//互斥量聲明
int g_counter = 0; //互斥量保護的全局變量
pthread_mutex_init(&mutex, NULL);

使用:
pthread_mutex_lock( &mutex1 );
g_counter++;
pthread_mutex_unlock( &mutex1 )

例子:
#include <stdio.h>
#include <pthread.h>

pthread_mutex_t mutex;		//互斥量聲明
int counter = 0;	//互斥量保護的全局變量

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

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;
}

2.多線程同步
使用pthread_mutex_t + pthread_cond_t + pthread_cond_wait + pthread_cond_signal
例子:
#include <iostream>
#include <pthread.h>
#include <string>
#include <unistd.h>

pthread_mutex_t count_lock;
pthread_cond_t count_nonzero;
unsigned count = 0;

decrement_count () 
{
    pthread_mutex_lock (&count_lock);
    while(count==0)
        pthread_cond_wait( &count_nonzero, &count_lock);

    count=count -1;
    pthread_mutex_unlock (&count_lock);
}
increment_count()
{
    pthread_mutex_lock(&count_lock);
    if(count==0)
        pthread_cond_signal(&count_nonzero);
    count=count+1;
    pthread_mutex_unlock(&count_lock);
}

int main(int argc, char* argv[])
{
        pthread_mutex_init(&count_lock, NULL);
        pthread_cond_init(&count_nonzero, 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); 
}

Makefile
overpass:main.o 
	g++ -g -o  overpass main.o -L. -lhiredis -lpthread -lcurl -lrt -lcrypto -lidn -lssl
main.o:main.cpp
	g++ -g -c main.cpp -o main.o
clean:
	rm -f *.o overpass

其它見(重要:):
http://blog.csdn.net/monkey_d_meng/article/details/5628663
Copyright © Linux教程網 All Rights Reserved