MCPcopy Create free account
hub / github.com/acl-dev/acl / thread_main

Function thread_main

lib_acl/samples/thread/thread2/main.c:14–93  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12#include <time.h>
13
14static void *thread_main(void *arg acl_unused)
15{
16 pid_t pid;
17 acl_pthread_mutex_t *__lock;
18 int ret;
19 char ebuf[256];
20
21 __lock = (acl_pthread_mutex_t*) acl_mycalloc(1,
22 sizeof(acl_pthread_mutex_t));
23 acl_pthread_mutex_init(__lock, NULL);
24
25 printf("current pid: %d, tid: %lu\r\n", getpid(), acl_pthread_self());
26
27 if ((ret = acl_pthread_mutex_lock(__lock)) == 0)
28 printf("0: main thread(%lu) lock ok\r\n", acl_pthread_self());
29 else
30 printf("0: main thread(%lu) lock error(%d:%s)\r\n",
31 acl_pthread_self(), ret,
32 acl_strerror(ret, ebuf, sizeof(ebuf)));
33
34 switch ((pid = fork())) {
35 case 0:
36 /* 先解锁 */
37 if ((ret = acl_pthread_mutex_unlock(__lock)) == 0)
38 printf("1: child thread(%lu) unlock ok\r\n",
39 acl_pthread_self());
40 else
41 printf("1: child thread(%lu) unlock error(%d:%s)\r\n",
42 acl_pthread_self(), ret,
43 acl_strerror(ret, ebuf, sizeof(ebuf)));
44
45 /* 再加锁 */
46 if ((ret = acl_pthread_mutex_lock(__lock)) == 0)
47 printf("1: child thread(%lu) lock ok\r\n",
48 acl_pthread_self());
49 else
50 printf("1: child thread(%lu) lock error(%d:%s)\r\n",
51 acl_pthread_self(), ret,
52 acl_strerror(ret, ebuf, sizeof(ebuf)));
53
54 /* 再释放锁--该函数会报错 */
55 if ((ret = acl_pthread_mutex_destroy(__lock)) == 0)
56 printf("1: child thread(%lu) destroy ok\r\n",
57 acl_pthread_self());
58 else
59 printf("1: child thread(%lu) destroy error(%d:%s)\r\n",
60 acl_pthread_self(), ret,
61 acl_strerror(ret, ebuf, sizeof(ebuf)));
62 acl_myfree(__lock);
63 exit (0);
64
65 case -1:
66 printf("fork failed\r\n");
67 exit (0);
68 default:
69 printf("0: parent, child pid: %d, tid: %lu\r\n",
70 getpid(), acl_pthread_self());
71 sleep(1);

Callers

nothing calls this directly

Calls 7

acl_pthread_mutex_initFunction · 0.85
acl_pthread_selfFunction · 0.85
acl_pthread_mutex_lockFunction · 0.85
acl_strerrorFunction · 0.85
acl_pthread_mutex_unlockFunction · 0.85
sleepFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…