linux线程与线程安全
0.在linux中创建一个线程
main.cpp
Makefile
引用了thead库
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.多线程互斥
例子:
2.多线程同步
使用pthread_mutex_t + pthread_cond_t + pthread_cond_wait + pthread_cond_signal
例子:
Makefile
其它见(重要:):
http://blog.csdn.net/monkey_d_meng/article/details/5628663
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