设定线程运行栈:pthread_attr_setstack()

本文介绍了在Linux中如何使用pthread_attr_setstack()函数来设定线程栈的地址和大小,以避免在内存紧张的嵌入式平台上的内存浪费。线程栈最小为16KB,超过栈大小会导致核心转储。示例代码展示了栈溢出的情况,当局部变量占用过多栈空间时,可能导致不可预测的线程行为。

概述

  • linux在创建线程时,如果使用默认的栈,默认栈的大小通常为8MB,这对内存比较紧张的嵌入式平台来说,是无法接受的巨量内存浪费;
  • pthread_attr_setstack()可以设定线程栈的地址和大小,设定的栈地址必须以linux页面大小对齐,所以这里使用posix_memalign()分配页面对齐的内存;该内存中不使用时也是使用free()释放;
  • 线程的最小栈大小为16KB,小于这个数值pthread_attr_setstack会设定失败;
  • 在线程中的局部变量是直接从线程栈中分配的,静态局部变量不在线程栈中;所以如果函数调用过多,导致局部变量占用空间超过栈空间,就会引起coredump;
  • 如果使用默认栈大小,还可以调用pthread_attr_setguardsize()设定线程栈的溢出保护大小,当栈溢出时,会发SIGSEGV 信号到该线程;
  • 如果不想设定线程栈的地址,可以使用pthread_attr_setstacksize来只设定栈大小,让kernel自动分配对应大小的栈,栈最小为16KB,并且是页面(通常为4KB)对齐;这时也可以调用pthread_attr_setguardsize设定栈边界保护区大小(至少为一页),防止爆栈后直接coredump;

pthread_attr_setstack示例

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <sys/prctl.h>

#define DBG_PRINT(fmt, args...) {printf("%s %d ", __FUNCTION__, __LINE__);printf(fmt,##args);}

/**
 * [msSleep 用nanosleep实现的毫秒级线程休眠]
 * @param msTime [description]
 */
void msSleep(unsigned msTime)
{
    struct timespec reqTime;
    struct timespec remTime;
    int ret = 0;

    if(msTime == 0)
    {
        DBG_PRINT("invalid argument!\n");
        return;
    }
    else if(msTime>=1000)
    {
        reqTime.tv_sec = msTime/1000;
        reqTime.tv_nsec = (unsigned long)((msTime%1000)*1000000UL);
    }
    else
    {
        reqTime.tv_sec = 0;
        reqTime.tv_nsec = (unsigned long)(msTime*1000000UL);
    }

    do
    {
        /**
         * 由于nanosleep在休眠线程过程中,线程可能会被中断唤醒,并且唤醒后,剩余的休眠时间会保存在
         * remTime中,所以使用remTime中的时间继续休眠线程;
         */
        ret = nanosleep(&reqTime, &remTime);
        if(-1 == ret)
        {
            switch(errno)
            {
                case EINTR:
                    reqTime.tv_sec = remTime.tv_sec;
                    reqTime.tv_nsec = remTime.tv_nsec;
                    continue;
                default:
                    break;
            }

        }
        break;
    }while(1);
}

#define BUFFER_LEN 0x3000
void *testThead1(void* arg)
{
    //设定线程名为zoobiTask1
    prctl(PR_SET_NAME, "zoobiTask1");

    char buffer[BUFFER_LEN];
    while(1)
    {
        DBG_PRINT("Start\n");
        msSleep(300);
        DBG_PRINT("End\n");
    }
}

