linux C++多线程

pthread_create: 参数1:指向线程标识符指针。参数2:线程属性。参数3:线程运行函数起始地址。参数4:运行函数的参数。
当创建线程成功时,函数返回0,若不为0则说明创建线程失败,常见的错误返回代码为EAGAIN和EINVAL。前者表示系统限制创建新的线程,例如线程数目过多了;后者表示第二个参数代表的线程属性值非法.
pthread_create的用法:由于pthread库不是Linux系统默认的库,所以在使用pthread_create创建线程时,需要在编译中请加-lpthread参数,eg:gcc -o test -lpthrea test.c

解决kdevelop下编译出现undefind reference to 'pthread_create'错误的方法:

1.第一种方法:只需在CMakeLists.txt中添加如下(第二行和第五行即可):

project(thread)
find_package (Threads)
add_executable(thread
main.cpp)
target_link_libraries (thread ${CMAKE_THREAD_LIBS_INIT})

2.第二种方法:加上pthread即可

project(thread)
add_executable(thread
main.cpp)
target_link_libraries (thread pthread)

 

1.简单多线程示例:

#include <iostream>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <time.h>

void threadAFunc()
{
    int i;
    for(i = 0; i < 2; i++) {
        printf("This is a pthread.\n");
        printf("threadA tid==%u ", (unsigned int)pthread_self());//线程ID
        printf("threadA pid is %d\n", getpid());//进程ID
        pthread_exit((void*)"the threadA is over");
    }

}

static void wait()
{

    time_t start_time = time(NULL);
    while (time(NULL) == start_time) {
        /* do nothing except chew CPU slices for up to one second */
    }
}

static void *threadBFunc(void *vptr_args)
{

    for (int i = 0; i < 5; i++) {
        printf(" %s\n", (char *)vptr_args);
        //wait();
        sleep(1);
    }

    return NULL;
}

int main(int argc, char **argv)
{
    pthread_t threadA;
    int i, retA, retB;
    retA = pthread_create(&threadA, NULL, (void * (*) (void *))threadAFunc, NULL);
    if(retA != 0) {
        printf ("Create pthread error!\n");
        exit (1);
    }
    void * threadA_return;
    pthread_join(threadA, &threadA_return); //阻塞主线程(main函数),直到threadA线程结束(相当于sleep(1));
    printf("%s\n", (char *)threadA_return);

    for(i = 0; i < 2; i++) {
        printf("This is the main process.\n");
    }

    pthread_t threadB;
    //参数1:指向线程标识符指针。参数2:线程属性。参数3:线程运行函数起始地址。参数4:运行函数的参数。
    //主线程(main)和retB交替
    if (retB = pthread_create(&threadB, NULL, threadBFunc, (void *)"b") != 0) {
        return EXIT_FAILURE;
    }
    for (i = 0; i < 5; i++) {
        puts("a");
        wait();
    }

    return 0;
}

pthread_join()的调用者将挂起并等待th线程终止, 还需要注意的是一个线程仅允许唯一的另一个线程使用 pthread_join()等待本线程的终止,并且被等待的线程应该处于可join状态,即非DETACHED状态。

 

2. 循环创建线程:

 

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <pthread.h>
#include <time.h>
#include <string.h>

#define THREADCOUNT 1000

using namespace std;
using namespace tc_server;

void fun((char *vptr_args)
{
    cout << vptr_args << endl;
}
static void *threadAFunc(void *vptr_args) { fun((char*)vptr_args); return NULL; }
int main(int argc, char* argv[]) {char str[THREADCOUNT][10]; pthread_t threadA[THREADCOUNT];
for(int i = 0; i < THREADCOUNT; i++) { sprintf(str[i], "%d", i); if(pthread_create(&threadA[i], NULL, threadAFunc, (void *)str[i]) != 0) { cout << "the threadA create failure" << endl; } cout << endl << "thread_count = " << i + 1 << endl; } sleep(10); return 0; }

 

  

注意: char str[THREADCOUNT][10], 必须保存threadAFunc函数的参数。假设不保存参数,设i = 1,当i = 2的时候前一次的threadAFunc线程
    如果还没执行完毕,可以假设前一次线程刚好执行到cout << vptr_args << endl;,而刚好此时i变为2,因为传递的是i的地址,所以前一次的线程参

   数就被改变了,那么vptr_args就是2了,而当前创建的线程参数也是2,那么就会出现2重复打印的现象.

 

http://zh.wikipedia.org/wiki/POSIX%E7%BA%BF%E7%A8%8B

 

http://www.cnblogs.com/cy163/archive/2008/08/16/1269488.html

 

http://blog.mcuol.com/user/langke279/article/37899_1.htm

 

http://www.cnblogs.com/xuxm2007/archive/2011/04/01/2002217.html

 

posted @ 2012-09-11 12:54  既有缘  阅读(701)  评论(0)    收藏  举报