小编Ann*_*lai的帖子

pthread竞争条件,可疑行为

我编写了以下代码来演示同一进程的2个线程之间的竞争条件.

`

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

int c = 0;
void *fnC()
{
    int i;
    for(i=0;i<10;i++)
    {   
        c++;
        printf(" %d", c); 
    }   
}


int main()
{
    int rt1, rt2;
    pthread_t t1, t2; 
    /* Create two threads */
    if( (rt1=pthread_create( &t1, NULL, &fnC, NULL)) )
        printf("Thread creation failed: %d\n", rt1);
    if( (rt2=pthread_create( &t2, NULL, &fnC, NULL)) )
        printf("Thread creation failed: %d\n", rt2);
    /* Wait for both threads to finish */
    pthread_join( t1, NULL);
    pthread_join( t2, NULL);
    printf ("\n");
    return …
Run Code Online (Sandbox Code Playgroud)

c linux posix

1
推荐指数
1
解决办法
5187
查看次数

标签 统计

c ×1

linux ×1

posix ×1