#define THREAD_STACK_LEN 0x4001
int main(int argc, const char* argv[])
{
    pthread_t thread1ID;
    pthread_attr_t attr;

    int ret = 0;
    void *stackAddr = NULL;
    //获取linux的页大小
    int paseSize = getpagesize();

    DBG_PRINT("The linux page size:0x%x\n", paseSize);

    pthread_attr_init(&attr);
    /**
     * 申请内存,并且内存以页大小对齐,需要申请的内存大小必须是2的整数次幂;
     * 经常用的malloc申请的内存只会默认使用8bytes或者16bytes对齐(依赖平台是32位还是64位);
     */
    ret = posix_memalign(&stackAddr, paseSize, THREAD_STACK_LEN);
    if(0 != ret)
    {
        DBG_PRINT("posix_memalign failed, errno:%s\n", strerror(ret));
        return -1;
    }
#if 1
    /**
     * 设定线程运行栈地址和大小,栈大小最小为16KB,并且栈地址以页面对齐;
     */
    ret = pthread_attr_setstack(&attr, stackAddr, THREAD_STACK_LEN);
    if(0 != ret)
    {
        DBG_PRINT("pthread_attr_setstack failed, errno:%s\n", strerror(ret));
        return -1;
    }
#endif
    void *getstackaddr = NULL;
    size_t getstackSize = 0;
    pthread_attr_getstack(&attr, &getstackaddr, &getstackSize);
    DBG_PRINT("getstackaddr:%p, getstackSize:0x%x\n", getstackaddr, getstackSize);

    ret = pthread_create(&thread1ID, &attr, testThead1, NULL);
    if(ret != 0)
    {
        DBG_PRINT("pthread_create failed! errno:%s\n", strerror(ret));
        return -1;
    }
    pthread_detach(thread1ID);

    while(1)
    {
        msSleep(10000);
    }

    return 0;
}

上面例程编译运行后打印如下:

[zoobi@localhost linux_env]$ g++ thread_stack.cpp -o thread_stack -lpthread
[zoobi@localhost linux_env]$ ./thread_stack
main 90 The linux page size:0x1000
main 117 getstackaddr:0x85f000, getstackSize:0x4001
testThead1 73 Start
testThead1 75 End
testThead1 73 Start

如果将BUFFER_LEN更改为0x4000,如下打印:

[zoobi@localhost linux_env]$ ./thread_stack
main 90 The linux page size:0x1000
main 117 getstackaddr:0x24d1000, getstackSize:0x4001

@

@

@

@C

打印都是乱码,说明局部变量过大导致栈已经溢出,线程运行情况是不确定的;
将pthread_attr_setstack()注释调,然后更改BUFFER_LEN,最大可以更改到0x7fe000;在我的平台测试,改到0x7ff000线程就会coredump,说明在测试平台上默认栈大小为0x800000;

