-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread2.cpp
More file actions
41 lines (36 loc) · 848 Bytes
/
thread2.cpp
File metadata and controls
41 lines (36 loc) · 848 Bytes
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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
using namespace std;
void* thread_main(void* arg);
int main(int argc, char* argv[])
{
pthread_t t_id;
int thread_param = 5;
void* status;
if (pthread_create(&t_id, NULL, thread_main, (void*)&thread_param) != 0) {
puts("thread create error");
exit(-1);
}
cout << "thread begin" << endl;
int err = pthread_join(t_id, &status);
if (err != 0) {
puts("pthread_join error");
exit(-1);
}
cout << "thread return msg :" << (char*)status << endl;
puts("end of main");
return 0;
}
void* thread_main(void* arg)
{
int i;
int cnt = *((int*)arg);
for (i = 0; i < cnt; i++) {
sleep(1);
puts("running thread");
}
return (void*)("thread end");
}