本人转载的 Linux多线程编程(一)和 linux多线程编程(五)解决我的 如下问题:
1、多线程的作用: 即什么情况考虑使用多线程?
2、多线程的创建: 默认参数创建即可!pthread_create(&tid,NULL,(void *)thread_func,NULL);
3、多线程的同步: 掌握互斥,条件 两种同步方式即可!
下表来自于:http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/index.html
表 1. 线程函数列表
对象 | 操作 | Linux Pthread API | Windows SDK 库对应 API |
---|---|---|---|
线程 | 创建 | pthread_create | CreateThread |
退出 | pthread_exit | ThreadExit | |
等待 | pthread_join | WaitForSingleObject | |
互斥锁 | 创建 | pthread_mutex_init | CreateMutex |
销毁 | pthread_mutex_destroy | CloseHandle | |
加锁 | pthread_mutex_lock | WaitForSingleObject | |
解锁 | pthread_mutex_unlock | ReleaseMutex | |
条件 | 创建 | pthread_cond_init | CreateEvent |
销毁 | pthread_cond_destroy | CloseHandle | |
触发 | pthread_cond_signal | SetEvent | |
广播 | pthread_cond_broadcast | SetEvent / ResetEvent | |
等待 | pthread_cond_wait / pthread_cond_timedwait | SingleObjectAndWait |
但我还有一个问题:
4、主线程是否比默认创建的线程优先级更高?
我用如下测试代码及运行结果说明问题:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
void Thread_1()
{
printf("%s pid :0x%x, tid :0x%x \n",__func__, getpid(), (int)pthread_self());
int i=0,j=0;
while(1)
{
for(j=1;j<5000000;j++)
{
}
//sleep(1); // 删除sleep,每个线程都全力运行!
i++;
printf("%s, %d\n", __func__, i);
}
}
void Thread_2()
{
printf("%s pid :0x%x, tid :0x%x \n",__func__, getpid(), (int)pthread_self());
int i=0,j=0;
while(1)
{
for(j=1;j<5000000;j++)
{
}
//sleep(1);
i++;
printf("%s, %d\n", __func__, i);
}
}
void Thread_3()
{
printf("%s pid :0x%x, tid :0x%x \n",__func__, getpid(), (int)pthread_self());
int i=0,j=0;
while(1)
{
for(j=1;j<5000000;j++)
{
}
//sleep(1);
i++;
printf("%s, %d\n", __func__, i);
}
}
void Thread_main()
{
printf("%s pid :0x%x, tid :0x%x \n",__func__, getpid(), (int)pthread_self());
int i=0,j=0;
while(1)
{
for(j=1;j<5000000;j++)
{
}
//sleep(1);
i++;
printf("%s, %d\n", __func__, i);
}
}
int main()
{
pthread_t ppid1,ppid2,ppid3;
printf("%s pid :0x%x, tid :0x%x \n",__func__, getpid(), (int)pthread_self());
pthread_create(&ppid1,NULL,(void *)Thread_1,NULL);
pthread_create(&ppid2,NULL,(void *)Thread_2,NULL);
pthread_create(&ppid3,NULL,(void *)Thread_3,NULL);
Thread_main();
return 0;
}
编译执行结果如下:
$ gcc test.c -lpthread
$ ./a.out
删除sleep,每个线程都全力运行!
运行结果可以看出:主线程没有明显的级别优先。
应避免使用“父线程”和“子线程”的说法
main pid :0x71c5, tid :0x65bfa720
Thread_2 pid :0x71c5, tid :0x64c4b700
Thread_main pid :0x71c5, tid :0x65bfa720
Thread_1 pid :0x71c5, tid :0x6544c700
Thread_3 pid :0x71c5, tid :0x6444a700
Thread_main, 1
Thread_3, 1
Thread_1, 1
Thread_2, 1
Thread_main, 2
Thread_3, 2
Thread_1, 2
Thread_main, 3
Thread_2, 2
Thread_3, 3
Thread_main, 4
Thread_1, 3
Thread_2, 3
Thread_main, 5
Thread_3, 4
Thread_1, 4
Thread_main, 6
Thread_2, 4
Thread_3, 5
Thread_main, 7
Thread_1, 5
Thread_2, 5
Thread_3, 6
Thread_main, 8
Thread_1, 6
Thread_2, 6
Thread_main, 9
Thread_3, 7
Thread_1, 7
Thread_main, 10
Thread_2, 7
Thread_3, 8
Thread_main, 11
Thread_1, 8
Thread_2, 8
Thread_main, 12
Thread_3, 9
Thread_1, 9
Thread_2, 9
Thread_main, 13
Thread_3, 10
Thread_1, 10
Thread_main, 14
Thread_2, 10
Thread_3, 11
Thread_1, 11
Thread_main, 15
Thread_2, 11
Thread_3, 12
Thread_main, 16
Thread_1, 12
Thread_2, 12
Thread_main, 17
Thread_3, 13
Thread_2, 13
Thread_main, 18
Thread_3, 14
Thread_1, 13
Thread_2, 14
Thread_main, 19
Thread_3, 15
Thread_1, 14
Thread_2, 15
Thread_main, 20
Thread_3, 16
Thread_main, 21
Thread_2, 16
Thread_1, 15
Thread_main, 22
Thread_3, 17
Thread_2, 17
Thread_main, 23
Thread_1, 16
Thread_3, 18
Thread_2, 18
Thread_main, 24
Thread_1, 17
Thread_2, 19
Thread_3, 19
Thread_main, 25
Thread_3, 20
Thread_2, 20
Thread_main, 26
Thread_1, 18
Thread_main, 27
Thread_2, 21
Thread_3, 21
Thread_1, 19
Thread_main, 28
Thread_2, 22
Thread_3, 22
Thread_main, 29
Thread_1, 20
Thread_3, 23
Thread_2, 23
Thread_main, 30
Thread_1, 21
Thread_2, 24
Thread_3, 24
Thread_main, 31
Thread_1, 22
Thread_main, 32
Thread_3, 25
Thread_2, 25
Thread_main, 33
Thread_1, 23
Thread_2, 26
Thread_3, 26
Thread_main, 34
Thread_1, 24
Thread_3, 27
Thread_2, 27
Thread_main, 35
Thread_1, 25
Thread_2, 28
Thread_main, 36
Thread_3, 28
Thread_main, 37
Thread_1, 26
Thread_3, 29
Thread_2, 29
Thread_main, 38
Thread_1, 27
Thread_3, 30
Thread_main, 39
..........
Thread_main, 3736
Thread_2, 3671
Thread_1, 2888
Thread_main, 3737
Thread_3, 2840
Thread_2, 3672