sr/include/time.h:371:27: error: unknown type name ‘timer_t’ extern int timer_settime (timer_t __timerid, int __flags, ^ /usr/include/time.h:376:27: error: unknown type name ‘timer_t’ extern int timer_gettime (timer_t __timerid, struct itimerspec *__value) ^ /usr/include/time.h:380:30: error: unknown type name ‘timer_t’ extern int timer_getoverrun (timer_t __timerid) __THROW; ^ In file included from /usr/include/pthread.h:26:0, from multi_threaded.c:2: /usr/include/bits/pthreadtypes.h:60:27: error: storage class specified for parameter ‘pthread_t’ typedef unsigned long int pthread_t; ^ /usr/include/bits/pthreadtypes.h:69:30: error: storage class specified for parameter ‘pthread_attr_t’ typedef union pthread_attr_t pthread_attr_t; ^ /usr/include/bits/pthreadtypes.h:79:3: error: storage class specified for parameter ‘__pthread_list_t’ } __pthread_list_t; ^ /usr/include/bits/pthreadtypes.h:106:5: error: expected specifier-qualifier-list before ‘__pthread_list_t’ __pthread_list_t __list; ^ /usr/include/bits/pthreadtypes.h:128:3: error: storage class specified for parameter ‘pthread_mutex_t’ } pthread_mutex_t; ^ /usr/include/bits/pthreadtypes.h:134:3: error: storage class specified for parameter ‘pthread_mutexattr_t’ } pthread_mutexattr_t; ^ /usr/include/bits/pthreadtypes.h:154:3: error: storage class specified for parameter ‘pthread_cond_t’ } pthread_cond_t; ^ /usr/include/bits/pthreadtypes.h:160:3: error: storage class specified for parameter ‘pthread_condattr_t’ } pthread_condattr_t; ^ /usr/include/bits/pthreadtypes.h:164:22: error: storage class specified for parameter ‘pthread_key_t’ typedef unsigned int pthread_key_t; ^ /usr/include/bits/pthreadtypes.h:168:13: error: storage class specified for parameter ‘pthread_once_t’ typedef int pthread_once_t; ^ /usr/include/bits/pthreadtypes.h:214:3: error: storage class specified for parameter ‘pthread_rwlock_t’ } pthread_rwlock_t; ^ /usr/include/bits/pthreadtypes.h:220:3: error: storage class specified for parameter ‘pthread_rwlockattr_t’ } pthread_rwlockattr_t; ^ /usr/include/bits/pthreadtypes.h:226:22: error: storage class specified for parameter ‘pthread_spinlock_t’ typedef volatile int pthread_spinlock_t; ^ /usr/include/bits/pthreadtypes.h:235:3: error: storage class specified for parameter ‘pthread_barrier_t’ } pthread_barrier_t; ^ /usr/include/bits/pthreadtypes.h:241:3: error: storage class specified for parameter ‘pthread_barrierattr_t’ } pthread_barrierattr_t; ^ In file included from /usr/include/pthread.h:27:0, from multi_threaded.c:2: /usr/include/bits/setjmp.h:31:18: error: storage class specified for parameter ‘__jmp_buf’ typedef long int __jmp_buf[8]; ^ In file included from multi_threaded.c:2:0: /usr/include/pthread.h:235:28: error: expected declaration specifiers or ‘...’ before ‘pthread_t’ extern int pthread_create (pthread_t *__restrict __newthread, ^ /usr/include/pthread.h:236:28: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token const pthread_attr_t *__restrict __attr, ^ /usr/include/pthread.h:244:13: error: storage class specified for parameter ‘pthread_exit’ extern void pthread_exit (void *__retval) __attribute__ ((__noreturn__)); ^ /usr/include/pthread.h:252:26: error: expected declaration specifiers or ‘...’ before ‘pthread_t’ extern int pthread_join (pthread_t __th, void **__thread_return); ^ /usr/include/pthread.h:273:28: error: expected declaration specifiers or ‘...’ before ‘pthread_t’ extern int pthread_detach (pthread_t __th) __THROW; ^ /usr/include/pthread.h:277:18: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘pthread_self’ extern pthread_t pthread_self (void) __THROW __attribute__ ((__const__)); ^ /usr/include/pthread.h:280:27: error: expected declaration specifiers or ‘...’ before ‘pthread_t’ extern int pthread_equal (pthread_t __thread1, pthread_t __thread2) ^ /usr/include/pthread.h:280:48: error: expected declaration specifiers or ‘...’ before ‘pthread_t’ extern int pthread_equal (pthread_t __thread1, pthread_t __thread2) ^ /usr/include/pthread.h:289:31: error: expected declaration specifiers or ‘...’ before ‘pthread_attr_t’ extern int pthread_attr_init (pthread_attr_t *__attr) __THROW __nonnull ((1)); ^ /usr/include/pthread.h:292:34: error: expected declaration specifiers or ‘...’ before ‘pthread_attr_t’ extern int pthread_attr_destroy (pthread_attr_t *__attr) ^ /usr/include/pthread.h:296:62: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_attr_getdetachstate (const pthread_attr_t *__attr, ^ /usr/include/pthread.h:301:41: error: expected declaration specifiers or ‘...’ before ‘pthread_attr_t’ extern int pthread_attr_setdetachstate (pthread_attr_t *__attr, ^ /usr/include/pthread.h:307:60: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_attr_getguardsize (const pthread_attr_t *__attr, ^ /usr/include/pthread.h:312:39: error: expected declaration specifiers or ‘...’ before ‘pthread_attr_t’ extern int pthread_attr_setguardsize (pthread_attr_t *__attr, ^ /usr/include/pthread.h:313:11: error: expected declaration specifiers or ‘...’ before ‘size_t’ size_t __guardsize) ^ /usr/include/pthread.h:318:61: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_attr_getschedparam (const pthread_attr_t *__restrict __attr, ^ /usr/include/pthread.h:323:40: error: expected declaration specifiers or ‘...’ before ‘pthread_attr_t’ extern int pthread_attr_setschedparam (pthread_attr_t *__restrict __attr, ^ /usr/include/pthread.h:328:62: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_attr_getschedpolicy (const pthread_attr_t *__restrict ^ /usr/include/pthread.h:333:41: error: expected declaration specifiers or ‘...’ before ‘pthread_attr_t’ extern int pthread_attr_setschedpolicy (pthread_attr_t *__attr, int __policy) ^ /usr/include/pthread.h:337:63: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_attr_getinheritsched (const pthread_attr_t *__restrict ^ /usr/include/pthread.h:342:42: error: expected declaration specifiers or ‘...’ before ‘pthread_attr_t’ extern int pthread_attr_setinheritsched (pthread_attr_t *__attr, ^ /usr/include/pthread.h:348:56: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_attr_getscope (const pthread_attr_t *__restrict __attr, ^ /usr/include/pthread.h:353:35: error: expected declaration specifiers or ‘...’ before ‘pthread_attr_t’ extern int pthread_attr_setscope (pthread_attr_t *__attr, int __scope) ^ /usr/include/pthread.h:357:60: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_attr_getstackaddr (const pthread_attr_t *__restrict ^ /usr/include/pthread.h:365:39: error: expected declaration specifiers or ‘...’ before ‘pthread_attr_t’ extern int pthread_attr_setstackaddr (pthread_attr_t *__attr, ^ /usr/include/pthread.h:370:60: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_attr_getstacksize (const pthread_attr_t *__restrict ^ /usr/include/pthread.h:377:39: error: expected declaration specifiers or ‘...’ before ‘pthread_attr_t’ extern int pthread_attr_setstacksize (pthread_attr_t *__attr, ^ /usr/include/pthread.h:378:11: error: expected declaration specifiers or ‘...’ before ‘size_t’ size_t __stacksize) ^ /usr/include/pthread.h:383:56: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_attr_getstack (const pthread_attr_t *__restrict __attr, ^ /usr/include/pthread.h:391:35: error: expected declaration specifiers or ‘...’ before ‘pthread_attr_t’ extern int pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr, ^ /usr/include/pthread.h:392:7: error: expected declaration specifiers or ‘...’ before ‘size_t’ size_t __stacksize) __THROW __nonnull ((1)); ^ /usr/include/pthread.h:423:35: error: expected declaration specifiers or ‘...’ before ‘pthread_t’ extern int pthread_setschedparam (pthread_t __target_thread, int __policy, ^ /usr/include/pthread.h:428:35: error: expected declaration specifiers or ‘...’ before ‘pthread_t’ extern int pthread_getschedparam (pthread_t __target_thread, ^ /usr/include/pthread.h:434:34: error: expected declaration specifiers or ‘...’ before ‘pthread_t’ extern int pthread_setschedprio (pthread_t __target_thread, int __prio) ^ /usr/include/pthread.h:488:26: error: expected declaration specifiers or ‘...’ before ‘pthread_once_t’ extern int pthread_once (pthread_once_t *__once_control, ^ /usr/include/pthread.h:500:12: error: storage class specified for parameter ‘pthread_setcancelstate’ extern int pthread_setcancelstate (int __state, int *__oldstate); ^ /usr/include/pthread.h:504:12: error: storage class specified for parameter ‘pthread_setcanceltype’ extern int pthread_setcanceltype (int __type, int *__oldtype); ^ /usr/include/pthread.h:507:28: error: expected declaration specifiers or ‘...’ before ‘pthread_t’ extern int pthread_cancel (pthread_t __th); ^ /usr/include/pthread.h:512:13: error: storage class specified for parameter ‘pthread_testcancel’ extern void pthread_testcancel (void); ^ /usr/include/pthread.h:521:5: error: expected specifier-qualifier-list before ‘__jmp_buf’ __jmp_buf __cancel_jmp_buf; ^ /usr/include/pthread.h:525:3: error: storage class specified for parameter ‘__pthread_unwind_buf_t’ } __pthread_unwind_buf_t __attribute__ ((__aligned__)); ^ /usr/include/pthread.h:525:3: error: alignment may not be specified for ‘__pthread_unwind_buf_t’ /usr/include/pthread.h:674:40: error: expected declaration specifiers or ‘...’ before ‘__pthread_unwind_buf_t’ extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf) ^ /usr/include/pthread.h:686:42: error: expected declaration specifiers or ‘...’ before ‘__pthread_unwind_buf_t’ extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf) ^ /usr/include/pthread.h:727:36: error: expected declaration specifiers or ‘...’ before ‘__pthread_unwind_buf_t’ extern void __pthread_unwind_next (__pthread_unwind_buf_t *__buf) ^ /usr/include/pthread.h:737:12: error: storage class specified for parameter ‘__sigsetjmp’ extern int __sigsetjmp (struct __jmp_buf_tag *__env, int __savemask) __THROWNL; ^ /usr/include/pthread.h:743:32: error: expected declaration specifiers or ‘...’ before ‘pthread_mutex_t’ extern int pthread_mutex_init (pthread_mutex_t *__mutex, ^ /usr/include/pthread.h:744:37: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token const pthread_mutexattr_t *__mutexattr) ^ /usr/include/pthread.h:748:35: error: expected declaration specifiers or ‘...’ before ‘pthread_mutex_t’ extern int pthread_mutex_destroy (pthread_mutex_t *__mutex) ^ /usr/include/pthread.h:752:35: error: expected declaration specifiers or ‘...’ before ‘pthread_mutex_t’ extern int pthread_mutex_trylock (pthread_mutex_t *__mutex) ^ /usr/include/pthread.h:756:32: error: expected declaration specifiers or ‘...’ before ‘pthread_mutex_t’ extern int pthread_mutex_lock (pthread_mutex_t *__mutex) ^ /usr/include/pthread.h:761:37: error: expected declaration specifiers or ‘...’ before ‘pthread_mutex_t’ extern int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex, ^ /usr/include/pthread.h:767:34: error: expected declaration specifiers or ‘...’ before ‘pthread_mutex_t’ extern int pthread_mutex_unlock (pthread_mutex_t *__mutex) ^ /usr/include/pthread.h:772:64: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_mutex_getprioceiling (const pthread_mutex_t * ^ /usr/include/pthread.h:779:42: error: expected declaration specifiers or ‘...’ before ‘pthread_mutex_t’ extern int pthread_mutex_setprioceiling (pthread_mutex_t *__restrict __mutex, ^ /usr/include/pthread.h:787:38: error: expected declaration specifiers or ‘...’ before ‘pthread_mutex_t’ extern int pthread_mutex_consistent (pthread_mutex_t *__mutex) ^ /usr/include/pthread.h:800:36: error: expected declaration specifiers or ‘...’ before ‘pthread_mutexattr_t’ extern int pthread_mutexattr_init (pthread_mutexattr_t *__attr) ^ /usr/include/pthread.h:804:39: error: expected declaration specifiers or ‘...’ before ‘pthread_mutexattr_t’ extern int pthread_mutexattr_destroy (pthread_mutexattr_t *__attr) ^ /usr/include/pthread.h:808:68: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_mutexattr_getpshared (const pthread_mutexattr_t * ^ /usr/include/pthread.h:814:42: error: expected declaration specifiers or ‘...’ before ‘pthread_mutexattr_t’ extern int pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr, ^ /usr/include/pthread.h:820:65: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_mutexattr_gettype (const pthread_mutexattr_t *__restrict ^ /usr/include/pthread.h:827:39: error: expected declaration specifiers or ‘...’ before ‘pthread_mutexattr_t’ extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind) ^ /usr/include/pthread.h:832:69: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_mutexattr_getprotocol (const pthread_mutexattr_t * ^ /usr/include/pthread.h:839:43: error: expected declaration specifiers or ‘...’ before ‘pthread_mutexattr_t’ extern int pthread_mutexattr_setprotocol (pthread_mutexattr_t *__attr, ^ /usr/include/pthread.h:844:72: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t * ^ /usr/include/pthread.h:850:46: error: expected declaration specifiers or ‘...’ before ‘pthread_mutexattr_t’ extern int pthread_mutexattr_setprioceiling (pthread_mutexattr_t *__attr, ^ /usr/include/pthread.h:856:67: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_mutexattr_getrobust (const pthread_mutexattr_t *__attr, ^ /usr/include/pthread.h:866:41: error: expected declaration specifiers or ‘...’ before ‘pthread_mutexattr_t’ extern int pthread_mutexattr_setrobust (pthread_mutexattr_t *__attr, ^ /usr/include/pthread.h:882:33: error: expected declaration specifiers or ‘...’ before ‘pthread_rwlock_t’ extern int pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock, ^ /usr/include/pthread.h:883:32: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token const pthread_rwlockattr_t *__restrict ^ /usr/include/pthread.h:887:36: error: expected declaration specifiers or ‘...’ before ‘pthread_rwlock_t’ extern int pthread_rwlock_destroy (pthread_rwlock_t *__rwlock) ^ /usr/include/pthread.h:891:35: error: expected declaration specifiers or ‘...’ before ‘pthread_rwlock_t’ extern int pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock) ^ /usr/include/pthread.h:895:38: error: expected declaration specifiers or ‘...’ before ‘pthread_rwlock_t’ extern int pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock) ^ /usr/include/pthread.h:900:40: error: expected declaration specifiers or ‘...’ before ‘pthread_rwlock_t’ extern int pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock, ^ /usr/include/pthread.h:906:35: error: expected declaration specifiers or ‘...’ before ‘pthread_rwlock_t’ extern int pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock) ^ /usr/include/pthread.h:910:38: error: expected declaration specifiers or ‘...’ before ‘pthread_rwlock_t’ extern int pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock) ^ /usr/include/pthread.h:915:40: error: expected declaration specifiers or ‘...’ before ‘pthread_rwlock_t’ extern int pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock, ^ /usr/include/pthread.h:921:35: error: expected declaration specifiers or ‘...’ before ‘pthread_rwlock_t’ extern int pthread_rwlock_unlock (pthread_rwlock_t *__rwlock) ^ /usr/include/pthread.h:928:37: error: expected declaration specifiers or ‘...’ before ‘pthread_rwlockattr_t’ extern int pthread_rwlockattr_init (pthread_rwlockattr_t *__attr) ^ /usr/include/pthread.h:932:40: error: expected declaration specifiers or ‘...’ before ‘pthread_rwlockattr_t’ extern int pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr) ^ /usr/include/pthread.h:936:70: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t * ^ /usr/include/pthread.h:942:43: error: expected declaration specifiers or ‘...’ before ‘pthread_rwlockattr_t’ extern int pthread_rwlockattr_setpshared (pthread_rwlockattr_t *__attr, ^ /usr/include/pthread.h:947:70: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t * ^ /usr/include/pthread.h:953:43: error: expected declaration specifiers or ‘...’ before ‘pthread_rwlockattr_t’ extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *__attr, ^ /usr/include/pthread.h:962:31: error: expected declaration specifiers or ‘...’ before ‘pthread_cond_t’ extern int pthread_cond_init (pthread_cond_t *__restrict __cond, ^ /usr/include/pthread.h:963:35: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token const pthread_condattr_t *__restrict __cond_attr) ^ /usr/include/pthread.h:967:34: error: expected declaration specifiers or ‘...’ before ‘pthread_cond_t’ extern int pthread_cond_destroy (pthread_cond_t *__cond) ^ /usr/include/pthread.h:971:33: error: expected declaration specifiers or ‘...’ before ‘pthread_cond_t’ extern int pthread_cond_signal (pthread_cond_t *__cond) ^ /usr/include/pthread.h:975:36: error: expected declaration specifiers or ‘...’ before ‘pthread_cond_t’ extern int pthread_cond_broadcast (pthread_cond_t *__cond) ^ /usr/include/pthread.h:983:31: error: expected declaration specifiers or ‘...’ before ‘pthread_cond_t’ extern int pthread_cond_wait (pthread_cond_t *__restrict __cond, ^ /usr/include/pthread.h:984:10: error: expected declaration specifiers or ‘...’ before ‘pthread_mutex_t’ pthread_mutex_t *__restrict __mutex) ^ /usr/include/pthread.h:994:36: error: expected declaration specifiers or ‘...’ before ‘pthread_cond_t’ extern int pthread_cond_timedwait (pthread_cond_t *__restrict __cond, ^ /usr/include/pthread.h:995:8: error: expected declaration specifiers or ‘...’ before ‘pthread_mutex_t’ pthread_mutex_t *__restrict __mutex, ^ /usr/include/pthread.h:1002:35: error: expected declaration specifiers or ‘...’ before ‘pthread_condattr_t’ extern int pthread_condattr_init (pthread_condattr_t *__attr) ^ /usr/include/pthread.h:1006:38: error: expected declaration specifiers or ‘...’ before ‘pthread_condattr_t’ extern int pthread_condattr_destroy (pthread_condattr_t *__attr) ^ /usr/include/pthread.h:1010:66: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_condattr_getpshared (const pthread_condattr_t * ^ /usr/include/pthread.h:1016:41: error: expected declaration specifiers or ‘...’ before ‘pthread_condattr_t’ extern int pthread_condattr_setpshared (pthread_condattr_t *__attr, ^ /usr/include/pthread.h:1021:64: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_condattr_getclock (const pthread_condattr_t * ^ /usr/include/pthread.h:1027:39: error: expected declaration specifiers or ‘...’ before ‘pthread_condattr_t’ extern int pthread_condattr_setclock (pthread_condattr_t *__attr, ^ /usr/include/pthread.h:1028:11: error: expected declaration specifiers or ‘...’ before ‘__clockid_t’ __clockid_t __clock_id) ^ /usr/include/pthread.h:1038:31: error: expected declaration specifiers or ‘...’ before ‘pthread_spinlock_t’ extern int pthread_spin_init (pthread_spinlock_t *__lock, int __pshared) ^ /usr/include/pthread.h:1042:34: error: expected declaration specifiers or ‘...’ before ‘pthread_spinlock_t’ extern int pthread_spin_destroy (pthread_spinlock_t *__lock) ^ /usr/include/pthread.h:1046:31: error: expected declaration specifiers or ‘...’ before ‘pthread_spinlock_t’ extern int pthread_spin_lock (pthread_spinlock_t *__lock) ^ /usr/include/pthread.h:1050:34: error: expected declaration specifiers or ‘...’ before ‘pthread_spinlock_t’ extern int pthread_spin_trylock (pthread_spinlock_t *__lock) ^ /usr/include/pthread.h:1054:33: error: expected declaration specifiers or ‘...’ before ‘pthread_spinlock_t’ extern int pthread_spin_unlock (pthread_spinlock_t *__lock) ^ /usr/include/pthread.h:1062:34: error: expected declaration specifiers or ‘...’ before ‘pthread_barrier_t’ extern int pthread_barrier_init (pthread_barrier_t *__restrict __barrier, ^ /usr/include/pthread.h:1063:34: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token const pthread_barrierattr_t *__restrict ^ /usr/include/pthread.h:1068:37: error: expected declaration specifiers or ‘...’ before ‘pthread_barrier_t’ extern int pthread_barrier_destroy (pthread_barrier_t *__barrier) ^ /usr/include/pthread.h:1072:34: error: expected declaration specifiers or ‘...’ before ‘pthread_barrier_t’ extern int pthread_barrier_wait (pthread_barrier_t *__barrier) ^ /usr/include/pthread.h:1077:38: error: expected declaration specifiers or ‘...’ before ‘pthread_barrierattr_t’ extern int pthread_barrierattr_init (pthread_barrierattr_t *__attr) ^ /usr/include/pthread.h:1081:41: error: expected declaration specifiers or ‘...’ before ‘pthread_barrierattr_t’ extern int pthread_barrierattr_destroy (pthread_barrierattr_t *__attr) ^ /usr/include/pthread.h:1085:72: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token extern int pthread_barrierattr_getpshared (const pthread_barrierattr_t * ^ /usr/include/pthread.h:1091:44: error: expected declaration specifiers or ‘...’ before ‘pthread_barrierattr_t’ extern int pthread_barrierattr_setpshared (pthread_barrierattr_t *__attr, ^ /usr/include/pthread.h:1105:32: error: expected declaration specifiers or ‘...’ before ‘pthread_key_t’ extern int pthread_key_create (pthread_key_t *__key, ^ /usr/include/pthread.h:1110:32: error: expected declaration specifiers or ‘...’ before ‘pthread_key_t’ extern int pthread_key_delete (pthread_key_t __key) __THROW; ^ /usr/include/pthread.h:1113:35: error: expected declaration specifiers or ‘...’ before ‘pthread_key_t’ extern void *pthread_getspecific (pthread_key_t __key) __THROW; ^ /usr/include/pthread.h:1116:33: error: expected declaration specifiers or ‘...’ before ‘pthread_key_t’ extern int pthread_setspecific (pthread_key_t __key, ^ /usr/include/pthread.h:1122:35: error: expected declaration specifiers or ‘...’ before ‘pthread_t’ extern int pthread_getcpuclockid (pthread_t __thread_id, ^ /usr/include/pthread.h:1123:7: error: expected declaration specifiers or ‘...’ before ‘__clockid_t’ __clockid_t *__clock_id) ^ /usr/include/pthread.h:1139:12: error: storage class specified for parameter ‘pthread_atfork’ extern int pthread_atfork (void (*__prepare) (void), ^ multi_threaded.c:5:21: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token int create_socket() { ^ multi_threaded.c:18:30: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token void bind_socket(int sockfd) { ^ multi_threaded.c:33:48: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token const char* get_content_type(const char* path) { ^ multi_threaded.c:50:36: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token int is_path_safe(const char* path) { ^ multi_threaded.c:55:40: error: unknown type name ‘http_status_t’ void send_http_response(int client_fd, http_status_t status, ^ multi_threaded.c:56:51: error: unknown type name ‘off_t’ const char* content_type, off_t content_length) { ^ multi_threaded.c:79:41: error: unknown type name ‘http_status_t’ void send_error_response(int client_fd, http_status_t status) { ^ multi_threaded.c:111:32: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token void* handle_client(void* arg) { ^ multi_threaded.c:182:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token int main() { ^ In file included from web_server.h:3:0, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from web_server.h:2, from multi_threaded.c:1: /usr/include/signal.h:403:12: error: old-style parameter declarations in prototyped function definition extern int __libc_current_sigrtmin (void) __THROW; ^ multi_threaded.c:218:1: error: expected ‘{’ at end of input } ^ multi_threaded.c:218:1: warning: control reaches end of non-void function [-Wreturn-type] } ^ makefile:13: recipe for target 'threaded_server' failed make: *** [threaded_server] Error 1 出现一大堆报错
最新发布
08-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值