最简单的多线程程序,不多说。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function( void *ptr );
main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}
Results:
Thread 1
Thread 2
Thread 1 returns: 0
Thread 2 returns: 0
Details:
- In this example the same function is used in each thread. The arguments
are different. The functions need not be the same.
- Threads terminate by explicitly calling pthread_exit
,
by letting the function return, or by a call to the function exit
which will terminate the process including any threads.
- Function call: pthread_create
int pthread_create(pthread_t * thread,
const pthread_attr_t * attr,
void * (*start_routine)(void *),
void *arg);
|
Arguments:
-
thread
- returns the thread id. (unsigned long int defined in bits/pthreadtypes.h)
-
attr
- Set to NULL if default thread attributes are used.
(else define members of the struct pthread_attr_t
defined in
bits/pthreadtypes.h)
Attributes include:
-
detached state (joinable? Default: PTHREAD_CREATE_JOINABLE. Other option: PTHREAD_CREATE_DETACHED)
-
scheduling policy (real-time? PTHREAD_INHERIT_SCHED,PTHREAD_EXPLICIT_SCHED,SCHED_OTHER)
-
scheduling parameter
-
inheritsched attribute (Default: PTHREAD_EXPLICIT_SCHED Inherit from parent thread: PTHREAD_INHERIT_SCHED)
-
scope (Kernel threads: PTHREAD_SCOPE_SYSTEM User threads: PTHREAD_SCOPE_PROCESS Pick one or the other not both.)
-
guard size
-
stack address (See unistd.h and bits/posix_opt.h _POSIX_THREAD_ATTR_STACKADDR)
-
stack size (default minimum PTHREAD_STACK_SIZE
set in pthread.h),
-
void * (*start_routine)
- pointer to the function to be threaded. Function has a single argument: pointer to void.
-
*arg
- pointer to argument of function. To pass multiple arguments, send a pointer to a structure.
- Function call: pthread_exit
void pthread_exit(void *retval);
Arguments:
-
retval
- Return value of thread.
This routine kills the thread. The pthread_exit
function never
returns. If the thread is not detached, the thread id and return value
may be examined from another thread by using pthread_join.
Note: the return pointer *retval
, must not be of local scope
otherwise it would cease to exist once the thread terminates.
分享到:
相关推荐
最基本,最常用的测试物理网络的 ping 192.168.0.8 -t ,参数-t是等待用户去中断测试 2.查看DNS、IP、Mac等 A.Win98:winipcfg B.Win2000以上:Ipconfig/all C.NSLOOKUP:如查看河北的DNS C:\>nslookup ...
1.最基本,最常用的,测试物理网络的 ping 192.168.10.88 -t ,参数-t是等待用户去中断测试 2.查看DNS、IP、Mac等 A.Win98:winipcfg B.Win2000以上:Ipconfig/all C.NSLOOKUP:如查看河北的DNS C:\>...
1.安装 2.最配置 3.建Git仓库的两种场景 4.往仓库添加件 5.给件重命名的简单法
最基本题型(说明:此类题型比较简单) 1.烧一根不均匀的绳,从头烧到尾总共需要1个小时。现在有若干条材质相同的绳子,问如何用烧绳的方法来计时一个小时十五分钟呢? 2.你有一桶果冻,其中有黄色、绿色、...
GB 9706.1-2020 医用电气设备 第1部分:基本安全和基本性能的通用要求 最新版60601-1:2012 MOD
1.最基本,最常用的,测试物理网络的 ping 192.168.0.8 -t ,参数-t是等待用户去中断测试 2.查看DNS、IP、Mac等 A.Win98:winipcfg B.Win2000以上:Ipconfig/all C.NSLOOKUP:如查看河北的DNS C:\>...
最基本,最常用的,测试物理网络的 ping 192.168.10.88 -t ,参数-t是等待用户去中断测试 2.查看DNS、IP、Mac等 A.Win98:winipcfg B.Win2000以上:Ipconfig/all C.NSLOOKUP:如查看河北的DNS C:\...
1.最基本,最常用的,测试物理网络的 ping 192.168.0.8 -t ,参数-t是等待用户去中断测试 2.查看DNS、IP、Mac等 A.Win98:winipcfg B.Win2000以上:Ipconfig/all C.NSLOOKUP:如查看河北的DNS C:\>...
最基本,最常用地,测试物理网络地ping192.168.0.8-t,参数-t是等待用户去中断测试中断用ctrl+c(在测试网络是否连通时很长用!)2.查看DNS、IP、Mac等A.Win98:winipcfgB.Win2000以上:I
1.最基本,最常用的,测试物理网络的 ping 192.168.10.88 -t ,参数-t是等待用户去中断测试 2.查看DNS、IP、Mac等 A.Win98:winipcfg B.Win2000以上:Ipconfig/all C.NSLOOKUP:如查看河北的DNS C...
1.最基本,最常用的,测试物理网络的 ping 192.168.10.88 -t ,参数-t是等待用户去中断测试 2.查看DNS、IP、Mac等 A.Win98:winipcfg B.Win2000以上:Ipconfig/all C.NSLOOKUP:如查看河北的DNS C:\>...
1.最基本,最常用的,测试物理网络的 ping 192.168.0.8 -t ,参数-t是等待用户去中断测试 2.查看DNS、IP、Mac等 A.Win98:winipcfg B.Win2000以上:Ipconfig/all C.NSLOOKUP:如查看河北的DNS C:\>...
本章的内容主要翻译自《ASN.1 Communication between Heterogeneous Systems》和《ASN.1 Complete》
最基本,最常用的,测试物理网络的 ping 192.168.0.8 -t ,参数-t是等待用户去中断测试 2.查看DNS、IP、Mac等 A.Win98:winipcfg B.Win2000以上:Ipconfig/all C.NSLOOKUP:如查看河北的DNS C:\>nslookup ...
1. 最基本的抓取 抓取大多数情况属于get请求,即直接从对方服务器上获取数据。 首先,Python中自带urllib及urllib2这两个模块,基本上能满足一般的页面抓取。另外,requests也是非常有用的包,
最基本,最常用的,测试物理网络的 3.网络信使 (经常有人问的~) 2.查看DNS、IP、Mac等 4.探测对方对方计算机名,所在的组、域及当前用户名 (追捕的工作原理) 5.netstat -a 显示出你的计算机当前所开放的所有...
1.最基本,最常用的,测试物理网络的 ping 192.168.10.88 -t ,参数-t是等待用户去中断测试 2.查看DNS、IP、Mac等 A.Win98:winipcfg B.Win2000以上:Ipconfig/all C.NSLOOKUP:如查看河北的DNS C:\>...
第1 章 无线网络导论.............................................................................................................. 24 1.1 为何需要无线? 24 1.1.1 无线频谱:关键资源........................