看流星社区

 找回密码
 注册账号
查看: 1707|回复: 0

linux线程与线程安全

[复制链接]

该用户从未签到

发表于 2017-6-1 12:52:11 | 显示全部楼层 |阅读模式
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;
}
[/code]
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
[/code]
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);[/code]
使用:


pthread_mutex_lock( &mutex1 );
g_counter++;
pthread_mutex_unlock( &mutex1 ) [/code]

例子:
#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, 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;
}
[/code]

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);
}
[/code]


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
[/code]

其它见(重要:
5628663
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-3-19 11:46

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表