-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy paththread_create.cpp
57 lines (46 loc) · 932 Bytes
/
thread_create.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// Created by jxq on 19-8-14.
//
// p37 poxis 线程(一)
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
using namespace std;
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0);
void * start_routine (void *arg)
{
pthread_detach(pthread_self());
for (int i = 0; i < 20; ++i)
{
cout << "B";
}
return (char *)"hello";
}
int main(int argc, char** argv) {
int ret;
pthread_t thread;
ret = pthread_create(&thread, NULL, start_routine, NULL);
if (ret != 0)
{
ERR_EXIT("pthread_create");
}
for (int i = 0; i < 20; ++i)
{
cout << "A";
}
void *retval;
if (pthread_join(thread, &retval) != 0)
{
ERR_EXIT("pthread_join");
}
printf("\n%s\n", (char *)retval);
return 0;
}