Multi-Threading support in c11

前端 未结 3 524
忘了有多久
忘了有多久 2020-12-07 10:59

The new C11 standard provides a support for Multi-Threading.
My Questions are a bit diversified but definitely answerable.
I have had a look at the C11 n1570

3条回答
  •  轮回少年
    2020-12-07 11:34

    Regarding What compilers provide support for the above mentioned features?


    Pelles C supports C11 . Creation of threads with Pelles C compiler example:

    #include 
    #include 
    
    #define NUM_THREADS 7
    
    static int threadData[NUM_THREADS];
    
    int threadFunction(void * data) {
        printf("%d-th thread up\n", *(int*)data);
        return 0;
    }
    
    int main(void) {
        thrd_t threadId[NUM_THREADS];
    
        // init thread data
        for (int i=0; i < NUM_THREADS; ++i)
            threadData[i] = i;
    
        // start NUM_THREADS amount of threads
        for (int i=0; i < NUM_THREADS; ++i) {
            if (thrd_create(threadId+i, threadFunction, threadData+i) != thrd_success) {
                printf("%d-th thread create error\n", i);
                return 0;
            }
        }
    
        // wait until all threads terminates
        for (int i=0; i < NUM_THREADS; ++i)
            thrd_join(threadId[i], NULL);
    
        return 0;
    }
    

    EDIT: Eliminated thread shared data problem and problem of exiting from main() earlier than all threads terminates.

提交回复
热议问题