Does an optimistic lock-free FIFO queue implementation exist?

后端 未结 5 1000
轻奢々
轻奢々 2021-01-02 05:40

Is there any C++ implementation (source codes) of \"optmistic approach to lock-free FIFO queues\" algorithm?

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-02 06:32

    How about this lfqueue

    This is cross-platform, unlimited enqueue thread safety queue, have been tested multi deq, multi enq-deq and multi enq. Guarantee memory safe.

    For example

    int* int_data;
    lfqueue_t my_queue;
    
    if (lfqueue_init(&my_queue) == -1)
        return -1;
    
    /** Wrap This scope in other threads **/
    int_data = (int*) malloc(sizeof(int));
    assert(int_data != NULL);
    *int_data = i++;
    /*Enqueue*/
     while (lfqueue_enq(&my_queue, int_data) == -1) {
        printf("ENQ Full ?\n");
    }
    
    /** Wrap This scope in other threads **/
    /*Dequeue*/
    while  ( (int_data = lfqueue_deq(&my_queue)) == NULL) {
        printf("DEQ EMPTY ..\n");
    }
    
    // printf("%d\n", *(int*) int_data );
    free(int_data);
    /** End **/
    
    lfqueue_destroy(&my_queue);
    

提交回复
热议问题